datagridview共有四列控件,第一列为checkbox列,中间两列绑定dataTable数据源,第四列为动态添加的textbox列。
dataTable是一个public static变量.
当checkbox列被选中时,第四列相应的cell的readonly属性为false。现在的问题是:当我第一次打开这个窗口时,一切正常。但当我第二次打开这个窗口时,当第一列的checkbox列为选中时,却是第三列相对应的cell的readonly属性为false。
不明白为什么:
下面是刚打开窗口时,设置datagridview数据源和动态创建第四列的代码:
 if(PostToServer.dtDataList.Rows.Count<=0)//PostToServer.dtDataList这是一个static变量
                        DREAMmail_Desktop_Login.postclass.GetFieldFunc();                    DataRow childrow = dtData.NewRow();//dtData是这个窗体的私有变量
                    foreach (DataRow dtrow in PostToServer.dtDataList.Rows)
                    {
                        childrow[0] = dtrow[0];
                        childrow[1] = dtrow[1].ToString() +"( "+ dtrow[2].ToString()+" )";
                        dtData.Rows.Add(childrow);
                        childrow = dtData.NewRow();
                    }
                    //Set up datagridView'properties
                   
                    DataGridViewTextBoxColumn txtCol = new DataGridViewTextBoxColumn();
                    dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                    txtCol.HeaderText = "Value";
                    dataGridView1.Columns.Insert(3, txtCol);
                    dataGridView1.Rows[0].Cells[3].Style.BackColor = Color.Aquamarine;
                    int i = 1;
                    for (; i < dataGridView1.Rows.Count; i++)
                    {
                        DataGridViewTextBoxCell txtCell = (DataGridViewTextBoxCell)dataGridView1.Rows[i].Cells[3];
                        txtCell.ReadOnly = true;
                        txtCell.Style.BackColor = Color.Silver;
                    }
                    dataGridView1.Rows[0].Cells[3].Style.BackColor = Color.Aquamarine;                    DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dataGridView1.Rows[0].Cells[0];
                    cbx.Value = true;
                    cbx.ReadOnly = true;
}
下面函数是datagridview空间的cellcontentclick事件:
 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {             
                if ((bool)dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue)
                {
                   // MessageBox.Show("changed");
                    dataGridView1.Rows[e.RowIndex].Cells[3].ReadOnly = false;
                    dataGridView1.Rows[e.RowIndex].Cells[3].Style.BackColor = Color.Aquamarine;
                }
                else
                {
                    dataGridView1.Rows[e.RowIndex].Cells[3].ReadOnly = true;
                    dataGridView1.Rows[e.RowIndex].Cells[3].Style.BackColor = Color.Silver;
                }
            }
        }
谢谢大家!!