我正在使用我的c#windows应用程序,这是第一次使用tdbgrid(component1)现在我想阻止用户从数据库验证后输入重复值, 下面是我在其中使用的代码(BeforeColUpdate)事件:
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)事件:
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;
}
}
提前感谢...
答案 0 :(得分:0)
componentone回答我的问题:
public static class Program
{
static Program()
{
// Initialize logging
log4net.Config.XmlConfigurator.Configure();
}
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
...
}
链接: http://our.componentone.com/groups/topic/how-do-i-prevent-duplicate-entries-in-c1truedbgrid/ 希望这对未来的其他人有所帮助: