+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 28

Thread: update gridview dynamically

  1. #1
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    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.

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    What exactly do you mean by "Update Gridview"?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

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

    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"?

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    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
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    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
    

  6. #6
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    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
    

  7. #7
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    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.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  8. #8
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    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.

  9. #9
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Indeed, if you can get RowEditing working, you should be able to get the other events working
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  10. #10
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    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 to Thread
Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. add additional footer to gridview
    By peebman2000 in forum .NET Development
    Replies: 13
    Last Post: May 2nd, 2008, 02:50 PM
  2. Update sql connection string in web config
    By peebman2000 in forum .NET Development
    Replies: 5
    Last Post: April 18th, 2008, 01:23 PM
  3. Create ToolTip for GridView Header
    By jmurrayhead in forum .NET Code Samples
    Replies: 0
    Last Post: March 21st, 2008, 03:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO