Solved

Trying to edit multiple rows in GridView

Posted on 2006-06-29
13
1,380 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
  • 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now