Hello,
Is there way to obtain a UltraGridRow from a DataRow? Please advise.
Adrian
Hi Adrian,
If you know the index of the row you can use the GetRowWithListIndex method on the Rows collection.
Otherwise, you will need to loop through the rows in the grid and examine the ListObject property of the row and compare it to the object you are looking for. If you are using a DataSet, keep in mind that the ListObject will return a DataRowView, not a DataRow - so be sure to get the Row propety from the DataRowView before you try to compare.
Question 1: In my case, the grid is bound to the DataView defined on a DataTable. At any point of time, one may see a subset of all records on the grid depending on the filter clause on the DataView. If the GridRow is not visible at the time of doing the GetRowWithListIndex, what would it return me?
In other words, does the GetRowWithListIndex work only if the GridRow is visible to the user?
Question 2:In response to a previous email from Adrian where he used this construct
UltraGridRow gridRow = UltraGrid1.DisplayLayout.Rows.GetRowWithListIndex(table.Rows.IndexOf(DataRow));
you wrote
Mike Saltzman"] That's good. But it's a bit inefficient, because you are now doing two linear walks instead of one. Once walk through the data rows with the call to IndexOf, and a second one through the grid's Rows collection for GetRowWithListIndex.
I see why this is not as efficient as your original suggestion. But when I tried to do it differently but I could not find any way to go from the DataRow to its index. I thought there would be a Index property on the DataRow that I could use. Any suggestions?
Thanks!
vrn said:Question 1: In my case, the grid is bound to the DataView defined on a DataTable. At any point of time, one may see a subset of all records on the grid depending on the filter clause on the DataView. If the GridRow is not visible at the time of doing the GetRowWithListIndex, what would it return me? In other words, does the GetRowWithListIndex work only if the GridRow is visible to the user?
I'm not sure the visibility of the grid would make any difference. Do you have some reason to think that it does? It's possible that the method will fail to find the row if the rows have not been created, yet. And that might be the case if the grid has never painted. But I would think calling GetRowWithListIndex would force the creation of the rows at that point, just as accessing the indexer would do.
vrn said:Question 2:In response to a previous email from Adrian where he used this construct UltraGridRow gridRow = UltraGrid1.DisplayLayout.Rows.GetRowWithListIndex(table.Rows.IndexOf(DataRow)); you wrote Mike Saltzman"] That's good. But it's a bit inefficient, because you are now doing two linear walks instead of one. Once walk through the data rows with the call to IndexOf, and a second one through the grid's Rows collection for GetRowWithListIndex. I see why this is not as efficient as your original suggestion. But when I tried to do it differently but I could not find any way to go from the DataRow to its index. I thought there would be a Index property on the DataRow that I could use. Any suggestions?You probably have to do it this way if you are using a filtered DataView so that you get the correct index of the DataRow. The alternative would be to walk through the grid rows and compare the grid row's ListObject with the DataRow you are looking for. This would be more code and might look like it's doing more work, but it's actually less, since it's only walking over the rows once.
Mike Saltzman"] That's good. But it's a bit inefficient, because you are now doing two linear walks instead of one. Once walk through the data rows with the call to IndexOf, and a second one through the grid's Rows collection for GetRowWithListIndex. I see why this is not as efficient as your original suggestion. But when I tried to do it differently but I could not find any way to go from the DataRow to its index. I thought there would be a Index property on the DataRow that I could use. Any suggestions?
You probably have to do it this way if you are using a filtered DataView so that you get the correct index of the DataRow.
The alternative would be to walk through the grid rows and compare the grid row's ListObject with the DataRow you are looking for. This would be more code and might look like it's doing more work, but it's actually less, since it's only walking over the rows once.
Why GetRowWithListIndex returns null when the datarow state is Added? Is there any other way to get the underlying datarow?
private void btnUnassign_Click( object sender, EventArgs e ) { for( int i = companyDepositBO.CompanyDepositDetail.Rows.Count - 1; i >= 0; i-- ) { WebRef.CompanyDepositBO.CompanyDepositDetailRow row = companyDepositBO.CompanyDepositDetail[ i ]; if( row.RowState == System.Data.DataRowState.Deleted ) continue;
// This returns null if the DataRow state is Added. ultraWinGrid.UltraGridRow gRow = assignedReceiptsGrid.DisplayLayout.Rows.GetRowWithListIndex( companyDepositBO.CompanyDepositDetail.Rows.IndexOf( row ), true );
// "UnAssign" is a check type unbound column. This will fail when gRow is null. if( (bool) gRow.Cells[ "UnAssign" ].Value ) { // Do something... } } }
The AddNew row is always the last one in the collection. You could check to see if the row is an AddNew row and if so use:
assignedReceiptsGrid.DisplayLayout.Rows.Rows[assignedReceiptsGrid.DisplayLayout.Rows.Count - 1]
Thank you Mike. The problem is not with the AddNew row. I add some rows to a typed dataset by code using companyDepositBO.CompanyDepositDetail.AddCompanyDepositDetailRow( newRow ), so all they have System.Data.DataRowState.Added state. The dataset is bound to the assignedReceiptsGrid grid, then when I loop through the dataset (begining with the last row in the collection) to GetRowWithListIndex using one of those added rows (every row in the "for") I get a null UltraGridRow. I'm sure the row exists in the dataset and that it is shown in the grid. Is this the normal behavior? I'm wondering if there are another way to get the underlying UltraGridRow (in my first post I said datarow, my mistake)? I need to have acces to an unbound column (gRow.Cells[ "UnAssign" ]) in the grid for every row in the dataset.
BTW... using GetRowWithListIndex is not very efficient. This method loops through the grid's Rows collection.
If you just want to populate an unbound column, it would be more efficient (not to mention easier) to just use the InitializeRow event of the grid.
Hi,
Oh, in that case, the problem is probably that the grid hasn't yet responded to the notification from the data source. So if you add a row to your data source and then immediately, on the next line of code, try to get the grid row, it may not work, because the grid responds to DataSource notification asynchronously. Since your code is working synchronously, the row doesn't exist in the grid, yet.
This is easy to fix. The grid will update itself the next time it paints. So before you call GetRowWithListIndex, you can force the grid to paint by calling grid.Update. This should work, unless you are using BeginUpdate or the grid is not visible.