[英]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.