Solved

Trying to edit multiple rows in GridView

Posted on 2006-06-29
13
1,385 Views
Last Modified: 2007-12-19
As the title says, I'm trying to edit multiple rows in my GridView at once.  I have the following code:

Private tableCopied As Boolean = False
    Private originalDataTable As System.Data.DataTable

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If Not tableCopied Then
                originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
                ViewState("originalValuesDataTable") = originalDataTable
                tableCopied = True
            Else
                Label1.Text = "what?"
            End If
        End If
    End Sub

    Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)

        For Each r As GridViewRow In GridView1.Rows
            If IsRowModified(r) Then GridView1.UpdateRow(r.RowIndex, False)
        Next

        ' Rebind the Grid to repopulate the original values table.
        tableCopied = False
        GridView1.DataBind()
    End Sub

    Protected Function IsRowModified(ByVal r As GridViewRow) As Boolean
        Dim currentID As Integer
        Dim currentCompleted As String

        currentID = Convert.ToInt32(GridView1.DataKeys(0).Value)

        If CType(r.FindControl("cbCompleted"), CheckBox).Checked Then
            currentCompleted = "True"
        Else
            currentCompleted = "False"
        End If

       

        Dim row As System.Data.DataRow = _
            originalDataTable.Select(String.Format("RegistrationID = {0}", currentID))(0)

        If Not currentCompleted.Equals(row("Completed").ToString) Then Return True

        Return False
    End Function

It's bombing out right away when the gridview populates in the GridView1_RowDataBound sub.  I added the label1.text to make sure of this.  I have "Private tableCopied As Boolean = False" and it still goes to the ELSE clause right away.  Not sure what's happening, any help would be appreciated.  I'm assigning a large point value because I need this ASAP.

MikeD
0
Comment
Question by:freestyle18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
13 Comments
 
LVL 10

Expert Comment

by:aki4u
ID: 17012451
try this:
Private Shared tableCopied As Boolean = False
0
 

Author Comment

by:freestyle18
ID: 17014916
That didn't do anything unfortunately, it still goes straight to the ELSE clause and sets label1.text to "what?"  Any other suggestions?

MikeD
0
 
LVL 10

Expert Comment

by:aki4u
ID: 17015129
This should work, unless you have somewhere else tableCopied = False? Did you try to debug it and see that it hits the line: tableCopied = True? Try to replace it with: Label1.Text = "OK" or to debug it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:freestyle18
ID: 17015231
It doesn't work, but what I've found is I can replace:

If Not tableCopied Then
                originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
                ViewState("originalValuesDataTable") = originalDataTable
                tableCopied = True
            Else
 with:

If Not FALSE Then
                originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
                ViewState("originalValuesDataTable") = originalDataTable
                tableCopied = True
            Else

And it still doesn't do anything.  At that point it goes through the If statement and sets tableCopied = True, but the Button Click event still doesn't do anything, so I have additonal problems too.

MikeD
0
 
LVL 10

Expert Comment

by:aki4u
ID: 17017356
Try creating a new page, copy all the same code and try it again.
0
 

Author Comment

by:freestyle18
ID: 17020194
That didn't work either....I'm not sure what I'm doing wrong but there must be something obviously.  Anything else?

MikeD
0
 
LVL 10

Expert Comment

by:aki4u
ID: 17021293
if it hits tableCopied = True and you decalred it as Shared, there is no reason why it should be false, unless you change the value, like in UpdateButton_Click where you have tableCopied = False. maybe you have it in some other place? put break point on UpdateButton_Click and see.
0
 

Author Comment

by:freestyle18
ID: 17112300
Well nothing I've tried seems to work for me so I guess I'll just try something different.  Thanks for trying...

MikeD
0
 

Author Comment

by:freestyle18
ID: 17271398
This issue can be closed.  I went a different route and solved my own problem.  Thank you for trying to help.
0
 
LVL 10

Expert Comment

by:aki4u
ID: 17275225
How did you solve it?
0
 

Author Comment

by:freestyle18
ID: 17366361
Sorry for the delay in responding, I've been in and out of the office lately.  Here is what I finally came up with for editing multiple rows at once.  I included a checkbox in a template field and if checked when the Update button was pressed then it set the field to a value...

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
        For index As Integer = 0 To GridView1.Rows.Count - 1
            'Programmatically access the CheckBox from the TemplateField
            Dim cb As CheckBox = CType(GridView1.Rows(index).FindControl("cbCompleted"), CheckBox)

            'If it's checked, delete it...
            If cb.Checked Then
                sdsClassRegistration.UpdateParameters(0).DefaultValue = "True"
                sdsClassRegistration.UpdateParameters(1).DefaultValue = CType(GridView1.Rows(index).FindControl("txtMissedHours"), TextBox).Text
            Else
                sdsClassRegistration.UpdateParameters(0).DefaultValue = "False"
                sdsClassRegistration.UpdateParameters(1).DefaultValue = "0"
            End If

            sdsClassRegistration.UpdateParameters(2).DefaultValue = GridView1.DataKeys(index).Value.ToString()
            sdsClassRegistration.Update()
        Next

    End Sub


Pretty easy once I wrapped my head around it...thanks for all the help!

MadLad18
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 17575784
PAQed with points refunded (500)

CetusMOD
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

631 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