I currently have this code to populate ASP.NET gridview from code behind with DataTable.
protected void bindGridView()
{
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
SqlCommand cmd = sqlConn.CreateCommand();
cmd.CommandText = "SELECT id AS 'Member ID', name AS Name, age AS Age, sympton AS Sympton, phone AS Phone, nirc AS NIRC, address AS Address FROM tbl_customer_profile WHERE id = @id";
cmd.Parameters.AddWithValue("@id", txtSearchID.Text);
DataTable dtSearchResult = new DataTable();
SqlDataAdapter daSearchResult = new SqlDataAdapter();
try
{
sqlConn.Open();
daSearchResult.SelectCommand = cmd;
daSearchResult.Fill(dtSearchResult);
gridSearchResult.DataSource = dtSearchResult;
gridSearchResult.DataBind();
}
catch (SqlException ex)
{
lblStatus.Text = ex.Message;
}
finally
{
sqlConn.Close();
}
}
But I would lose the Grid's Selection, Sorting, Paging functions. So I was thinking if I could fill to SqlDataSource instead of Datatable and then bind to Gridview, I wouldn't have to handle the selection, sorting etc manually?
But I can't just simply do like daSearchResult.Fill(sqlDataSource1);
Is there any workaround?