gpt4 book ai didi

c# - 使用 boundfield 时如何更新 GridView

转载 作者:行者123 更新时间:2023-12-02 05:32:12 26 4
gpt4 key购买 nike

我有一个 GridView 并且它绑定(bind)到一个数据库。我发现在更新 GridView 和数据库中的相应表时遇到困难。

绑定(bind)到 SQLdatasource 后,我的 GridView 的 asp 代码是:

<asp:GridView ID="GridView2" runat="server" OnRowEditing="GridView2_RowEditing"
OnRowUpdating="GridView2_RowUpdating" CellPadding="4" ForeColor="#333333" OnRowCancelingEdit="GridView2_RowCancelingEdit"
OnRowDataBound="GridView2_RowDataBound" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" AutoGenerateEditButton="True" DataKeyNames="Locations">
<Columns>
<asp:BoundField DataField="Locations" HeaderText="Locations"
SortExpression="Locations" ReadOnly="true"/>
<asp:BoundField DataField="Lamp_pro4" HeaderText="Lamp_pro4"
SortExpression="Lamp_pro4" />
<asp:BoundField DataField="Lamp_pro5" HeaderText="Lamp_pro5"
SortExpression="Lamp_pro5" />
<asp:BoundField DataField="AC_Profile5" HeaderText="AC_Profile5"
SortExpression="AC_Profile5" />
</Columns>

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TouchPadConnectionString %>"
SelectCommand="SELECT * FROM [Quantity]">
</asp:SqlDataSource>

我的数据键是 Locations 并且它是只读的。

更新的.cs代码是:

protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ARCHANA-PC\\ARCHANA;Initial Catalog=TouchPad;Integrated Security=True");
string LocName = GridView2.DataKeys[e.RowIndex].Values["Locations"].ToString();
TextBox txt1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro4");
TextBox txt2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro5");
TextBox txt3 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("AC_Profile5");
string updStmt = "UPDATE Quantity set Lamp_pro4=@Lamp_pro4,Lamp_pro5=@Lamp_pro5,AC_Profile5=@AC_Profile5 where Locations=@locName";

con.Open();
SqlCommand updCmd = new SqlCommand(updStmt, con);

updCmd.Parameters.AddWithValue("@locName", LocName);
updCmd.Parameters.AddWithValue("@Lamp_pro4", txt1.Text);
updCmd.Parameters.AddWithValue("@Lamp_pro5", txt2.Text);
updCmd.Parameters.AddWithValue("@AC_Profile5", txt3.Text);
updCmd.ExecuteNonQuery();
GridView2.DataBind();

}

最佳答案

1) 你必须在处理结束时调用 GridView2.DataBind()

protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];

string LocName = GridView2.DataKeys[e.RowIndex].Values["Locations"].ToString();
TextBox txt1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro4");
TextBox txt2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro5");
TextBox txt3 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("AC_Profile5");
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE Quantity set Lamp_pro4='" + txt1.Text + "',Lamp_pro5='" + txt2.Text + "',AC_Profile5='" + txt3.Text + "' where Locations=" + LocName, con);
cmd.ExecuteNonQuery();
con.Close();

GridView2.EditIndex = -1;
//BindQuantity();
GridView2.DataBind();
}

2) 在 SqlDataSource 上定义 UpdateCommand

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:TouchPadConnectionString %>"
SelectCommand="SELECT * FROM [Quantity]"
UpdateCommand="UPDATE Quantity set Lamp_pro4 = @Lamp_pro4 ,Lamp_pro5=@Lamp_pro5,AC_Profile5=@AC_Profile5 where Locations=@Locations">
</asp:SqlDataSource>

链接:http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.sqldatasource.updatecommand.aspx

关于c# - 使用 boundfield 时如何更新 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com