Click here to Skip to main content
15,885,933 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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.


VB
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.
Posted
Comments
Lakhan Anjana 23-Jun-15 9:07am    
I had to specify which checkbox object, like this

System.Web.UI.WebControls.CheckBox
Also I had to add this to the gridview aspx page

OnRowDataBound="GridView1_RowDataBound"
Abhishek Pant 24-Jun-15 1:33am    
Please have a look to this thread's answer here[^]
Mahatma Aladdin 24-Jun-15 4:23am    
@Abhishek Thanx. Exactly what I needed.
Abhishek Pant 24-Jun-15 15:00pm    
My Pleasure :)

1 solution

Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
rect.Y = 3;
rect.X = rect.Location.X + (rect.Width/4);
CheckBox checkboxHeader = new CheckBox();
checkboxHeader.Name = "checkboxHeader";
//datagridview[0, 0].ToolTipText = "sdfsdf";
checkboxHeader.Size = new Size(18, 18);
checkboxHeader.Location = rect.Location;
checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
dataGridView1.Controls.Add(checkboxHeader);

Call this method while binding the Data Grid, above code will add it as control
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900