C# 在 Winform 中的 Devexpres 网格中选中取消选中复选框列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11668980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check Uncheck Checkbox column in Devexpres grid in Winform
提问by user1528573
Hello developers I am using VS 2010 .I have a dev express grid in which I have a checkbox column.The problem is When i check the checkbox it gets checked but when i move to any other cell or column the checkbox gets automatically unchecked.Till now my code is as follows
您好开发人员我正在使用 VS 2010。我有一个开发快速网格,其中有一个复选框列。问题是当我选中复选框时,它会被选中,但是当我移动到任何其他单元格或列时,复选框会自动取消选中。直到现在我的代码如下
if (e.Column.ToString()=="Active" )
{
RepositoryItemCheckEdit edit = UserInfoGridView.GridControl.RepositoryItems.Add("CheckEdit") as RepositoryItemCheckEdit;
column = e.Column;
column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
column.Visible = true;
column.VisibleIndex = 3;
column.FieldName = "CheckMarkSelection";
column.Caption = "Active";
column.OptionsColumn.ShowCaption = true;
column.OptionsColumn.AllowEdit = true;
column.OptionsColumn.AllowSize = false;
column.UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
column.ColumnEdit = edit;
}
采纳答案by user1528573
Well I found the Answer.........What I did was this
好吧,我找到了答案.......我所做的是这个
public frmLoad()
{
InitializeComponent();
string DisplayQuery = "Select * from TableName";
MasterDs = SqlHelper.ExecuteDataset(CommonClass.ConnectionString, CommandType.Text, DisplayQuery);
MasterDs.Tables[0].Columns.Add("FLAG", typeof(string));
MainGrid.DataSource = MasterDs.Tables[0];
gridview.PopulateColumns();
gridview.Columns["ID"].VisibleIndex = -1;
gridview.Columns["FLAG"].VisibleIndex = -1;
DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit selectnew = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
gridview.Columns["ColName"].ColumnEdit = selectnew;
selectnew.NullText = "";
selectnew.ValueChecked = "Y";
selectnew.ValueUnchecked = "N";
selectnew.ValueGrayed = "-";
}
回答by SidAhmed
It may be because the datasource to witch the grid is bound is not being modified after the check uncheck action. Do you handle the CellValueChangedevent of your view to change the property value of an object of your darasource ?
这可能是因为在 check uncheck 操作后没有修改绑定网格的数据源。您是否处理CellValueChanged视图事件以更改 darasource 对象的属性值?
private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
if (e.Column.Name != "Active")
return;
var person = gridView1.GetFocusedRow() as Person;
person.Active = Convert.ToBoolean(e.Value);
}
Or, if you use a DataSet as a Datasource of your grid :
或者,如果您使用 DataSet 作为网格的数据源:
var id = gridView1.GetFocusedRowCellValue("IdColumnName");
var active = gridView1.GetFocusedRowCellValue("ActiveColumnName");
NorthwindDataSet.PersonsRow PersonsRow =
northwindDataSet1.Persons.FindByPersonID(id);
PersonsRow.ACTIVE= Convert.ToBoolean(active);
PS : Not tested
PS:未测试
回答by Eldar Zeynalov
Try like this
string DisplayQuery = "declare @Active bit; set @Active=0; select @Active as Active, * from TableName";
试试这个
字符串 DisplayQuery = "declare @Active bit; set @Active=0; select @Active as Active, * from TableName";
回答by Pragmateek
The issue is probably caused by an incorrect underlying type for the column: it should be a boolto work out-of-the-box.
该问题可能是由列的不正确基础类型引起的:它应该是bool开箱即用的。
To set the type you may try something like:
要设置类型,您可以尝试以下操作:
MasterDs.Tables[0].Columns[0].DataType = typeof(bool);
回答by sweetfa
I am using Entity Framework 6.1 as the backend with ObjectContext. I had set the database type to be tinyint, which maps to byte when entity framework updates the classes.
我使用 Entity Framework 6.1 作为 ObjectContext 的后端。我已将数据库类型设置为 tinyint,当实体框架更新类时映射到字节。
Changing the database type to bit and refreshing the entity objects creates the correct boolean type, after which the checkbox shows in DevExpress Winforms correctly.
将数据库类型更改为 bit 并刷新实体对象会创建正确的布尔类型,之后复选框在 DevExpress Winforms 中正确显示。

