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;
}
}
}
谢谢大家!!
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;
}
}
}
谢谢大家!!
解决方案 »
- 如果记录软件的运行时间?
- ListView访问数据库
- c#打开窗口问题,高手来答答
- 用serialPort控件使用com1口自发自接数据(50分)
- using 在下面方法中的作用?
- 关于vs2005中Excel的引用的问题
- 问一个OleDbCommand连接access的我问题
- 新手。C#模仿一个简单聊天程序,出现System.Net.Sockets.SocketException”类型的未经处理的异常出现在 System.dll 中
- 新手问题,希望大神能帮忙解决
- 如何编写winform主窗口的浮动窗口.
- 异步socket,如何保存客户端的连接?及向这些连接广播?要加锁?
- 如果将 这个数据文件中的 0xd2 转为 0x00,是用Replace吗?
运行的时候发现有时候这一列的index是最后一列,有时候是0
最后给绑定数据源就行了可以尝试也给他绑定数据源里面的空值,这样他的index就不会乱了