[DevExpress]gridcontrol中单元格属性设置为RepositoryItemCheckEdit
Posted 厦门德仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[DevExpress]gridcontrol中单元格属性设置为RepositoryItemCheckEdit相关的知识,希望对你有一定的参考价值。
默认情况下,RepositoryItemCheckEdit的空值,会显示为灰色半选中状态,要想改为白色空状态,需要修改NullStyle属性为Unchecked。
如果RepositoryItemCheckEdit所在的列未绑定数据源,那么该列默认只可以单选;如果绑定了数据源,那么可以同时多选。
ColumEdit关联FieldName数据源,但是在设计功能中并不仅仅这样就可以同时多选
窗体初始化时:
this.repositoryItemCheckEdit1.QueryCheckStateByValue += new DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventHandler(this.repositoryItemCheckEdit1_QueryCheckStateByValue);
gridView1.OptionsSelection.MultiSelect = true;
实现注册事件:
private void repositoryItemCheckEdit1_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
{
string val = "";
if (e.Value != null)
{
val = e.Value.ToString();
}
else
{
val = "False";//默认为不选
}
switch (val)
{
case "True":
case "Yes":
case "1":
e.CheckState = CheckState.Checked;
break;
case "False":
case "No":
case "0":
e.CheckState = CheckState.Unchecked;
break;
default:
e.CheckState = CheckState.Unchecked;
break;
}
e.Handled = true;
}
在绑定数据源的情况下实现只可以单选,可以通过循环每行的状态来实现
/// <summary>
/// 选择状态改变触发事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
DataTable dt = gridControl1.DataSource as DataTable;
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["Select"] = false;
}
gridControl1.RefreshDataSource();
}
绑定数据源后的全选和反选
//全选
private void btnSelectAll_Click(object sender, EventArgs e)
{
DataTable dt = gridControl1.DataSource as DataTable;
foreach (DataRow dr in dt.Rows)
{
dr["Select"] = "True";
}
}
//反选
private void btnInvertSelect_Click(object sender, EventArgs e)
{
DataTable dt = gridControl1.DataSource as DataTable;
foreach (DataRow dr in dt.Rows)
{
string str = dr["Select"].ToString();
if (str == "True")
dr["Select"] = "False";
else
dr["Select"] = "True";
}
}
获取勾选记录:
private void GetRecord(object sender, EventArgs e)
{
List<string> delList = new List<string>();
if (gridView1.FocusedRowHandle >= 0)
{
for (int i = 0; i < gridView1.RowCount; i++)
{
string obj = gridView1.GetRowCellValue(i, "Select").ToString();
if (obj.Equals("True"))
{
string id = this.gridView1.GetRowCellValue(i, "i_id").ToString();
delList.Add(id);
}
}
if (delList.Count > 0)
{
DialogResult result = MessageBox.Show("确定删除记录?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
string strDel = string.Join(",", delList);
pADOHelper.ExecuteNonQuery(string.Format("DELETE from {0} where i_id in ({1})", CommonClass.tableConfigMapLayers, strDel));
RefreshMapLayers();
}
}
else
MessageBox.Show("请勾选数据"); return;
}
}
以上是关于[DevExpress]gridcontrol中单元格属性设置为RepositoryItemCheckEdit的主要内容,如果未能解决你的问题,请参考以下文章
Devexpress GridControl 设置combobox下拉框
如何在GridControl中显示图片列?控件DevExpress.XtraGrid.GridControl中显示图片列。