Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1388
  • Last Modified:

Trying to edit multiple rows in GridView

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
freestyle18
Asked:
freestyle18
  • 6
  • 5
1 Solution
 
aki4uCommented:
try this:
Private Shared tableCopied As Boolean = False
0
 
freestyle18Author Commented:
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
 
aki4uCommented:
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!

 
freestyle18Author Commented:
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
 
aki4uCommented:
Try creating a new page, copy all the same code and try it again.
0
 
freestyle18Author Commented:
That didn't work either....I'm not sure what I'm doing wrong but there must be something obviously.  Anything else?

MikeD
0
 
aki4uCommented:
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
 
freestyle18Author Commented:
Well nothing I've tried seems to work for me so I guess I'll just try something different.  Thanks for trying...

MikeD
0
 
freestyle18Author Commented:
This issue can be closed.  I went a different route and solved my own problem.  Thank you for trying to help.
0
 
aki4uCommented:
How did you solve it?
0
 
freestyle18Author Commented:
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
 
CetusMODCommented:
PAQed with points refunded (500)

CetusMOD
Community Support Moderator
0

Featured Post

Technology Partners: 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!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now