[英]Possible to fill SqlDataSource with SqlDataAdapter?
I currently have this code to populate ASP.NET gridview from code behind with DataTable. 我目前有这段代码,可以用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();
}
}
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?
所以我在想是否可以填充SqlDataSource而不是Datatable然后绑定到Gridview,所以我不必手动处理选择,排序等吗?
But I can't just simply do like daSearchResult.Fill(sqlDataSource1); 但是我不能只是简单地做daSearchResult.Fill(sqlDataSource1);
Is there any workaround? 有什么解决方法吗?
Kind of like what Neeraj was saying you can bind directly to a gridview with an SqlDataSource. 有点像Neeraj所说的,您可以使用SqlDataSource直接绑定到gridview。 You don't need to have all that plumbing code.
您不需要所有的管道代码。
This is how in asp.net 这是asp.net中的方式
<asp:GridView ID="grid" runat="server" DataSourceID="source"></asp:GridView>
<asp:SqlDataSource ID="source" runat="server"></asp:SqlDataSource>
This is how in C# 这就是C#中的方式
SqlDataSource source =new SqlDataSource();
GridView grid = new GridView();
grid.DataSource = s;
grid.DataBind();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.