Wednesday, November 09, 2005

How to handle New Row in the Grid Control

Recently I was developing some grid controls where I faced a situation where I want to programtically update the cell of a newly created row on its cell changed. The status of a New Row in the datagrid is always Detached until you changed the focus from that row to some other row, when its status changed to added. If you try to change the value of the cell in the row when its status is detached, it will not work and if you try to remove that row from its source and then add it, it will give error whenever you try to change its value that the row has already been added.

So I used the following code for the detached row and it worked:

//If it is a new row that has just been added but not attached to data grid, then ends its editing.
if(dr.RowState == DataRowState.Detached )
this.BindingContext[this.grid.DataSource, this.grid.DataMember].EndCurrentEdit();

No comments: