简体   繁体   中英

How to get cell value in RowUpdating Event GridView?

I want to obtain the new value of a cell in the gridview rowUpdating event:

 roles.RoleName = gvRoles.Rows[e.RowIndex].Cells[GetColumnIndexByName(row, "RoleName")].Text;

But the value is empty.

I have checked e.NewValues which is an ordered Dictionary. It contains the new changed value. How do I retrieve the new values for update?

aspx design is:

<asp:GridView ID="gvRoles" DataKeyNames="RoleId" runat="server" 
    AutoGenerateColumns="False" GridLines="Vertical" CssClass="table 
    table-striped table-bordered" AutoGenerateEditButton="True" 
    OnRowCancelingEdit="gvRoles_RowCancelingEdit" OnRowUpdating="gvRoles_RowUpdating" 
    OnRowEditing="gvRoles_RowEditing">
    <Columns>
        <asp:BoundField DataField="RoleId" HeaderText="RoleId" ReadOnly="True" />
        <asp:BoundField DataField="RoleName" HeaderText="RoleName" ReadOnly="false" />
        <asp:BoundField DataField="Role_Description"  HeaderText="Role Description"  ReadOnly="false" />
        <asp:TemplateField HeaderText="RoleStatus">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlStatus" runat="server" SelectedValue='<%# Bind("Role_Status") %>' >
                <asp:ListItem>True</asp:ListItem>
                <asp:ListItem>False</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblRoleStatus" runat="server" 
                    Text='<%# Bind("Role_Status") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The easiest way which i found out is to discovered e.NewValues . As I mentioned above it is an ordered dictionary and only i need to manage it.

I need to retrieve new values from this dictionary, which is done i this way.

if (!string.IsNullOrEmpty(e.NewValues["RoleName"].ToString()))
            {
                roles.RoleName = e.NewValues["RoleName"].ToString();
            }

if you have template field it also work for it;

  if (!string.IsNullOrEmpty(e.NewValues["Role_Status"].ToString()))
        {
            roles.Role_Status = Convert.ToBoolean(e.NewValues["Role_Status"].ToString());
        }

This is the easiest thing I ever discovered. before that i want just using Find control and casting and then retrieving all lot code.

There is also e.OldValues ordered dictionary. Every one can use it to compare the new value with old ones. If values are same they could notify user to change the value(give new cell value).

GridViewRow row = (GridViewRow)gvRoles.Rows[e.RowIndex];

TextBox textRName = (TextBox)row.Cells[1].Controls[0];

string rname=textRname.Text;

Try This:

protected void gvRoles_RowUpdating(object sender, GridViewUpdateEventArgs e)
{    
  GridViewRow row = gvRoles.Rows[e.RowIndex];
  TextBox txtBox= (TextBox)(row.Cells[1].Controls[0]);
  if(txtBox!=null)
  {
   String str = txtBox.Text;
  }
}

After searching long and hard I found a great article that solved my issue. Take a look at the page load if you are binding on post back then the values get updated before you are able to access them.

Follow this link for more details --> https://taditdash.wordpress.com/2014/06/30/why-gridview-rowupdating-event-is-not-giving-the-updated-values/

The return data type ICollection is a bit tricky. Would have preferred a different data type than a string array but this was simplest direct conversion.

        // grabs column headers
        String[] keyStrings = new string[e.NewValues.Count];
        System.Collections.ICollection keys = e.NewValues.Keys;
        keys.CopyTo(keyStrings, 0);

        // grabs edit row values
        String[] valuesStrings = new string[e.NewValues.Count];
        System.Collections.ICollection values = e.NewValues.Values;
        values.CopyTo(valuesStrings, 0);

The end result here has two string arrays values and keys which can then be indexed, where the column header has the same index value as the cell value of the edited row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM