我在按钮点击
上创建了如下所示的datagridviewprivate void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Columns.Contains("column1") && dataGridView1.Columns.Contains("chk"))
{
}
else
{
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
//chk.ReadOnly = true;
chk.Name = "chk";
chk.DisplayIndex = 0;
chk.Frozen = true;
chk.HeaderText = "check";
dataGridView1.Columns.Add(chk);
DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
column1.ReadOnly = true;
column1.Name = "column1";
column1.HeaderText = "my column name";
dataGridView1.Columns.Add(column1);
DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
column2.ReadOnly = true;
column2.Name = "column2";
column2.HeaderText = "my column name1";
dataGridView1.Columns.Add(column2);
}
}
但是当我点击复选框时,会自动创建ROW,为什么会发生这种情况..
答案 0 :(得分:3)
这是因为默认情况下属性AllowUserToAddRows
为true。
设置
dataGridView1.AllowUserToAddRows = false;
并在添加列之后。向dataGridView1
添加一个新行dataGridView1.Rows.Add();