我目前有这段代码可以从后面的代码中用 DataTable 填充 ASP.NET gridview。
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();
}
}
但我会失去网格的选择、排序、分页功能。所以我在想如果我可以填充到 SqlDataSource 而不是 Datatable 然后绑定到 Gridview,我就不必手动处理选择、排序等?
但我不能只是简单地做 daSearchResult.Fill(sqlDataSource1);
有什么解决方法吗?