Solved

Trying to edit multiple rows in GridView

Posted on 2006-06-29
13
1,382 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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