can't modify DataGridView in OnLoad() event

S

sklett

I've got a strange situation here. I have a databound DataGridView that
also has un-bound columns. When the view loads, I want to update the values
of the unbound columns. If I attempt to modify the unbound column's cells
in the OnLoad() handler the results are very strange. If I add a button to
my view and update the unbound cells in the butt's handler it works fine.

Here is an example of the code I'm trying to use (it's test code):
protected override void OnLoad(EventArgs e)
{
BuildOrderCollection orders = new BuildOrderCollection();
if(orders.LoadAll())
{
// Bind the first build order to the datagridview
BuildOrder order = orders[0] as BuildOrder;
buildOrderItemsBindingSource.DataSource =
order.BuildOrderItemCollectionByBuildOrderID;

// loop through the rows in the grid and try
// to change some cell values. Cells 0, 2 and 3 are NOT
// databound, only cell 1 is databound. If I attempt to
// modify the value of Cell 1 it will work, but if I try
// to change the row color or value of other cells it has
// no effect.
foreach(DataGridViewRow row in dataGridView_SNs.Rows)
{
row.DefaultCellStyle.BackColor = Color.GreenYellow; // has no
effect
row.Cells[1].Value = "I was changed in OnLoad!"; //
works
row.Cells[2].Value = "I was changed in OnLoad!"; // has
no effect
}
}

//_presenter.OnViewReady();
}


Here is the code I've added to a button click handler:
private void pictureBox_RefreshStatus_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView_SNs.Rows)
{
row.DefaultCellStyle.BackColor = Color.GreenYellow;
row.Cells[2].Value = "I was changed in OnLoad!";
}

//_presenter.RefreshUnitStatus();
}


That code does EXACTLY what it should.

So my question is why won't this work in the same method where I'm setting
up the binding? Is it that the DataGridView isn't done "binding" yet so
it's overwriting my changes? It wouldn't be that because there is only one
databound column.

I don't get it, totally lost on this one. Any help would be greatly
appreciated.

Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top