GridView RowDataBound and Checking Alternate Rows For Edit Mode

Posted on 2012-08-22
Last Modified: 2012-08-23

I have a RowDataBound event for a gridView control I am populating with table data. The check if the row was selected for edit does not seem to be working for alternate rows, only normal rows.

Can someone please shed light on what I am doing wrong?

Much thanks ... David

Protected Sub gvProducts_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProducts.RowDataBound

        Dim lblActive As New Label
        Dim ddlActive As New DropDownList
        Dim drProduct As schRegUsers.up_s_ProductAllRow
        Dim tbxProductId As New TextBox

        If e.Row.RowType = DataControlRowType.DataRow Then
            Exit Sub
        End If

        If e.Row.RowState = DataControlRowState.Normal Or
           e.Row.RowState = DataControlRowState.Alternate Then
            lblActive = e.Row.FindControl("lblActive")

            If lblActive.Text = "True" Then
                lblActive.Text = "Yes"
                lblActive.Text = "No"
            End If

        End If

        If e.Row.RowState = DataControlRowState.Edit Then
            ddlActive = e.Row.FindControl("ddlActive")
            tbxProductId = e.Row.FindControl("tbxProductId")
            drProduct = dsRegUsers.up_s_ProductAll.FindBystrProductId(tbxProductId.Text)

            If drProduct.blnActive Then
                ddlActive.SelectedIndex = 1
                ddlActive.SelectedIndex = 0
            End If

        End If

    End Sub

Open in new window

Question by:David Bach
    LVL 83

    Expert Comment

    Why are you using RowDataBound for checking edit mode? Use RowEditing event instead.

    Author Comment

    by:David Bach
    Hi CodeCruiser;

    So, when the RowEditing event fires ... the templates defined for editing will be available at that time?

    LVL 26

    Accepted Solution

    Hi David,

    Protected Sub gvProducts_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProducts.RowDataBound
       ' declare your variables
       Dim objTextBox As TextBox = Nothing
        ' get the commandargument
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        ' Retrieve the row that contains the controls to be found
        Dim row As GridViewRow = sender.Rows(index)
        If (row.RowType = DataControlRowType.DataRow) AndAlso ((row.RowState And DataControlRowState.Edit) > 0) Then
          If row.RowState = DataControlRowState.Normal Or row.RowState = DataControlRowState.Alternate Then
            ' do stuff
                objTextBox = CType(row.FindControl("tbxProductId"), TextBox)
                If objTextBox IsNot Nothing Then
                    With objTextBox
                        .Text = "Hello World!"
                        ' strSomeVariable = .Text
                    End With
                End If
                ' ... do more stuff
          End If
        End If

    Open in new window


    Author Closing Comment

    by:David Bach
    Hi Alan;

    And of course after reading the fine print regarding the DataControlRowState.Edit numeration, the description indicates to use a logical bit function. So, after ANDing the expressions as you have done, Alan, all works well.

    Much thanks ... David

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now