Gridview row databound event - can't get past the 1st row of gridview

Discussion in 'ASP .Net' started by maurban@gmail.com, Oct 12, 2006.

  1. Guest

    Hi there experts,
    I have a gridview with a couple textboxes and a dropdownlist. I'm
    trying to insert a default value into my database driven dropdownlist.

    I'm doing this in the rowdatabound event. My problem is that my code
    only works for the very first row in the gridview. For the first row,
    when I press "edit", my gridview goes to edit mode, my textboxes and
    dropdownlist populate. Further, my dropdownlist has the "Select" I
    forced in as its default value.

    If I try the 2nd row in my gridview, the dropdownlist populates,
    however doesn't trigger the row databound command of inserting a
    default value.

    I tried going the "for each gridview row approach", and that just loops
    for the number of rows I have. If I have two rows, it inserts the
    default value "Select" twice in the dropdownlist for the first row
    only. It still doesn't recognize any other row.

    ---- ASPX snippet ----
    <EditItemTemplate>
    <asp:dropdownlist ID="ddlUpdate_UserTypeID"
    datavaluefield="user_type_id" datatextfield="user_type_desc"
    DataSource='<%# Bind_ddlUserType() %>' Runat="server" />
    </EditItemTemplate>

    ---- Code Behind snippet----
    Sub gvUsers_rowDataBound(ByVal sender As Object, ByVal e As
    GridViewRowEventArgs) _
    Handles gvUsers.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow And _
    e.Row.RowState = DataControlRowState.Edit Then
    Dim user_id As String = e.Row.Cells(0).Text
    CType(e.Row.FindControl("ddlUpdate_UserTypeID"),
    DropDownList).Items.Insert(0, New ListItem("Select"))
    End If

    Please let me know if you require more detail. I've been spinning my
    wheels on this for sometime now.

    Thanks much for any assistance!

    Mike
     
    , Oct 12, 2006
    #1
    1. Advertising

  2. Superman Guest

    Maurban,

    I would switch out of RowDataBound mode and use the RowCreated
    method...when switching between EditItemIndex values, the data is not
    re-bound therefore your RowDataBound event will not fire. Here's what
    my solution would be:

    1) Leave all your databound code intact, and add an additional event
    handler to the gridview for "RowCreated".

    2) Insert the following code (c#)

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
    DropDownList list =
    (DropDownList)e.Row.Cells[x].FindControl("myDropDown");
    myList.Items.Insert(0, new ListItem("defaultValue", "Select"));
    }

    Let us know how it goes!

    -Brenton


    wrote:
    > Hi there experts,
    > I have a gridview with a couple textboxes and a dropdownlist. I'm
    > trying to insert a default value into my database driven dropdownlist.
    >
    > I'm doing this in the rowdatabound event. My problem is that my code
    > only works for the very first row in the gridview. For the first row,
    > when I press "edit", my gridview goes to edit mode, my textboxes and
    > dropdownlist populate. Further, my dropdownlist has the "Select" I
    > forced in as its default value.
    >
    > If I try the 2nd row in my gridview, the dropdownlist populates,
    > however doesn't trigger the row databound command of inserting a
    > default value.
    >
    > I tried going the "for each gridview row approach", and that just loops
    > for the number of rows I have. If I have two rows, it inserts the
    > default value "Select" twice in the dropdownlist for the first row
    > only. It still doesn't recognize any other row.
    >
    > ---- ASPX snippet ----
    > <EditItemTemplate>
    > <asp:dropdownlist ID="ddlUpdate_UserTypeID"
    > datavaluefield="user_type_id" datatextfield="user_type_desc"
    > DataSource='<%# Bind_ddlUserType() %>' Runat="server" />
    > </EditItemTemplate>
    >
    > ---- Code Behind snippet----
    > Sub gvUsers_rowDataBound(ByVal sender As Object, ByVal e As
    > GridViewRowEventArgs) _
    > Handles gvUsers.RowDataBound
    >
    > If e.Row.RowType = DataControlRowType.DataRow And _
    > e.Row.RowState = DataControlRowState.Edit Then
    > Dim user_id As String = e.Row.Cells(0).Text
    > CType(e.Row.FindControl("ddlUpdate_UserTypeID"),
    > DropDownList).Items.Insert(0, New ListItem("Select"))
    > End If
    >
    > Please let me know if you require more detail. I've been spinning my
    > wheels on this for sometime now.
    >
    > Thanks much for any assistance!
    >
    > Mike
     
    Superman, Oct 12, 2006
    #2
    1. Advertising

  3. Superman Guest

    Maurban,

    I just thought of something else.

    You have your datasource being bound to a function call. Be sure it
    doesn't bind over every post. Make sure that function checks for
    Page.IsPostBack.

    Good luck.

    -Brenton


    wrote:
    > Hi there experts,
    > I have a gridview with a couple textboxes and a dropdownlist. I'm
    > trying to insert a default value into my database driven dropdownlist.
    >
    > I'm doing this in the rowdatabound event. My problem is that my code
    > only works for the very first row in the gridview. For the first row,
    > when I press "edit", my gridview goes to edit mode, my textboxes and
    > dropdownlist populate. Further, my dropdownlist has the "Select" I
    > forced in as its default value.
    >
    > If I try the 2nd row in my gridview, the dropdownlist populates,
    > however doesn't trigger the row databound command of inserting a
    > default value.
    >
    > I tried going the "for each gridview row approach", and that just loops
    > for the number of rows I have. If I have two rows, it inserts the
    > default value "Select" twice in the dropdownlist for the first row
    > only. It still doesn't recognize any other row.
    >
    > ---- ASPX snippet ----
    > <EditItemTemplate>
    > <asp:dropdownlist ID="ddlUpdate_UserTypeID"
    > datavaluefield="user_type_id" datatextfield="user_type_desc"
    > DataSource='<%# Bind_ddlUserType() %>' Runat="server" />
    > </EditItemTemplate>
    >
    > ---- Code Behind snippet----
    > Sub gvUsers_rowDataBound(ByVal sender As Object, ByVal e As
    > GridViewRowEventArgs) _
    > Handles gvUsers.RowDataBound
    >
    > If e.Row.RowType = DataControlRowType.DataRow And _
    > e.Row.RowState = DataControlRowState.Edit Then
    > Dim user_id As String = e.Row.Cells(0).Text
    > CType(e.Row.FindControl("ddlUpdate_UserTypeID"),
    > DropDownList).Items.Insert(0, New ListItem("Select"))
    > End If
    >
    > Please let me know if you require more detail. I've been spinning my
    > wheels on this for sometime now.
    >
    > Thanks much for any assistance!
    >
    > Mike
     
    Superman, Oct 12, 2006
    #3
  4. Guest

    Brenton -- thanks sir, I'll give it a go and let you know.

    Mike

    Superman wrote:
    > Maurban,
    >
    > I just thought of something else.
    >
    > You have your datasource being bound to a function call. Be sure it
    > doesn't bind over every post. Make sure that function checks for
    > Page.IsPostBack.
    >
    > Good luck.
    >
    > -Brenton
    >
    >
    > wrote:
    > > Hi there experts,
    > > I have a gridview with a couple textboxes and a dropdownlist. I'm
    > > trying to insert a default value into my database driven dropdownlist.
    > >
    > > I'm doing this in the rowdatabound event. My problem is that my code
    > > only works for the very first row in the gridview. For the first row,
    > > when I press "edit", my gridview goes to edit mode, my textboxes and
    > > dropdownlist populate. Further, my dropdownlist has the "Select" I
    > > forced in as its default value.
    > >
    > > If I try the 2nd row in my gridview, the dropdownlist populates,
    > > however doesn't trigger the row databound command of inserting a
    > > default value.
    > >
    > > I tried going the "for each gridview row approach", and that just loops
    > > for the number of rows I have. If I have two rows, it inserts the
    > > default value "Select" twice in the dropdownlist for the first row
    > > only. It still doesn't recognize any other row.
    > >
    > > ---- ASPX snippet ----
    > > <EditItemTemplate>
    > > <asp:dropdownlist ID="ddlUpdate_UserTypeID"
    > > datavaluefield="user_type_id" datatextfield="user_type_desc"
    > > DataSource='<%# Bind_ddlUserType() %>' Runat="server" />
    > > </EditItemTemplate>
    > >
    > > ---- Code Behind snippet----
    > > Sub gvUsers_rowDataBound(ByVal sender As Object, ByVal e As
    > > GridViewRowEventArgs) _
    > > Handles gvUsers.RowDataBound
    > >
    > > If e.Row.RowType = DataControlRowType.DataRow And _
    > > e.Row.RowState = DataControlRowState.Edit Then
    > > Dim user_id As String = e.Row.Cells(0).Text
    > > CType(e.Row.FindControl("ddlUpdate_UserTypeID"),
    > > DropDownList).Items.Insert(0, New ListItem("Select"))
    > > End If
    > >
    > > Please let me know if you require more detail. I've been spinning my
    > > wheels on this for sometime now.
    > >
    > > Thanks much for any assistance!
    > >
    > > Mike
     
    , Oct 12, 2006
    #4
  5. Guest

    Well, this is still a no-go... Adding this code to the rowcreated event
    has no effect on the gridview.

    My original code is works for the first row of the gridview. I think I
    just need to figure out how to make it acknowlege rows 2 +.

    When I click the edit button for rows 2 +, there is no effect on the
    rowdatabound event (or rowcreated event)

    Any other ideas?

    Thanks!


    wrote:
    > Brenton -- thanks sir, I'll give it a go and let you know.
    >
    > Mike
    >
    > Superman wrote:
    > > Maurban,
    > >
    > > I just thought of something else.
    > >
    > > You have your datasource being bound to a function call. Be sure it
    > > doesn't bind over every post. Make sure that function checks for
    > > Page.IsPostBack.
    > >
    > > Good luck.
    > >
    > > -Brenton
    > >
    > >
    > > wrote:
    > > > Hi there experts,
    > > > I have a gridview with a couple textboxes and a dropdownlist. I'm
    > > > trying to insert a default value into my database driven dropdownlist.
    > > >
    > > > I'm doing this in the rowdatabound event. My problem is that my code
    > > > only works for the very first row in the gridview. For the first row,
    > > > when I press "edit", my gridview goes to edit mode, my textboxes and
    > > > dropdownlist populate. Further, my dropdownlist has the "Select" I
    > > > forced in as its default value.
    > > >
    > > > If I try the 2nd row in my gridview, the dropdownlist populates,
    > > > however doesn't trigger the row databound command of inserting a
    > > > default value.
    > > >
    > > > I tried going the "for each gridview row approach", and that just loops
    > > > for the number of rows I have. If I have two rows, it inserts the
    > > > default value "Select" twice in the dropdownlist for the first row
    > > > only. It still doesn't recognize any other row.
    > > >
    > > > ---- ASPX snippet ----
    > > > <EditItemTemplate>
    > > > <asp:dropdownlist ID="ddlUpdate_UserTypeID"
    > > > datavaluefield="user_type_id" datatextfield="user_type_desc"
    > > > DataSource='<%# Bind_ddlUserType() %>' Runat="server" />
    > > > </EditItemTemplate>
    > > >
    > > > ---- Code Behind snippet----
    > > > Sub gvUsers_rowDataBound(ByVal sender As Object, ByVal e As
    > > > GridViewRowEventArgs) _
    > > > Handles gvUsers.RowDataBound
    > > >
    > > > If e.Row.RowType = DataControlRowType.DataRow And _
    > > > e.Row.RowState = DataControlRowState.Edit Then
    > > > Dim user_id As String = e.Row.Cells(0).Text
    > > > CType(e.Row.FindControl("ddlUpdate_UserTypeID"),
    > > > DropDownList).Items.Insert(0, New ListItem("Select"))
    > > > End If
    > > >
    > > > Please let me know if you require more detail. I've been spinning my
    > > > wheels on this for sometime now.
    > > >
    > > > Thanks much for any assistance!
    > > >
    > > > Mike
     
    , Oct 13, 2006
    #5
  6. Guest

    I figured out the solution:

    If ((e.Row.RowState = (DataControlRowState.Edit Or _
    DataControlRowState.Alternate)) Or _
    (e.Row.RowState = DataControlRowState.Edit)) Then

    bla bla

    end if

    Works great!! Hopefully this will help someone else out there.

    wrote:
    > Well, this is still a no-go... Adding this code to the rowcreated event
    > has no effect on the gridview.
    >
    > My original code is works for the first row of the gridview. I think I
    > just need to figure out how to make it acknowlege rows 2 +.
    >
    > When I click the edit button for rows 2 +, there is no effect on the
    > rowdatabound event (or rowcreated event)
    >
    > Any other ideas?
    >
    > Thanks!
    >
    >
    > wrote:
    > > Brenton -- thanks sir, I'll give it a go and let you know.
    > >
    > > Mike
    > >
    > > Superman wrote:
    > > > Maurban,
    > > >
    > > > I just thought of something else.
    > > >
    > > > You have your datasource being bound to a function call. Be sure it
    > > > doesn't bind over every post. Make sure that function checks for
    > > > Page.IsPostBack.
    > > >
    > > > Good luck.
    > > >
    > > > -Brenton
    > > >
    > > >
    > > > wrote:
    > > > > Hi there experts,
    > > > > I have a gridview with a couple textboxes and a dropdownlist. I'm
    > > > > trying to insert a default value into my database driven dropdownlist.
    > > > >
    > > > > I'm doing this in the rowdatabound event. My problem is that my code
    > > > > only works for the very first row in the gridview. For the first row,
    > > > > when I press "edit", my gridview goes to edit mode, my textboxes and
    > > > > dropdownlist populate. Further, my dropdownlist has the "Select" I
    > > > > forced in as its default value.
    > > > >
    > > > > If I try the 2nd row in my gridview, the dropdownlist populates,
    > > > > however doesn't trigger the row databound command of inserting a
    > > > > default value.
    > > > >
    > > > > I tried going the "for each gridview row approach", and that just loops
    > > > > for the number of rows I have. If I have two rows, it inserts the
    > > > > default value "Select" twice in the dropdownlist for the first row
    > > > > only. It still doesn't recognize any other row.
    > > > >
    > > > > ---- ASPX snippet ----
    > > > > <EditItemTemplate>
    > > > > <asp:dropdownlist ID="ddlUpdate_UserTypeID"
    > > > > datavaluefield="user_type_id" datatextfield="user_type_desc"
    > > > > DataSource='<%# Bind_ddlUserType() %>' Runat="server" />
    > > > > </EditItemTemplate>
    > > > >
    > > > > ---- Code Behind snippet----
    > > > > Sub gvUsers_rowDataBound(ByVal sender As Object, ByVal e As
    > > > > GridViewRowEventArgs) _
    > > > > Handles gvUsers.RowDataBound
    > > > >
    > > > > If e.Row.RowType = DataControlRowType.DataRow And _
    > > > > e.Row.RowState = DataControlRowState.Edit Then
    > > > > Dim user_id As String = e.Row.Cells(0).Text
    > > > > CType(e.Row.FindControl("ddlUpdate_UserTypeID"),
    > > > > DropDownList).Items.Insert(0, New ListItem("Select"))
    > > > > End If
    > > > >
    > > > > Please let me know if you require more detail. I've been spinning my
    > > > > wheels on this for sometime now.
    > > > >
    > > > > Thanks much for any assistance!
    > > > >
    > > > > Mike
     
    , Oct 13, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GaryDean
    Replies:
    1
    Views:
    8,699
    Walter Wang [MSFT]
    Aug 18, 2006
  2. rodchar
    Replies:
    0
    Views:
    397
    rodchar
    Nov 26, 2007
  3. Craig Buchanan
    Replies:
    1
    Views:
    2,470
    Reggie
    Oct 2, 2008
  4. Manu
    Replies:
    0
    Views:
    171
  5. Karlo Lozovina

    Can't get past 'use strict' :(

    Karlo Lozovina, Dec 15, 2004, in forum: Perl Misc
    Replies:
    15
    Views:
    147
    Brian McCauley
    Dec 16, 2004
Loading...

Share This Page