繁体   English   中英

如何使用VB.Net中的datagridview windowsform在我的复选框列中添加复选框

[英]How to add a checkbox in my checkbox column using datagridview windowsform in VB.Net

Private Sub Recipients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    mycom.Connection = cn
    mycom.CommandText = <SQL> SELECT Idno,Name,YearSec,Course,Organization FROM tbl_students </SQL>.Value

    Dim myadap As New MySqlDataAdapter(mycom)
    Dim mydt As New DataTable

    mydt.Columns.Add("", GetType(Boolean))
    myadap.Fill(mydt)
    grdRecipients.DataSource = mydt

    myadap.Dispose()

End Sub

我在我的复选框列的标题中创建一个复选框时遇到问题,该复选框应该是复选框,以便我可以检查数据gridview中存在的所有行。 任何答案都会有很大帮助。 谢谢

像这样尝试

    DataGridViewCheckBoxColumn c1;
    CheckBox ckBox;

   grdRecipients.DataSource = mydt

   ckBox = new CheckBox();

  //Get the column header cell bounds
  Rectangle rect =
  this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
  ckBox.Size = new Size(18, 18);

  //Change the location of the CheckBox to make it stay on the header
  ckBox.Location = rect.Location;
  ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);

  //Add the CheckBox into the DataGridView
  this.dataGridView1.Controls.Add(ckBox);

下面的方法用于选中取消选中所有复选框

     void ckBox_CheckedChanged(object sender, EventArgs e)
    {
        for (int j = 0; j < this.dataGridView1.RowCount; j++)
        {
            this.dataGridView1[0, j].Value = this.ckBox.Checked;
        }
        this.dataGridView1.EndEdit();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM