views:

124

answers:

1

I am trying to update a database using the GridView edit, update CommandField. I have two editable fields which are displayed as text boxes when in edit mode. When clicking submit, I am trying to put the text box values into variables to work with, but I am unable to access them. The two column names are "EOR" and "CategoryName". I have found several suggestions on other forums to try something like:

protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    TextBox txtEor = (TextBox)myGridName.Rows[e.RowIndex].FindControl("EOR"); 

When I debug the program, txtEor is always null. The only thing i can think of is that I am not referencing the cell properly. I set the Headertext, AccessibleHeaderText, DataField, and SortExpression to "EOR" but it still just comes up null.

Any help would be greatly appreciated!

asp for the gridview:

<asp:GridView ID="grdEOR" runat="server" BackColor="White"
            BorderColor="#999999" OnPageIndexChanging="grdEor_PageIndexChanging"
            BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
            AllowPaging="True"
            PageSize="15" AutoGenerateColumns="False" onrowediting="grdEOR_RowEditing" 
                        onrowcancelingedit="grdEOR_RowCancelingEdit" 
                        onrowupdating="grdEOR_RowUpdating" onrowdeleting="grdEOR_RowDeleting" 
                        ShowFooter="True">
            <PagerSettings Mode="NumericFirstLast" />
            <Columns>
                <asp:BoundField DataField="EORCategoryID" HeaderText="EORCategoryID" 
                    SortExpression="EORCategoryID" ReadOnly="True">
                </asp:BoundField>
                <asp:BoundField DataField="EOR" HeaderText="EOR" SortExpression="EOR" 
                    AccessibleHeaderText="EOR"/>
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                    SortExpression="CategoryName" />
                <asp:CommandField ButtonType="Button" ShowDeleteButton="True" 
                    ShowEditButton="True" />


            </Columns>
            <FooterStyle BackColor="#CCCCCC" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#CCCCCC" BorderColor="Black" 
                BorderStyle="Solid" BorderWidth="5px" />
        </asp:GridView>
A: 

I finally found a way that works:

        string newEor = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string newCategoryName = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
Starwfanatic
It's dangerous, because it assumes that the boundfield is renderized as a textbox. You should use an EditItemTemplate and put a textbox inside it.
onof
Do you know of a good guide on how EditItemTemplate works? I've never used it before.
Starwfanatic
Here is an example: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.edititemtemplate.aspx
onof