Have you tried using SqlDataAdapter to populate a DataSet / DataTable with your SQL results? Then use this DataTable as the data source for the GridView. The main framework for populating your DataTable:
public DataTable GetDataTable(String connectionString, String query) { DataTable dataTable = new DataTable(); try { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command)) { dataAdapter.Fill(dataTable); } } } } catch { } return dataTable; }
And then you can use this DataTable as a GridView data source:
String connectionString = "Data Source=<datasource>;Initial Catalog=<catalog>;User Id=<userID>;Password=<password>;"; String query = "SELECT * FROM TABLE_NAME WHERE ID=BLAH"; GridView1.DataSource = GetDataTable(connectionString, query); GridView1.DataSourceID = null; GridView1.Visible = true; GridView1.AllowPaging= true; GridView1.DataBind();
Hope this helps.
source share