2012-09-10 38 views
0

我正在使用GridView,並已綁定到SQL使用asp.net的前端。在button_Click之前,我確保重置了與GridView綁定的我的數據庫表的值,但它沒有反映在button_Click之後的GridView中。如何刷新GridView與數據庫綁定數據時使用asp.net c#

這裏是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(); 

回答

0

可以調用GridView的DataBind方法來刷新網格。

1

致電grdFoo.DataBind()在您的button_Click事件。

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() 
} 
+0

非常感謝你...它的作品:) –

+0

不客氣.. :) –

0

爲馬亞克所示你應該再次刷新&綁定數據,也可以再次使用更新面板,更新數據&綁定gridview

相關問題