Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

Sponsored Links

Discuss "update gridview dynamically" in the .Net Development forum.

.Net Development - Learn about the Microsoft.Net framework and how to create powerful web-based (ASP.net) and client-based (Windows Forms) applications utilizing various languages such as C#, VB.Net, J# and others.


Reply « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1  
Old May 7th, 2008, 10:46 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question update gridview dynamically

Hey everyone, it peebman2000 i'm posting this question for a friend and co-worker who is currently working on a .Net application. And here is his question:

I need help updating gridviews that are created dynamically in the AJAX TabContainer (in which the tabs are also created dynamically).

(The code I have currently creates 9 tabs with 2 gridviews in each tab.)

Everything works except I can't figure out how to handle the update. I thought about creating a generic RowEditing Sub and passing the gridview as a third parameter
However, I'm not really sure how to do that.

Any help or suggestions would be grateful!

Thanks!

ASPX page
Code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" title="Title" %>
 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePartialRendering="true">
    </cc1:ToolkitScriptManager>
      <cc1:TabContainer ID="TabContainer1" runat="server" AutoPostBack="true">
          
    </cc1:TabContainer> 
                         
        
</asp:Content>


ASPX.VB CODE:
Code:
Protected Sub Gridview_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs, ByVal grid As GridView)
        grid.EditIndex = e.NewEditIndex
    End Sub
 
    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
 Dim gv1, gv2 As New GridView()
 MyBase.OnInit(e)
        Dim sqlconn As New SqlConnection(WebConfigurationManager.ConnectionStrings("ConnectionString").ToString)
        Dim cmdExpr As SqlCommand
        Dim strSQL As String
        Dim myReader As SqlDataReader
        Dim id As Integer = 1
 
  strSQL = "Select * from sometable"
        cmdExpr = New SqlCommand(strSQL, sqlconn)
        sqlconn.Open()
        myReader = cmdExpr.ExecuteReader
        While myReader.Read
            ' Create the tab panel's content container and creates the table inside the tab content
            Dim tabContent, table As New Control()
 
            Dim tab As New AjaxControlToolkit.TabPanel()
            tab.ID = "tabPanel" + id
            tab.HeaderText = "Text"
            Dim tbl As New Table()
            Dim label As New Label()
            Dim gv1, gv2 As New GridView()
 
            Dim sdsold, sdsnew As New SqlDataSource()
     sdsold.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
            sdsnew.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
            sdsold.ID = "sdsOld" + id
            sdsnew.ID = "sdsNew" + id
            sdsnew.SelectCommand = "Select * from table1"
            sdsold.SelectCommand = "Select * from table2"
 

            gv1.EmptyDataText = "No record(s) exist"
            gv1.ID = "GV1" + id
            gv1.Attributes.Add("runat", "server")
     'Add Values to the editRowCommand
            gv1.Attributes.Add("onRowEditing", "Values go here")
            gv1.DataSourceID = sdsnew.ID
            gv1.Visible = True
            gv1.AutoGenerateEditButton = True
     
          
     gv2.ID = "GV2" + id
            gv2.EmptyDataText = "No record(s) exist"
     gv2.Attributes.Add("runat", "server")
     'Add Values to the editRowCommand
            gv2.Attributes.Add("onRowEditing", "Values go here")
            gv2.DataSourceID = sdsold.ID
            gv2.Visible = True
            gv2.AutoGenerateEditButton = True
 
            ' Create Table
            Dim tr As New TableRow()
            ' Create column 1
            Dim td1 As New TableCell()
            td1.Controls.Add(gv2)
            ' Create column 2
            Dim td2 As New TableCell()
            ' Add control to the table cell
            td2.Controls.Add(gv1)
            ' Add cell to the row
            tr.Cells.Add(td1)
            tr.Cells.Add(td2)
            ' Add row to the table.
            tbl.Rows.Add(tr)
 

            tabContent.Controls.Add(label)
            table.Controls.Add(tbl)
            table.Controls.Add(sdsnew)
            table.Controls.Add(sdsold)
            tab.Controls.Add(tabContent)
            tab.Controls.Add(table)
 
            Me.TabContainer1.Tabs.Add(tab)
 
            gv1.DataBind()
            gv2.DataBind()
 
            id += 1
        End While
        sqlconn.Close()
 
 'Set active tab index
        Me.TabContainer1.ActiveTabIndex = 0
     End Sub
THANKS FOR HELPING.
Reply With Quote
Sponsored Links
  #2  
Old May 7th, 2008, 10:51 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

What exactly do you mean by "Update Gridview"?
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
  #3  
Old May 7th, 2008, 11:03 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question reply

Hey thanks jmurrayhead, he's trying to edit the rows in the gridviews that are created. He says this:

Quote:
When I click on the edit link in the gridview, the textboxes appear. I am able to edit the values however, when I click the save link the old values appear and the gridview is still in edit mode.
Any ideas on how to edit the gridview and he's using SQL by the way.

Thanks.

Quote:
Originally Posted by jmurrayhead View Post
What exactly do you mean by "Update Gridview"?
Reply With Quote
  #4  
Old May 7th, 2008, 11:09 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

When you create the GridVew, try setting the event to the sub you create:

Code:
Dim WithEvents gv1 As New GridView
AddHandler gv1.RowEditing, AddressOf gv1_RowEditing
Reply With Quote
  #5  
Old May 7th, 2008, 11:26 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Thumbs up reply

Okay he's trying it, i'll let you know if it works for him.

Quote:
Originally Posted by jmurrayhead View Post
When you create the GridVew, try setting the event to the sub you create:

Code:
Dim WithEvents gv1 As New GridView
AddHandler gv1.RowEditing, AddressOf gv1_RowEditing
Reply With Quote
  #6  
Old May 7th, 2008, 11:41 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question reply

Hey jmurrayhead, its not working, here's his reply:

The only way I can add that code is:

Declaring the gridviews outside of Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs) :

Dim WithEvents gv1, gv2 As New GridView()

Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
......
End Sub

Then I created the following subroutines:

Protected Sub gv1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gv1.EditIndex = e.NewEditIndex
End Sub

Protected Sub gv2_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gv2.EditIndex = e.NewEditIndex
End Sub

But, because I created the Gridviews outside of the loop, the other tabs no longer have the gridviews inside them.
It is still not updating the edits. Do I need to create the other methods (RowUpdating, RowUpdated ) to handle this?

Thanks again



Quote:
Originally Posted by jmurrayhead View Post
When you create the GridVew, try setting the event to the sub you create:

Code:
Dim WithEvents gv1 As New GridView
AddHandler gv1.RowEditing, AddressOf gv1_RowEditing
Reply With Quote
  #7  
Old May 7th, 2008, 01:19 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

What if you declared the GridViews within the class?

Code:
Partial Class _myClass
    Inherits System.Web.UI.Page
 
    Private WithEvents gv1 As New GridView
 
    ''' Ad sub routines and such
    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
 
    End Sub
End Class
Like this, the gridviews would be within scope of every routine and should be accessible.
Reply With Quote
  #8  
Old May 7th, 2008, 01:19 PM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Default reply

Okay, making some progress. My co-worker has changed some of the code a little and here is his reply:

I added the following code:
Code:
Protected Sub gv1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'cast gridview to get values
        Dim tempGrid As GridView = DirectCast(sender, GridView)
        tempGrid.EditIndex = e.NewEditIndex
        Label1.Text = tempGrid.ID
    End Sub
 
    Protected Sub gv2_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        Dim tempGrid As GridView = DirectCast(sender, GridView)
        tempGrid.EditIndex = e.NewEditIndex
        Label1.Text = tempGrid.ID
    End Sub
The label displays the id of the grid. So, that is working. Now, I just need to code the update and the update command. I'll keep you posted on my progress.

Thanks for your help.
Reply With Quote
  #9  
Old May 7th, 2008, 01:41 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

Indeed, if you can get RowEditing working, you should be able to get the other events working
Reply With Quote
  #10  
Old May 7th, 2008, 02:23 PM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question reply

Hey thanks, this is starting to become a really crazy challenge for him, bumping head against the wall fustration; any ideas or suggestions on getting the other events working. Here's his reply and code.



I added gv1_RowUpating, gv2_RowUpdating, gv1_RowUpdated, and gv2_RowUpdated.

However it still doesn't seem like the update is being triggered. When I click update the new value is replaced by the old value and the gridview stays in edit mode.

Do I need to rebind the gridview or is there something else I'm missing?

Thanks!

Aspx.vb code:
Code:
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
 
 MyBase.OnInit(e)
        Dim sqlconn As New SqlConnection(WebConfigurationManager.ConnectionStrings("ConnectionString").ToString)
        Dim cmdExpr As SqlCommand
        Dim strSQL As String
        Dim myReader As SqlDataReader
        Dim id As Integer = 1
 
 
 
   strSQL = "Select * from sometable"
        cmdExpr = New SqlCommand(strSQL, sqlconn)
        sqlconn.Open()
        myReader = cmdExpr.ExecuteReader
        While myReader.Read
            ' Create the tab panel's content container and creates the table inside the tab content
            Dim tabContent, table As New Control()
 
 
 
            Dim tab As New AjaxControlToolkit.TabPanel()
            tab.ID = "tabPanel" + id
            tab.HeaderText = "Text"
            Dim tbl As New Table()
            Dim label As New Label()
            Dim gv1, gv2 As New GridView()
 
      AddHandler gv1.RowEditing, AddressOf gv1_RowEditing
            AddHandler gv1.RowUpdating, AddressOf gv1_RowUpdating
            AddHandler gv1.RowUpdated, AddressOf gv1_RowUpdated
            AddHandler gv2.RowEditing, AddressOf gv2_RowEditing
            AddHandler gv2.RowUpdating, AddressOf gv2_RowUpdating
            AddHandler gv2.RowUpdated, AddressOf gv2_RowUpdated
 
            Dim sdsold, sdsnew As New SqlDataSource()
     sdsold.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
            sdsnew.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
            sdsold.ID = "sdsOld" + id
            sdsnew.ID = "sdsNew" + id
            sdsnew.SelectCommand = "Select * from table1"
            sdsold.SelectCommand = "Select * from table2"
 
 
 

            gv1.EmptyDataText = "No record(s) exist"
            gv1.ID = "GV1" + id
            gv1.Attributes.Add("runat", "server")
     'Add Values to the editRowCommand
            gv1.Attributes.Add("onRowEditing", "Values go here")
            gv1.DataSourceID = sdsnew.ID
            gv1.Visible = True
            gv1.AutoGenerateEditButton = True
     
          
     gv2.ID = "GV2" + id
            gv2.EmptyDataText = "No record(s) exist"
     gv2.Attributes.Add("runat", "server")
     'Add Values to the editRowCommand
            gv2.Attributes.Add("onRowEditing", "Values go here")
            gv2.DataSourceID = sdsold.ID
            gv2.Visible = True
            gv2.AutoGenerateEditButton = True
 
 
 
            ' Create Table
            Dim tr As New TableRow()
            ' Create column 1
            Dim td1 As New TableCell()
            td1.Controls.Add(gv2)
            ' Create column 2
            Dim td2 As New TableCell()
            ' Add control to the table cell
            td2.Controls.Add(gv1)
            ' Add cell to the row
            tr.Cells.Add(td1)
            tr.Cells.Add(td2)
            ' Add row to the table.
            tbl.Rows.Add(tr)
 
 
 

            tabContent.Controls.Add(label)
            table.Controls.Add(tbl)
            table.Controls.Add(sdsnew)
            table.Controls.Add(sdsold)
            tab.Controls.Add(tabContent)
            tab.Controls.Add(table)
 
 
 
            Me.TabContainer1.Tabs.Add(tab)
 
 
 
            gv1.DataBind()
            gv2.DataBind()
 
 
 
            id += 1
        End While
        sqlconn.Close()
 
  'Set active tab index
        Me.TabContainer1.ActiveTabIndex = 0
     End Sub
 

    Protected Sub gv1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'cast gridview to get values
        Dim tempGrid As GridView = DirectCast(sender, GridView)
 
        tempGrid.EditIndex = e.NewEditIndex
        
    End Sub
 

    Protected Sub gv2_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'cast gridview to get values
        Dim tempGrid As GridView = DirectCast(sender, GridView)
 
        tempGrid.EditIndex = e.NewEditIndex
        
    End Sub
 
    Protected Sub gv1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        Dim tempGrid As GridView = DirectCast(sender, GridView)
 Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
    End Sub
    Protected Sub gv2_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        Dim tempGrid As GridView = DirectCast(sender, GridView)
 Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
    End Sub
 
    Protected Sub gv2_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
        Dim tempGrid As GridView = DirectCast(sender, GridView)
 Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
    End Sub
 
    Protected Sub gv1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
        Dim tempGrid As GridView = DirectCast(sender, GridView)
        Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
    End Sub



Quote:
Originally Posted by jmurrayhead View Post
Indeed, if you can get RowEditing working, you should be able to get the other events working
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Tags
dynamic, gridview, rowediting, update

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
add additional footer to gridview peebman2000 .Net Development 13 May 2nd, 2008 01:50 PM
Update sql connection string in web config peebman2000 .Net Development 5 April 18th, 2008 12:23 PM
[ASP.Net/General] Create ToolTip for GridView Header jmurrayhead Code Samples 0 March 21st, 2008 02:26 PM


All times are GMT -4. The time now is 08:38 PM.



Content Relevant URLs by vBSEO 3.2.0