且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式将复选框控件添加到gridview的标题

更新时间:2023-11-30 16:01:28

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);



绑定数据网格时调用此方法,上面的代码将其添加为控件


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.

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