简体   繁体   English

使用ASP.NET C#绑定数据时如何使用数据库刷新GridView

[英]How to refresh GridView with database when it is bound data using asp.net c#

I am using GridView and have bound to SQL in the frontend using asp.net. 我正在使用GridView并已使用asp.net在前端绑定到SQL。 Before button_Click I make sure that the values of the my database table which is bound to GridView is reset, but it is not reflecting in the GridView after button_Click. 在button_Click之前,我确保已重置绑定到GridView数据库表的值,但在button_Click之后,它未反映在GridView

Here is my asp code for GridView : 这是我的GridView ASP代码:

<Columns>
    <asp:BoundField DataField="Profiles" HeaderText="Profiles" ReadOnly="true" SortExpression="Profiles" />
    <asp:BoundField DataField="Location_Profile" HeaderText="Location_Profile" SortExpression="Location_Profile" />
</Columns>

The datasource asp code is: 数据源的ASP代码为:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TouchPadConnectionString %>"
        InsertCommand="INSERT INTO [Loc_Pro_Grid] ([Profiles], [Location_Profile]) VALUES (@Profiles, @Location_Profile)"
        SelectCommand="SELECT * FROM [Loc_Pro_Grid]" 
        UpdateCommand="UPDATE Loc_Pro_Grid SET Location_Profile = @Location_Profile WHERE (Profiles = @Profiles)">
    <InsertParameters>
        <asp:Parameter Name="Profiles" Type="String" />
        <asp:Parameter Name="Location_Profile" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Location_Profile" />
        <asp:Parameter Name="Profiles" />
    </UpdateParameters>
</asp:SqlDataSource>

My .cs code is: 我的.cs代码是:

 protected void Save_Click(object sender, EventArgs e)
    {

// I am doing some operations here then I am resetting the table as below

com = new SqlCommand("UPDATE Loc_Pro_Grid SET Location_Profile = 0 WHERE Profiles='" + ListBox1.Items[i].ToString() + "' ", con);
}
                    con.Open();
                    com.ExecuteNonQuery();
                    con.Close();   

call grdFoo.DataBind() on your button_Click Event. button_Click事件上调用grdFoo.DataBind()

protected void Save_Click(object sender, EventArgs e)
{
     // I am doing some operations here then I am resetting the table as below
      com = new SqlCommand("UPDATE Loc_Pro_Grid SET Location_Profile = 0 WHERE Profiles='" +        ListBox1.Items[i].ToString() + "' ", con);
      con.Open();
      com.ExecuteNonQuery();
      con.Close();   
      // Call DataGrid's bind method here..for example
      grdFoo.DataBind()
}

您可以调用GridView的DataBind方法来刷新网格。

Mayak所示,您应该再次刷新和绑定数据,或者可以再次使用“更新”面板,“更新数据并绑定gridview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM