Hi!
I am adding rows to datasourse of the grid and want to get newly added rows (to select or mark them). How to do this?
I am adding rows like this:
//copy old row to new row with a different name
DataRow newDataRow = newDataTable.NewRow(); foreach (DataColumn dataColumn in newDataTable.Columns) //init from input { newDataRow[dataColumn.ColumnName] = oldRow[dataColumn.ColumnName]; } newDataRow["Name"] = newName; newDataTable.Rows.Add(newDataRow);
ultraGrid1.Rows[newDataRow].Selected = true; //this is not compiling
I'm not sure if any notification will be given by the grid to tell you that a row has been picked up from the DataSource (aside from InitializeRow). You could probably check the InitializeRow event's e.Row property to see if it's the same row:
if(e.Row.ListObject == this.lastDataRow)
// Do stuff
Another option is to call this.ultraGrid1.Rows.GetRowWithListIndex(indexOfDataRow);
-Matt
I have no this.lastDataRow and I don't need to go to InitializeRow. All I want to do - insert row in the grid and select it. If it isnot possible to receive inserted row from dataSource tell if it is possible to insert row directly into the grid or find row in ultragrid by key or something
My "this.lastDataRow" was just an implication that you could store the row yourself and then check it in InitializeRow. Did you try my other suggestion about using the GetRowWithListIndex?
You could also directly add it to the grid:
UltraGridRow newRow = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
If for whatever reason you can't use these approaches, you could loop through the rows in the grid until you found your row, but I'd prefer one of the two approaches listed here.
My lastDataRow is of type DataRow, not UltraGridRow - so I can't compare it in RowInitialize. Also I need to select only one row in one place - not all the rows that can be added to my grid. I tried GetRowWithListIndex but also couldn't get anything from it because I have DataRow.
this.ultraGrid1.DisplayLayout.Bands[0].AddNew(); gives me an error if Bands[0] is not active and I donno how to make it active before adding row. Besides Bands[0] can be empty before adding row and in this case AddNew() again give me an errorof unknown context
The new row will always be added as the last row in the grid. So why not just do something like this:
ultraGrid1.Rows[ultraGrid1.Rows.Count-1].Selected = true;
I have Hierarchical multiband grid. So ultraGrid1.Rows.Count always gives me number of rows in the top-level band. Though I can see that inserted row is always the last row in the band there it was inserted. I can't access band with the new row because I am inserting in the dataTable by name like this:
dataTable = this._HierarchicalDataSet.Tables["Type" + ultraGrid1.Selected.Rows[0].Cells["Level"].Value.ToString()]
DataRow newDataRow = dataTable.NewRow();
...//init row
dataTable.Rows.Add(newDataRow);
so I don't now the index of this band to access it.
Well, if you are adding a child row, then there's no easy way to find it in the grid unless you know the whole hierarchy. You would have to find the parent row that the row belongs to and then look at the last child of that parent.
If you want to do this by brute force, you could loop through all the rows in the grid recursively and find a row where the ListObject of the row matches the row you just added.