【问题标题】:Edit data in gridview row using Rowupdating event in asp.net使用asp.net中的Rowupdating事件编辑gridview行中的数据
【发布时间】:2015-02-16 05:12:19
【问题描述】:

我的gridview 是基于sqldatasource 的。当我使用下拉 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 &gt; 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 事件。如何成功更新网格视图中的选定行

  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();
    }
}

错误:

参数化查询“(@id varchar(8000),@name float)update TENANTNEW set fixedrate=@n”需要参数“@id”,但未提供该参数。

【问题讨论】:

    标签: c# sql asp.net gridview


    【解决方案1】:

    你好兄弟你必须通过0而不是e.RowIndex作为DataKeys中的索引所以使用下面的代码

    int id = Convert.ToInt32(grdTenantUpdate.DataKeys[0].Value.ToString());
    

    而不是

    int id = Int32.Parse(grdTenantUpdate.DataKeys[e.RowIndex].Value.ToString());
    

    Deference between Int32.Parse and Convert.ToInt32

    【讨论】:

    • 我试过了,但没用,我认为 Karen 是正确的
    • @ricky 您也可以使用 HIddenfield 并将其值设置为 Value='&lt;%# Eval("TENANTCODE") %&gt;' 在网格视图的模板字段内的项目模板字段中,并在其 Gridview 控件的 RowUpdating 事件中找到该控件...
    【解决方案2】:

    tenantcode - 不是数字值

    【讨论】:

    • 是的,你说得对,tenantcode 是一个字符串,我该怎么做才能更正它?
    【解决方案3】:

    在 Html 方面 - DataKeyNames="tenantcode,id"

    在服务器端:

    int id;   
    Int32.TryParse(grdTenantUpdate.DataKeys[e.RowIndex].Values[1].ToString(), out id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多