2

Two exceptions:

  1. Index out of bound
  2. FindControl returns null (it's pretending or not detecting the controls)

cs code: (for now dropdownlist just needs to be populated at editing mode)

protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView3.EditIndex = e.NewEditIndex;
            ShowData("a"); //bind data

            GridViewRow gVR = GridView3.Rows[GridView3.EditIndex];                                       

aspx code:

                 <asp:TemplateField HeaderText="x" ItemStyle-CssClass="ix">
                     <EditItemTemplate>
                         <asp:DropDownList ID="xnList"  runat="server" Text='<%# Bind("[columnx]")%>'>
                         </asp:DropDownList>
                     </EditItemTemplate>
                     <ItemTemplate>
                         <asp:Label ID="Label3" runat="server" Text='<%# Bind("[columnx]") %>'></asp:Label>
                     </ItemTemplate>
                     <ItemStyle CssClass="ix" />
                 </asp:TemplateField>

Given above snippet, right at the 3rd line I am getting following error. This is absurd as the same works well for other gridview and this gridview has 10 rows, so definitely not out of bound. What could be the issue here?

References:

EDIT:

Those who are generously trying and sparing their time to help me out with a solution, please check out Jeff Atwood's blog post about Page.FindControl. Reading it, I feel my dropdownlist is definitely a child within Gridview... Given this post, it comes much closer to what I have encountered.. But I am not 100% sure, if same case applies to what I am struggling with, since I have two gridviews. However only one has edit mode controls - the other is plain plain gridvos. Can someone show me the right direction?

EDIT: I have tried each one of above link's answers/solution. None working as of now.

Community
  • 1
  • 1
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • What is the value of GridView3.EditIndex – TGH Jul 16 '14 at 02:26
  • Let's say I click on 3rd row the edit index is 3. And at the same time when I check the row count of Gridview that's also 3!!! how can that be possible, and that's why it's going out of bound.. So I *force* it to be index 2. Then that line pass. BUT (it's not correct). However the DropDownList is still null.. – bonCodigo Jul 16 '14 at 02:34
  • If I recall correct - shouldn't your FindControl be called on e.Item? – Allan S. Hansen Jul 16 '14 at 07:03
  • @AllanS.Hansen Christ sake... this is `GridView` and it aint got `item` property mate... – bonCodigo Jul 16 '14 at 07:18
  • @bonCodigo Steady there, I just remember wrong because I was recently working in a telerik grid which had the item property and had the two mixed. No need to get testy. – Allan S. Hansen Jul 16 '14 at 07:44
  • @AllanS.Hansen no worries mate, ;) I forgot to add the smiley! BTW if familiar, do you have any idea why isn't `RowDataBound` capturing `RowState` correctly during edit mode? By right `RowState` should be `edit`, instead it keeps showing `normal`... – bonCodigo Jul 16 '14 at 07:46
  • I'm not terrible sure, because I rarely use GridView directly, but I did some rudimentary testing and it seems in the Editing event, that I can only get easy access (via FindControl("")) to the item template controls and in the Updating, I get easy access (via FindControl("")) to the EditTemplate controls. It might be possible to access Edit controls via navigating the entire control tree (.controls(x).controls(y) etc) but outside that I cannot find an (easy) way. – Allan S. Hansen Jul 16 '14 at 08:47
  • 1
    @AllanS.Hansen mate, I found the culprit. `` there's no need for that `Text='<%# Bind("[columnx]")%>'` as the databinding is done by RowDataBound() after checking for `edit` mode on the row. This solves couple of issues I had. Thanks for the quality discussion. It helps :) – bonCodigo Jul 16 '14 at 09:50

1 Answers1

2

As many have pointed out the RowDataBound() is the correct event to hook data up for controls within gridview for edit, update or display modes. I was desperate and then tried out Row_Updating. HOwever that wasn't the issue with the error I was getting.

It was mainly due to the Text='<%# Bind("[columnx]")%>' of,

<asp:DropDownList ID="xnList" runat="server" Text='<%# Bind("[columnx]")%>'>

So the final solution is as per any of the answers posted out there.

cs:

    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {            

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                DropDownList ddl = e.Row.FindControlRecursive("dhl") as DropDownList;
                DropDownList stageDDL = e.Row.FindControlRecursive("dhl") as DropDownList;
                stageDDL.DataSource = this.clservice.Getstuff("someparam");
                stageDDL.DataTextField = "columnx";
                stageDDL.DataValueField = "columnx";
                stageDDL.DataBind();

            }
        }
    }

aspx:

            <asp:TemplateField HeaderText="x" ItemStyle-CssClass="ix">
                <EditItemTemplate>
                    <asp:DropDownList ID="xnList"  runat="server" DataTextField="columnx" DataValueField="columnx">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("[columnx]") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle CssClass="ix" />
            </asp:TemplateField>
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • [@MikeSmithDev thanks much to you as well as to OP and everyone for this insight. Although FindControl works for me, yours was better - Jeff Atwood's](http://stackoverflow.com/a/13337066/1389394) – bonCodigo Jul 16 '14 at 09:59