Winforms Datagrid Vs DataRow BeginEdit

Hi Folks,


Scenario:-

I have a DataTable (T1) with 2 columns "id" and "name", i have bound this to a winforms datagrid (D1). The table T1 is added with a uniqueconstraint as here.

T1.Constraints.Add(new UniqueConstraint("say the Name column"));
// So this says that, the name column, can't have duplication of value.

Assume that the T1 is having 2 rows with values like this
row 1 - "1", "Bill"
row 2 - "2", "Mill"

When i run this application, the grid is displayed. i can edit the value in the datagrid. If I am editing the value of second row to "Bill" from "Mill", it is clear that, this violates uniqueconstraint. Assume I want to edit this in a way as follows, ie, first row I want to make as "Mill" and second row I want as "Bill". For doing this, first I have to modify one row and go to the next row to edit that. But the uniqueconstraint doesn't allow this swapping. One solution I can think is, I can drop the constraint till I edit and then once I swap the value, I can invoke the constraint back into place. But I want to make use of the DataRow.BeginEdit method to do this (Correct me if this BeginEdit is not meant for it).

Question:-

In MSDN, in this link for DataRow.BeginEdit,

http://msdn2.microsoft.com/en-us/library/system.data.datarow.beginedit.aspx

In this, they have given an example, it says that though the uniqueconstraint is there for a table, I can invoke the beginedit and do the task and invoke the endedit after finishing. Through this I can easily swap the values.

In this link, it is mentioned as "The BeginEdit method is called implicitly when the user changes the value of a data-bound control". Here the datagrid is databound to a table called T1 in our case. So when I edit the row it should trigger the BeginEdit and it should allow me to swap values "Bill" to "Mill" and "Mill" to "Bill".

1) "DataRow.BeginEdit", can it be used in this manner or Am I trying something wrong

Thanks in Advance,

Suki



Answer this question

Winforms Datagrid Vs DataRow BeginEdit

  • David Trowbridge

    I think when you edit a cell in a row of a DataGrid and then move to the next row, EndCurrentEdit() of a CurrencyManager is automatically called and this violates constrains.

  • Winforms Datagrid Vs DataRow BeginEdit