我有 gridview 提取我想要的数据,但是我添加了一个按钮,当单击该按钮时,它会将 lastLeak 更新为我设置的日期...我知道查询在 SQL 中有效,所以不确定我遗漏了什么让它工作...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="lastLeakCheck" Width="850px">
<Columns>
<asp:BoundField DataField="customerName" HeaderText="customer Name" SortExpression="customerName" />
<asp:BoundField DataField="acctNum" HeaderText="acct Num" SortExpression="acctNum" />
<asp:BoundField DataField="phoneNum" HeaderText="phone Num" SortExpression="phoneNum" />
<asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
<asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
<asp:BoundField DataField="lastLeak" HeaderText="Last Leak" SortExpression="lastLeak" />
<asp:ButtonField ButtonType="Button" CommandName="Update" HeaderText="Update Date" ShowHeader="True" Text="Completed" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="lastLeakCheck" runat="server" ConnectionString="Data Source=server;Initial Catalog=propane;User ID=user;Password=pass;Integrated Security=True" SelectCommand="SELECT customerName, acctNum, phoneNum, city, address, lastLeak from custInfo WHERE lastLeak <= CONVERT(datetime, '4-6-2012' ) ORDER BY CONVERT(DATETIME, lastLeak) ASC" UpdateCommand = "UPDATE custInfo SET lastLeak='4/5/2017'WHERE customerName='@customerName';"></asp:SqlDataSource>
这是我的按钮点击:
cn = new SqlConnection(@"Data Source=server;Initial Catalog=propane;User ID=id;Password=pass;Integrated Security=True");
cmd = new SqlCommand("UPDATE custInfo SET lastLeak='4/5/2017' WHERE customerName='@customerName'", cn);
cn.Open();
cmd.ExecuteNonQuery();
最佳答案
这是我创建网页和数据表并对其进行测试的完整解决方案。 Uploaded如果需要,您可以下载到我的网站之一
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="850px" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="customerName" HeaderText="customerName" SortExpression="customerName" />
<asp:BoundField DataField="acctNum" HeaderText="acctNum" SortExpression="acctNum" />
<asp:BoundField DataField="phoneNum" HeaderText="phoneNum" SortExpression="phoneNum" />
<asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
<asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
<asp:BoundField DataField="lastLeak" HeaderText="lastLeak" SortExpression="lastLeak" />
<asp:ButtonField ButtonType="Button" CommandName="UpdateLeak" HeaderText="Update Date" ShowHeader="True" Text="Completed" />
</Columns>
</asp:GridView>
页面加载
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindLeaks();
}
}
将泄漏绑定(bind)到网格
private void BindLeaks()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LeakConnection"].ConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT CustomerName, AcctNum, PhoneNum, City, Address, LastLeak from Leak WHERE lastLeak >= CONVERT(datetime, '4-6-2012') ORDER BY LastLeak ASC", conn);
DataSet dsLeaks = new DataSet();
conn.Open();
da.Fill(dsLeaks);
conn.Close();
GridView1.DataSource = dsLeaks;
GridView1.DataBind();
}
}
更新数据的行命令事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UpdateLeak")
{
if (e.CommandArgument != null)
{
int RowID = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[RowID];
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LeakConnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("UPDATE Leak SET lastLeak='4/5/2017' WHERE CustomerName=@customerName", conn);
if (e.CommandArgument != null)
cmd.Parameters.AddWithValue("@customerName", row.Cells[0].Text.Trim());
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
BindLeaks();
}
}
}
}
关于c# - Gridview 更新问题 - .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238735/