Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to edit multiple rows in GridView

Posted on 2006-06-29
13
Medium Priority
?
1,387 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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