簡體   English   中英

使用ASP.NET C#綁定數據時如何使用數據庫刷新GridView

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

我正在使用GridView並已使用asp.net在前端綁定到SQL。 在button_Click之前,我確保已重置綁定到GridView數據庫表的值,但在button_Click之后,它未反映在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>

數據源的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>

我的.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();   

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