I’m building a GridView that contains edit/update capabilities.  In edit mode for certain rows I need to turn on a drop down list, and based on that row add/remove Items from the list, other rows I want to show a text input to edit, all based on Foreign Key for that row in the database (which I store in a DataKey to access).  I handle the OnRowDataBound with a custom subroutine.  This hasn?t been an issue in the past but this time I have multiple controls in the ItemTemplate and need to manipulate at databound time, which is a first. 


<asp:DropDownList ID=?ddlAnswer? runat=?server? Visible=?False?>

<asp:ListItem Value=?Yes? Text=?Yes? />

<asp:ListItem Value=?No? Text=?No? />

<asp:ListItem Value=?Neutral? Text=?Neutral? />


<asp:TextBox ID=?txtAnswer? runat=?server? Text=?<%# Bind(?Answer?) %>? Visible=?False?></asp:TextBox>



Both have visible=false so I need to get at the bound event and flip one on and change the item list if it?s the DropDownList.


I find the e.Row.RowState property and assign an if conditional to DataControlRowState.Edit. 


If e.Row.RowState = DataControlRowState.Edit Then



CType(e.Row.FindControl(?lbUpdate?), LinkButton).CommandArgument = _



Dim iAnswerTypeID = CInt(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(0))


Select Case CInt(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(0))

Case 1

CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Visible = True

CType(e.Row.FindControl(?ddlAnswer?), DropDownList).SelectedValue = _



Case 2

CType(e.Row.FindControl(?txtAnswer?), TextBox).Visible = True


Case 3


Case 4

CType(e.Row.FindControl(?txtAnswer?), TextBox).Visible = True

CType(e.Row.FindControl(?txtAnswer?), TextBox).TextMode = TextBoxMode.MultiLine


Case 5

CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Visible = True

Dim oldListItem As ListItem = CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Items.FindByValue(?Neutral?)


CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Items.Remove(oldListItem)


CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Items.Add _

(New ListItem(CStr(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(1)), CStr(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(1))))

CType(e.Row.FindControl(?ddlAnswer?), DropDownList).SelectedValue = _


End Select


End If


This doesn?t work, I never get into the if block for all rows. Setting a debug point I find the state of the row is not always DataControlRowState.Edit, but sometimes Alternate with an enumeration of 5.  Looking thru Microsoft documentation I don?t see any 5 reference. 

My Assumption

I am assuming that every property of the DataControlRowState handler has an Alternate row of that plus 1.  So edit  = 4 and edit on an alternative row is 5.


A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at tboland@gmail.com

10 thoughts on “e.Row.RowState

  1. Chris Davis says:

    Hi, I think it’s the IF, have you tried:

    e.Row.RowType == DataControlRowType.DataRow

  2. Tim says:

    The above code is within an IF e.Row.RowType == DataControlRowType.DataRow block.

  3. Matt says:

    Looks like a bug in the GridView control. ( e.Row.RowState == DataControlRowState.Edit ) is never true, even when I’ve clicked to edit the row. Instead, I have to check if( e.Row.RowIndex == MyGridView.EditIndex )

  4. Chris says:

    The rowstate can either be:

    DataControlRowState.Edit or
    (DataControlRowState.Alternate | DataControlRowState.Edit)

    so your IF statement should display:

    If e.Row.RowState = DataControlRowState.Edit OR (e.Row.RowState = DataControlRowState.Alternate|DataControlRowState.Edit) Then

    The alternate tag is assigned to every odd index in the gridview. The tag is used for asthetic purposes (you can assign a different background color for every other line)

  5. Chris says:

    Correction to my comment:

    If e.Row.RowState = DataControlRowState.Edit OR e.Row.RowState = (DataControlRowState.Alternate|DataControlRowState.Edit) Then
  6. Ryan says:

    I have something similare working like this:

    If Not e.Row.RowState = 5 And Not e.Row.RowState = DataControlRowState.Edit Then

    What is the | in your statement for? I have not seen that used before.


  7. Our dazzling collection of interior water walls will enthuse and impress anyone who sees it in your home or office.

  8. Deep says:

    hi chris, your alternate|edit concept was really helpful. thanks

  9. obd2 says:

    thank you for your sharing , i love your post

  10. Glen says:

    Great things to do is ti make such articles

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>