Binding GridView to SqlDataSource programmatically

只谈情不闲聊 提交于 2019-12-11 11:32:58

问题


I'm trying to replicate the binding in my markup with something programmatic. It seems I am forced to use a dataset to bind to rather than a SqlDataSource as shown in my markup. Furthermore, when using a dataset, I suddenly lose paging, sorting, etc., and get errors that I need to handle them manually. They were handled automatically before??

So here is my markup:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" 
    CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" 
    GridLines="None" AllowPaging="True" AllowSorting="True" Width="520px">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
    </Columns>
</asp:GridView>   
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>" 
    SelectCommand="CustOrdersOrders" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
        <asp:ControlParameter ControlID="txtCustId" DefaultValue="AROUT" 
            Name="CustomerID" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

I replaced it with something like this:

SqlDataAdapter adapter = new SqlDataAdapter();                
conn.Open();
using (SqlConnection conn = new SqlConnection(connStr))
{
    SqlDataAdapter adapter = new SqlDataAdapter();

    conn.Open();

    using (SqlCommand cmd = new SqlCommand("CustOrdersOrders", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("CustomerID", "AROUT");
        cmd.ExecuteScalar();
        adapter.SelectCommand = cmd;
    }

    GridView1.DataSource = adapter;                
    GridView1.DataBind();
}

This ofcourse raises an error that that the data source is not of the correct type, yet this is exactly what the markup does. The only thing I can think of is that is uses DataSource instead of DataSourceID. So how do I replicate this markup and get all the sorting and paging automatically? If I have to use a dataset, why does the markup not require one? But, even with the dataset, it doesn't seem to handle the paging and sorting automatically like I said.

Thanks! Levi


回答1:


You can not set the DataAdapter as the DataSource property value.SqlDataAdapter provides the communication between the Dataset and the SQL database. You must be using the data adapter to fill a DataTable /DataSet and use that as the DataSource.

Your code had some other problems

1) You are trying to open a connection before declaring it.

2) Sql Data adapter object is created twice with same name.

The below code will work

using (SqlConnection conn = new SqlConnection(connStr))
{
    SqlDataAdapter adapter = new SqlDataAdapter();    
    conn.Open();
    DataTable dt = new DataTable();
    using (SqlCommand cmd = new SqlCommand("CustOrdersOrders", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("CustomerID", "AROUT");           
        adapter.SelectCommand = cmd;
        adapter.Fill(dt);
    }

    GridView1.DataSource = dt;                
    GridView1.DataBind();
}

http://www.dotnetperls.com/sqldataadapter

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx



来源:https://stackoverflow.com/questions/8934850/binding-gridview-to-sqldatasource-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!