mardi 23 juin 2015

Add checkbox control to header of the gridview programmatically

I have created a webform which imports the excel file into gridview. Now I want to add checkbox control to each header (along with the header text).

Below is the code where I am 1. Importing the excel data. 2. Store it into a datatable 3. Creating gridview dynamically 4. Binding the data into the gridview.

    Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
    If FileUpload1.HasFile Then
        Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
        Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
        Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")

        Dim FilePath As String = Server.MapPath(FolderPath + FileName)
        FileUpload1.SaveAs(FilePath)
        Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
    End If
End Sub

    Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)
    Dim conStr As String = ""
    Select Case Extension
        Case ".xls"

            conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
            Exit Select
        Case ".xlsx"

            conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
            Exit Select
    End Select
    conStr = String.Format(conStr, FilePath, isHDR)

    Dim connExcel As New OleDbConnection(conStr)
    Dim cmdExcel As New OleDbCommand()
    Dim oda As New OleDbDataAdapter()
    Dim dt As New DataTable()

    cmdExcel.Connection = connExcel


    connExcel.Open()
    Dim dtExcelSchema As DataTable
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
    connExcel.Close()


    connExcel.Open()
    cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
    oda.SelectCommand = cmdExcel
    oda.Fill(dt)
    connExcel.Close()


    Dim GridView1 As GridView = New GridView
    GridView1.AutoGenerateColumns = False
    For i As Integer = 0 To dt.Columns.Count - 1
        Dim boundfield As BoundField = New BoundField

        boundfield.DataField = dt.Columns(i).ColumnName.ToString()
        boundfield.HeaderText = dt.Columns(i).ColumnName.ToString()

        GridView1.Columns.Add(boundfield)
    Next


    GridView1.DataSource = dt
    GridView1.DataBind()
    Panel1.Controls.Add(GridView1)
End Sub

All working fine. But when it comes to adding checkbox to header text, I am completely blank. Kindly suggest the solution or any other approach I can use.




Aucun commentaire:

Enregistrer un commentaire