?
Solved

GridView RowDataBound and Checking Alternate Rows For Edit Mode

Posted on 2012-08-22
4
Medium Priority
?
4,525 Views
Last Modified: 2012-08-23
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

0
Comment
Question by:David Bach
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38322800
Why are you using RowDataBound for checking edit mode? Use RowEditing event instead.
0
 

Author Comment

by:David Bach
ID: 38323035
Hi CodeCruiser;

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

David
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 38323444
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
 

Author Closing Comment

by:David Bach
ID: 38326088
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

829 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