GridView RowDataBound and Checking Alternate Rows For Edit Mode

Greetings;

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
        Else
            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"
            Else
                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
            Else
                ddlActive.SelectedIndex = 0
            End If

        End If

    End Sub

Open in new window

David BachAsked:
Who is Participating?
 
Alan WarrenConnect With a Mentor Applications DeveloperCommented:
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
                    .Dispose()
                End With
            End If

            ' ... do more stuff
        
      End If
    End If

Open in new window

Alan
0
 
CodeCruiserCommented:
Why are you using RowDataBound for checking edit mode? Use RowEditing event instead.
0
 
David BachAuthor Commented:
Hi CodeCruiser;

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

David
0
 
David BachAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.