如何反转DataGridView中的Checkbox值 [英] How to invert Checkbox value in DataGridView
问题描述
大家好
我有一个从绑定源填充的DataGridView
我从源获得的数据包含一个0,1列女巫我将其显示为复选框
我想反转DataGridView中的复选框值
(表示如果值= 0>>已检查,如果值= 1>> unchecked)
Hi all
I have a DataGridView which filled from binding source
the data i get from the source contains a 0,1 column witch i show it as checkbox
I want to invert checkbox value in the DataGridView
(means if value=0>>checked , if value=1>>unchecked)
推荐答案
Hello Mr.Daban,
您可以通过使用.net控件来访问gridView的rowDataBound事件的任何控件值/>
以下代码是c#语言
Hello Mr.Daban,
You Can access any control value on rowDataBound event of gridView by using .net control
below code is in c# language
protected void YourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text == 0)
{
((CheckBox)e.Rows.Cells[1].Controls[0]).Checked= true;
}
else if (e.Row.Cells[0].Text == 1)
{
((CheckBox)e.Rows.Cells[1].Controls[0]).Checked= false;
}
}
}
如果您的单元格确实是复选框单元格,则值不是0或1,它们是布尔值。
您可以对问题中的所有单元格执行此操作:
If your cells are really check box cells, the values are not 0 or 1, they are Boolean values.
You can do this to all the cells in questions:
DataGridViewCell cell = // some cell of the runtime type DataGridViewCheckBoxCell
cell.Value = !((bool)cell.Value);
如果必须,可以检查单元格的运行时类型,或者,更好的是,属性 DataGridViewCell.ValueType
,以确保您正在使用单元格或适当的类型。
请参阅:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell(v = vs.110).aspx [ ^ ],
https:// msdn。 microsoft.com/en-us/library/system.windows.forms.datagridviewcell%28v=vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en -us / library / system.windows.forms.datagridviewcell.value(v = vs.110).aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.valuetype(v = vs.110).aspx [ ^ ] 。
If you have to, you can checkup either the runtime type of the cell, or, better, the property DataGridViewCell.ValueType
, to make sure you are working with the cell or appropriate type.
Please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.value(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.valuetype(v=vs.110).aspx[^].
这篇关于如何反转DataGridView中的Checkbox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!