C# – Both Datasource and DatasourceID are defined on GridView

asp.netcsql

I get the above error in my C#/SQL/ASP.NET app because I have a datasource defined both in the ASPX file and the ASPX.CS file. But I want the Gridview to have selectable rows. So if I comment out the ASPX.CS datasource I get the above error but if I comment out the ASPX datasource I get the Gridview output but it is not selectable. How do I code this successfully?

Best Answer

You should only be specifying one or the other. If you want to give the data object directly to the grid, set the DataSource to be that object.

Otherwise, set the DataSourceID to the ID of the datasource that you want to be binding to, and let it do it's thing.

Selectable rows shouldn't have anything to do with where the data is coming from. You can select a row by setting the SelectedRow property. Making them selectable in the UI is a whole different topic.