Reputation: 29
I am trying to update my GridView to auto select a new row that is added after the user fills out the fields and presses add. However, I am having no luck when trying to set the FocusedRowHandle property. The FocusedRowHandle value stays at -2147483648 even after I try to assign a value to it as shown in the following code:
private void gvExample_RowUpdated(object sender, RowObjectEventArgs e)
{
try
{
ExampleMethodToUpdateTableAdapter();
GridView view = sender as GridView;
Object obj;
if (e.RowHandle != GridControl.NewItemRowHandle)
obj = view.GetRow(view.FocusedRowHandle);
else
{
dsExample.dtExample.DefaultView.Sort = "ID desc";
DataTable dtSorted = dsExample.dtExample.DefaultView.ToTable(true);
int lastID = Convert.ToInt32(dtSorted.Rows[0]["ID"]);
gvExample.FocusedRowHandle = lastID;
}
}
catch (Exception ex)
{
}
}
When stepping into the else statement the aim was to get the latest ID from the DataTable now it has been updated (since the value at e.RowHandle is -2147483648 because its a new row) and set the Focused Row to this ID.
I have tried to have a look online for this but can't seem to find a solution so I thought I'd have a go at posting on here. Apologies if I've missed any info out.
Upvotes: 1
Views: 1750
Reputation: 319
If i understand correct you want to focus the new row (which is the last row):
gridView.FocusedRowHandle = gridView.RowCount - 1;
If not, you have to search the id inside the gridvw with for i 0-> gridView.RowCount - 1 and then when you will find the Id to focus the row with the i value.
for (int i = 0; i <= gridView.RowCount - 1; i++)
{
if (gridView.GetRowCellValue(i, gridView.columns(column_number)))
{
gridView.FocusedRowHandle = i;
}
}
Upvotes: 0