Question

The code below at my application start-up it is called. The datasource does have 1 item. However the messagebox is never shown. Now when the user interface is drawn for the first time the gridview does indeed have one row. If i call the code again on a button press the messagebox is shown correctly. What is going on here and how can i fix it (I assume it has something to do with threading as the radGridview has not actually been updated yet)?

C# Code to Bind Grid

// Bind list to gridview
this.radGridViewFiles.BeginInvoke((MethodInvoker)(() => this.radGridViewFiles.DataSource = null));
this.radGridViewFiles.BeginInvoke((MethodInvoker)(() => this.radGridViewFiles.DataSource = MyGlobals.ListOfItemsToControl.Concat(MyGlobals.lstNewItems.Where(i => i.sItemRequestStatus == "Add").ToList()))); //

if (radGridViewFiles.Columns.Count > 0)
{
   RadMessageBox.Show(" This messagebox should show on startup but it does not - But if i call all this code again manually on a button press it does show ??? !!");

}
Was it helpful?

Solution

You are binding GridView asynchronously, that's why when you check the Column count it's still zero.

I suggest you bind it in the same thread or wait for async operation to finish.

As you requested, here is code example using EndInvoke. Although if you put it like this all in one method - you will not gain anything from async calls.

// You don't need this, so i commented it out.
// it's excessive, you are going to overwrite this variable anyway
// this.radGridViewFiles.BeginInvoke((MethodInvoker)(() =>this.radGridViewFiles.DataSource = null));

var asyncRes = this.radGridViewFiles.BeginInvoke((MethodInvoker)(() => this.radGridViewFiles.DataSource = MyGlobals.ListOfItemsToControl.Concat(MyGlobals.lstNewItems.Where(i => i.sItemRequestStatus == "Add").ToList())));

// This method blocks until previous operation is done
// It's quite pointless. In real life you should call it somewhere from another thread
// While qui thread is unblocked and displaying progress bar or something like this. 
this.radGridViewFiles.EndInvoke(asyncRes);

// at this point, binding is complete
if (radGridViewFiles.Columns.Count > 0)
{
   RadMessageBox.Show(" This messagebox should show on startup but it does not - But if i call all this code again manually on a button press it does show ??? !!");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top