dev.e.loper
dev.e.loper

Reputation: 36044

Asp.net GridView - how to access datasource before data is bound?

I'm trying to access datasource before I bind it to grid. How do I do that? My guess I should look in one of the events like Grid.DataBinding

Upvotes: 2

Views: 1770

Answers (2)

StevenMcD
StevenMcD

Reputation: 17482

One way would be to not define the datasource on the grid itself. In the page_load, create and populate your datasource and then dynamically bind it to your datagrid. That way you would be able to access it.

Hope it helps!

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

Yes, the GridView.DataBinding event will be raised prior to the control being bound. You can access the DataSource control in that event and make modifications if you so desire (for example, modifying parameters).

protected void Grid_DataBinding(object sender, EventArgs e)
{
  mySqlDataSource.SelectCommand = "Select * from Stars";
}

Upvotes: 1

Related Questions