因为用DevExpress控件很不熟练,故下面实现一个自定义验证方法。思路为:在输入编号时,判断此列中是否已经有此编号,如果有则提示。
如果没有,则完成编辑,并在下拉框中去除掉此条目。
定义验证事件
_view.dataGridView.ValidateRow += new DevExpress.XtraGrid.Views.Base.ValidateRowEventHandler(dataGridView_ValidateRow);
事件中的验证代码:
//验证 void dataGridView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) { string str = _view.dataGridView.GetRowCellValue(e.RowHandle, "PnId").ToString(); int count = _view.dataGridView.RowCount; for (int i = 0; i < count-1; i++) { if (i!=e.RowHandle&&str.Equals(_view.dataGridView.GetRowCellValue(i, "PnId").ToString())) { e.ErrorText = "编号已存在\n"; e.Valid = false; return; } } //去除已填写的编号 if (mesPnIdlst.Contains(str)) { mesPnIdlst.Remove(str); DDataGrid.InitGridListBox(_view.dataGridView.Columns["PnId"], mesPnIdlst); } }
向编号列中添加下拉框及其数据:
List<string> mesPnIdlst = new List<string>();
private void AddPnID()
{
mesPnIdlst.Clear();
foreach (string pnid in SensorGroup.sensorHas.Keys)
{
if (!SensorGroup.SensorMapHas.ContainsKey(pnid) && !mesPnIdlst.Contains(pnid))
{
mesPnIdlst.Add(pnid);
}
}
DDataGrid.InitGridListBox(_view.dataGridView.Columns["PnId"], mesPnIdlst);
}
绑定下拉框数据:
/// <summary>
/// 加入列表值
/// </summary>
/// <param name="colum">Grid列</param>
/// <param name="dataSource">值列表</param>
public static void InitGridListBox(DevExpress.XtraGrid.Columns.GridColumn colum, IList dataSource)
{
DevExpress.XtraEditors.Repository.RepositoryItemComboBox rpCmb
= new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
foreach (var item in dataSource)
{
rpCmb.Items.Add(item);
}
colum.ColumnEdit = rpCmb;
}