Hi,
I am tring to add an unbounded column of edit buttons to a grid.
the assignment to the grid DataSource happends at runtime, and the rows attached there don't contain the defaultCellValue i wanted for these buttons (their buttons has no caption).
I tried to assign it manualy right after the binding:
private void InitData()
this.ultraGrid.DataSource = this.innerMultiDropDownValues.Values;
//foreach (UltraGridRow row in ultraGrid.Rows)
// row.Cells[editBtnColumnKey].Value = this.ultraGrid.DisplayLayout.Bands[0].Columns[editBtnColumnKey].DefaultCellValue;
}
But i got an exception because the Cells property for all the rows is null, although the DataSource is valid.
Can u please help me solve this issue, and also explain to me how to handle a mixture of bounded and unbounded columns?
Are you sure Cells is null? The Cells collection should never be null. Although it's certainly possible that the unbound column does not yet exist.
If you want a button in an unbound column with some text on it, the best way to do to it is to add the unbound column to the grid in the grid's InitializeLayout event. To set the text on the button, you should use the InitializeRow event and do something like this:
if (e.ReInitialize == false)
{
e.Row.Cells["my unbound button column"].Value = "The text I want on the button";
This is exactly what I did at first, but then I got the exception for the null Cell collection.
I think that my problem with this is conceptual. During life time of my editor object the user can assign a new DataSource to the grid. It doesn’t important when I add my unbounded column - whether I add it in the constructor (or alternatively ultraGrid_InitializeLayout) or after the DataSource assignment, In any case the ultraGrid_InitializeRow evant is being called twice for each row: in one of them the unbounded column exist and in the other it’s not…
maya_gispan said:This is exactly what I did at first, but then I got the exception for the null Cell collection.
Once again, are you certain that this is the exception you got? Unless you application is using multiple threads or something like that, there is no way the Cells collection of a row can ever be null, because the collection is created when you access it. I think what probably happened here is that the InitializeRow event fired a few times before the column was added. So the individual cell does not exist. But the Cells collection itself can't be null.
This is very easy to solve. You just check the band.Columns.Exists method inside of InitializeRow to make sure the column exists before you try to access the cell. If it does not exist, you just skip that code. The event will fire again when the column is added.
Hmm not sure if I am missing the point here but whenever I want to add a button to a grid via an unbound column I simply add a column as a button style using the grid's designer. Then when I am setting up the datasource for the grid I simply add an extra column to the select statment with a blank string as its value
i.e.
Select Table.*, '' as DeleteButton FROM table.
Everything works fine when I do that, provided the unbound column I created in the grid matches the blank column I am bringing out in the SQL in this case "DeleteButton".