为什么在单击动态创建的复选框列时添加了新行

时间:2010-12-31 03:59:12

标签: c# winforms datagridview

我在按钮点击

上创建了如下所示的datagridview
private 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,为什么会发生这种情况..

1 个答案:

答案 0 :(得分:3)

这是因为默认情况下属性AllowUserToAddRows为true。

设置

dataGridView1.AllowUserToAddRows = false;

并在添加列之后。向dataGridView1

添加一个新行
dataGridView1.Rows.Add();