本文介绍了如何反转DataGridView中的Checkbox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个从绑定源填充的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)

推荐答案

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;
            }
        }
    }


DataGridViewCell cell = // some cell of the runtime type DataGridViewCheckBoxCell
cell.Value = !((bool)cell.Value);



如果必须,可以检查单元格的运行时类型,或者,更好的是,属性 DataGridViewCell.ValueType ,以确保您正在使用单元格或适当的类型。



请参阅:

[],

[],

[],

[] 。



-SA


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[^].

—SA


这篇关于如何反转DataGridView中的Checkbox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 21:58