绑定Gridview与Clause不起作用的位置

时间:2016-11-18 11:59:10

标签: c# sql gridview userid

我有一个填充了产品信息的gridview。 SQL表如下所示:

ID || ||用户ID ||名称价格||

我希望产品的ID与Users(User_ID)的id匹配。 (我有一个带有用户信息的SQL表。)

我有这个代码,但gridview没有显示。它只是空的......

protected void BindGrid()

        { string constr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(constr))
            {

                using (SqlCommand cmd = new SqlCommand())
                { //Land ergänzen
                    cmd.Connection = conn;
                    cmd.CommandText = "SELECT * From Product WHERE UserID=@UserID";
                    cmd.Parameters.Add("@UserID", SessionHelper.User.ID);
                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        ad.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }


aspx:

<asp:GridView ID="GridView1" CssClass="gridview" Width="100%" GridLines="None" OnRowDataBound="RowDataBound" SelectedRowStyle-BackColor="#a8c066" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="True" Visible="false" />
        <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID"  />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />     
    </Columns>

0 个答案:

没有答案