我的gridview基于sqldatasource。当我使用dropdown selectedIndexChanged事件过滤数据时,gridview将根据搜索的内容加载数据。我需要对点击的行进行更新操作。
<div class="GridviewPanelBody">
<asp:GridView ID="grdTenantUpdate" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource1"
ForeColor="Black" GridLines="Vertical" AutoGenerateEditButton="True" DataKeyNames="tenantcode"
Font-Size="Small" width="100%" OnRowDataBound="grdTenantUpdate_RowDataBound" OnRowCancelingEdit="grdTenantUpdate_RowCancelingEdit" OnRowEditing="grdTenantUpdate_RowEditing" OnRowUpdating="grdTenantUpdate_RowUpdating" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="tenantcode" HeaderText="Retail Partner Code" SortExpression="tenantcode" ReadOnly="true" />
<asp:BoundField DataField="name" HeaderText="Retail Partner" SortExpression="name" ReadOnly="true" />
<asp:BoundField DataField="fixedrate" HeaderText="Fixed Rate" SortExpression="fixedrate" /> <%--DataFormatString="{0:#,##0.00;(#,##0.00);0}"--%>
<asp:BoundField DataField="percentrate" HeaderText="Percent Rate" SortExpression="percentrate" />
<asp:BoundField DataField="percentage" HeaderText="Percentage %" SortExpression="percentage" />
<asp:BoundField DataField="locationd" HeaderText="Location" SortExpression="locationd" ReadOnly="true" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#90FF90" Font-Bold="True" ForeColor="Black" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT a.tenantcode, a.name, a.fixedrate, a.percentrate, a.percentage, b.locationd, c.status FROM TENANTNEW AS a INNER JOIN LOCATION AS b ON a.LOCATION = b.location INNER JOIN TENANT AS c ON a.tenantcode = c.tenantcode WHERE (c.status > 1) ORDER BY b.location, a.name"
UpdateCommand="UPDATE [TENANTNEW] set fixedrate = @fixedrate, percentrate = @percentrate, percentage = @percentage WHERE tenantcode = @tenantcode" OnSelected="SqlDataSource1_Selected" >
<UpdateParameters>
<asp:Parameter Name="fixedrate" />
<asp:Parameter Name="percentrate" />
<asp:Parameter Name="percentage" />
<asp:Parameter Name="tenantcode" />
</UpdateParameters>
</asp:SqlDataSource>
代码背后。我在gridview中使用了以下事件,唯一的问题是rowupdating事件。如何成功更新gridview中的选定行
private void BindData()
{
SqlDataSource1.SelectCommand = Session["GridviewData"].ToString();
}
protected void grdTenantUpdate_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
grdTenantUpdate.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
protected void grdTenantUpdate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
grdTenantUpdate.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
protected void grdTenantUpdate_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)grdTenantUpdate.Rows[e.RowIndex];
Label tcode = (Label)row.FindControl("tenantcode");
TextBox tname = (TextBox)row.FindControl("fixedrate");
// TextBox tques = (TextBox)row.FindControl("que");
SqlCommand cmd = new SqlCommand("update TENANTNEW set fixedrate=@name where id = @id", con);
cmd.Parameters.Add("@id", SqlDbType.VarChar ).Value = tcode;
cmd.Parameters.Add("@name", SqlDbType.Float).Value = tname;
// cmd.Parameters.Add("@ques", SqlDbType.VarChar, 40).Value = tques.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
grdTenantUpdate.EditIndex = -1;
BindData();
}
}
ERROR:
参数化查询'(@ id varchar(8000),@ name float)update TENANTNEW set fixedrate = @ n'需要参数'@id',这是未提供的。
答案 0 :(得分:0)
你好兄弟,你必须在0
中传递e.RowIndex
而不是DataKeys
作为索引,所以请使用下面的代码
int id = Convert.ToInt32(grdTenantUpdate.DataKeys[0].Value.ToString());
而不是
int id = Int32.Parse(grdTenantUpdate.DataKeys[e.RowIndex].Value.ToString());
答案 1 :(得分:0)
tenantcode - 不是数字值
答案 2 :(得分:0)
在Html方面 - DataKeyNames =“tenantcode,id”
在服务器端:
int id;
Int32.TryParse(grdTenantUpdate.DataKeys[e.RowIndex].Values[1].ToString(), out id);