[winform]Datagrid中CheckedBox选择问题?? CurrentCellChanged中直接将checkBox设置为两种情况,true,false;一次dataGrid[当前行,当前列]=bool.Parse("true");第二次dataGrid[当前行,当前列]=bool.Parse("false");重复即可。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DataGridBoolColumn.AllowNull = False另外数据源中的数据也应该是没有DBNull值的。 我这样写:datagrid1.Columns["IsMandatory"].AllowDBNull=false;可还是有灰色出现,郁闷 CurrentCellChanged中直接写if (dataGrid.CurrentCell.ColumnNumber==你的CheckedBox列数){ string checkBoxValue=dataGrid[dataGrid.CurrentRowIndex,CheckedBox列数].ToString(); if (checkBoxValue=="") checkBoxValue="true"; bool bcheckBoxValue=bool.Parse(checkBoxValue); dataGrid[dataGrid.CurrentRowIndex,CheckedBox列数]=!bCurBoolValue;} 不好意思,错了,将dataGrid[dataGrid.CurrentRowIndex,CheckedBox列数]=!bCurBoolValue;改成dataGrid[dataGrid.CurrentRowIndex,CheckedBox列数]=!bcheckBoxValue; 不会吧,还不能用。你在CurrentCellChanged事件中的程序贴出来了看一下。 建列的程序在上面,private void dgdFunctionArea_CurrentCellChanged(object sender, System.EventArgs e) { if (dgdFunctionArea.CurrentCell.ColumnNumber==3) { string checkBoxValue=dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3].ToString(); if (checkBoxValue=="") checkBoxValue="true"; bool bcheckBoxValue=bool.Parse(checkBoxValue); dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3]=bcheckBoxValue; } } 这个dgdFunctionArea_CurrentCellChanged事件,在鼠标第一次点击cell时触发,当鼠标不移动然在cell里点,他就没反应了 private void Form1_Load(object sender, System.EventArgs e) { DataTable dtblFunctionalArea = new DataTable(); DataColumn dtcCheck = new DataColumn("IsMandatory"); dtcCheck.DataType = System.Type.GetType("System.Boolean"); dtcCheck.DefaultValue = true; dtblFunctionalArea.Columns.Add(dtcCheck); DataColumn dc = new DataColumn("dd"); dtblFunctionalArea.Columns.Add(dc); for(int i = 1; i< 10;i++) { DataRow dr = dtblFunctionalArea.NewRow(); dr[1] = "A"+i.ToString(); dtblFunctionalArea.Rows.Add(dr); } dataGrid1.DataSource = dtblFunctionalArea; } private void dataGrid1_Click(object sender, System.EventArgs e) { if (dataGrid1.CurrentCell.ColumnNumber==0) { string checkBoxValue=dataGrid1[dataGrid1.CurrentRowIndex,0].ToString(); if (checkBoxValue=="") checkBoxValue="true"; bool bcheckBoxValue=bool.Parse(checkBoxValue); dataGrid1[dataGrid1.CurrentRowIndex,0]=!bcheckBoxValue; } }这样子可以的。 你的程序中dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3]=bcheckBoxValue;要改成dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3]=!bcheckBoxValue; 把你的考过去用,但还是会出现 “灰色选中”...........可以把你程序发来吗? [email protected] 上面程序确实还会有些问题,下面用MouseUp就不会有问题了。有问题继续探讨。private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e){ DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(e.X, e.Y); try { if( hti.Type == DataGrid.HitTestType.Cell && hti.Column == 0) { this.dataGrid1[hti.Row, hti.Column] = ! (bool) this.dataGrid1[hti.Row, hti.Column]; } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } }刚才回复不了了,提示不过连续三次以上回贴。 确实可以拉,谢谢luminrong(卢民荣) C#怎么接收C++传回来的指针类型 c#中如何将字符串转化成GBK编码? 高手进来指导,获得奖励分,在线等候!! FTP上传时遇到的BUG? datagrid 修改问题? 网站上传服务器之无法写入session 关于object类型的问题 C# winform加载子窗体很慢 如何用进度条显示 string转URI Proces p = System.Diagnostics.Process.Start(v_OpenFilePath)的返回值问题 Web的TreeView空件简单问题 关于C#软件怎么让别人才可以使用?
另外数据源中的数据也应该是没有DBNull值的。
datagrid1.Columns["IsMandatory"].AllowDBNull=false;
可还是有灰色出现,郁闷
if (dataGrid.CurrentCell.ColumnNumber==你的CheckedBox列数)
{
string checkBoxValue=dataGrid[dataGrid.CurrentRowIndex,CheckedBox列数].ToString();
if (checkBoxValue=="")
checkBoxValue="true";
bool bcheckBoxValue=bool.Parse(checkBoxValue);
dataGrid[dataGrid.CurrentRowIndex,CheckedBox列数]=!bCurBoolValue;
}
private void dgdFunctionArea_CurrentCellChanged(object sender, System.EventArgs e)
{
if (dgdFunctionArea.CurrentCell.ColumnNumber==3)
{
string checkBoxValue=dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3].ToString();
if (checkBoxValue=="")
checkBoxValue="true";
bool bcheckBoxValue=bool.Parse(checkBoxValue);
dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3]=bcheckBoxValue;
} }
当鼠标不移动然在cell里点,他就没反应了
{
DataTable dtblFunctionalArea = new DataTable();
DataColumn dtcCheck = new DataColumn("IsMandatory");
dtcCheck.DataType = System.Type.GetType("System.Boolean");
dtcCheck.DefaultValue = true;
dtblFunctionalArea.Columns.Add(dtcCheck); DataColumn dc = new DataColumn("dd");
dtblFunctionalArea.Columns.Add(dc);
for(int i = 1; i< 10;i++)
{
DataRow dr = dtblFunctionalArea.NewRow();
dr[1] = "A"+i.ToString();
dtblFunctionalArea.Rows.Add(dr);
}
dataGrid1.DataSource = dtblFunctionalArea;
} private void dataGrid1_Click(object sender, System.EventArgs e)
{
if (dataGrid1.CurrentCell.ColumnNumber==0)
{
string checkBoxValue=dataGrid1[dataGrid1.CurrentRowIndex,0].ToString();
if (checkBoxValue=="")
checkBoxValue="true";
bool bcheckBoxValue=bool.Parse(checkBoxValue);
dataGrid1[dataGrid1.CurrentRowIndex,0]=!bcheckBoxValue;
}
}
这样子可以的。
dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3]=bcheckBoxValue;要改成
dgdFunctionArea[dgdFunctionArea.CurrentRowIndex,3]=!bcheckBoxValue;
可以把你程序发来吗?
[email protected]
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(e.X, e.Y);
try
{
if( hti.Type == DataGrid.HitTestType.Cell && hti.Column == 0)
{
this.dataGrid1[hti.Row, hti.Column] = ! (bool) this.dataGrid1[hti.Row, hti.Column];
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
刚才回复不了了,提示不过连续三次以上回贴。