问题描述
我正在使用我的c#windows aplication,这是第一次使用tdbgrid(component1)现在我想阻止用户从数据库验证后输入重复值,下面是我使用它的代码(BeforeColUpdate )事件:
i am working with my c# windows aplication, and this is first time to use tdbgrid(component1) now i want to prevent users from input duplicated value after validate it from database , Below is the code wich iam use it in (BeforeColUpdate)Event:
bool ExitValue = false;
private void C1TrueDBGrid_BeforeColUpdate(object sender, C1.Win.C1TrueDBGrid.BeforeColUpdateEventArgs e)
{
if (e.Column.Name == "Groups Code")
{
for (int currentRow = 0; currentRow < this.C1TrueDBGrid.Rows.Count - 1;currentRow++)
{
string rowToCompare = this.C1TrueDBGrid.Splits[0].DisplayColumns[C1TrueDBGrid.Col].DataColumn.CellValue(currentRow).ToString();
for (int otherRow = currentRow+1 ; otherRow < this.C1TrueDBGrid.Rows.Count; otherRow++)
{
bool DuplicatedRow = true;
string Row = this.C1TrueDBGrid.Splits[0].DisplayColumns[C1TrueDBGrid.Col].DataColumn.CellValue(otherRow).ToString();
if (Row!=rowToCompare)
{
ExitValue = false;
break;
}
if (DuplicatedRow)
{
C1TrueDBGrid.Splits[0].DisplayColumns[tgdGroupsUsers.Col].DataColumn.Value = DBNull.Value;
MessageBox.Show("Sorry: but this item(s) is already Exists ", "Error Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
ExitValue = true;
e.Cancel = true;
}
}
}
}
else
{
//Cleare Feilds
C1TrueDBGrid.Splits[0].DisplayColumns[C1TrueDBGrid.Col].DataColumn.Value = null;
e.Cancel = true;
}
}
}
如果不是duplictad以下是我使用它的代码(AfterColUpdate)事件:
if not duplictad Below is the code wich iam use it in (AfterColUpdate)Event:
private void C1TrueDBGrid_AfterColUpdate(object sender, C1.Win.C1TrueDBGrid.ColEventArgs e)
{
if (!ExitValue)
{
int indexRow = this.C1TrueDBGrid.RowBookmark(this.C1TrueDBGrid.Row);
this.C1TrueDBGrid[indexRow, 0] = CSystemUsers.GroupsCode;
this.C1TrueDBGrid[indexRow,0] = CSystemUsers.EngName;
}
}
推荐答案
private void c1TrueDBGrid1_BeforeColUpdate(object sender, C1.Win.C1TrueDBGrid.BeforeColUpdateEventArgs e)
{
if (e.ColIndex == 1)
{
for (int i = 0; i < c1TrueDBGrid1.RowCount; i++)
{
if (c1TrueDBGrid1.Editor.Text == c1TrueDBGrid1[i, e.ColIndex].ToString())
{
MessageBox.Show("Sorry: but this item(s) already Exists", "Error Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
}
}
}
}
链接:http://our.componentone.com/groups/topic/how-do-i-prevent-duplicate-entries-in-c1truedbgrid/
希望这会有所帮助将来的其他人:
link: http://our.componentone.com/groups/topic/how-do-i-prevent-duplicate-entries-in-c1truedbgrid/
hope this helps someone else in future:
这篇关于如何防止C1TrueDBGrid中的重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!