Adding to a Bound DataGrid: What the @#$!% am I doing wrong!

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Adding to a Bound DataGrid: What the @#$!% am I doing wrong!

    I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried
    everything and I am at a loss. The using goes into add mode with the add
    button adds his data then updates with the update button, seems simple.
    I am using ALL visual controls (supposedly to simplify things. If I was not
    using the visual controls and calling an ExecuteNonQuery no prob.
    Please look at my code and tell me what I am doing wrong. Also, what are the
    advatages and disadvantages of using the visual controls and typed Datasets.
    THANKS SO MUCH!!!!


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    If IsPostBack Then
    If Not (Request.Form("btnAdd") Is Nothing) Then
    Add()
    End If
    Else
    BindGrid()
    End If
    End Sub


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnUpdate.Click
    Update()
    End Sub



    ' Bind the database to the table
    Private Sub BindGrid()
    vdaPasswords.Fill(dsPasswords1)

    If Session("dsPasswords") Is Nothing Then
    Session("dsPasswords") = dsPasswords1
    End If

    DataGrid1.DataSource = dsPasswords1
    DataGrid1.DataBind()

    End Sub

    ' The "Add" sub
    Private Sub Add()

    Dim dr As dsPasswords.PasswordsRow

    If dsPasswords1 Is Nothing Then
    dsPasswords1 = Session("dsPasswords")
    End If

    dr = dsPasswords1.Tables("Passwords").NewRow()
    dr("NUMBER") = 80
    dr("USERNAME") = "SDASD"
    dsPasswords1.Tables("Passwords").Rows.InsertAt(dr, 0)
    DataGrid1.EditItemIndex = 0
    BindGrid()

    End Sub

    ' User clicked "update".
    Sub Update()
    ' Either insert a new row or update the existing row here,
    ' depending on whether adding or not.
    DataGrid1.EditItemIndex = -1
    BindGrid()
    End Sub




    Aaron Ackerman Guest

  2. Similar Questions and Discussions

    1. UserControl inside of datagrid - loses its viewstate when datagrid is re-bound on postback
      I have a simple usercontrol, a datepicker which contains 3 dropdownlist , it resides inside a datagrid column and i set the selecteddate property of...
    2. bound columns of a datagrid
      i have a datagrid with few bound columns.i want to make the first column clickable.can i make a bound column clickable and write a handler for...
    3. Dynamically Adding Bound Columns Won't Sort
      All- I'm basically creating a user control which contains a datagrid that will be used throughout our application by all pages. My user control...
    4. Bound DataGrid using nested XML
      Hi, I am trying to use a datagrid control to display hierarchical data in a flat format essentially in a master-detail relationship. i.e. ...
    5. Sort on bound DataGrid
      I have create a page with SQLDateAdapter, SQLConnetion, DataSet and Datagrid. I have bound the Datagrid at design time. I could not find a way to...
  3. #2

    Default RE: Adding to a Bound DataGrid: What the @#$!% am I doing wrong!

    Hi Aaron,

    I have checked your code carefully, and found that the problem should be
    vdaPasswords. In BindGrid, dsPasswords1 is filled again, so the new datarow
    lost. Please see my comments below:


    Private Sub Add()

    Dim dr As dsPasswords.PasswordsRow

    If dsPasswords1 Is Nothing Then
    dsPasswords1 = Session("dsPasswords")
    End If

    dr = dsPasswords1.Tables("Passwords").NewRow()
    dr("NUMBER") = 80
    dr("USERNAME") = "SDASD"
    dsPasswords1.Tables("Passwords").Rows.InsertAt(dr, 0) //**** Once
    you click Add button, a datarow was inserted into the dsPasswords1
    DataGrid1.EditItemIndex = 0
    BindGrid()//****Then call BindGrid

    End Sub

    Private Sub BindGrid()
    vdaPasswords.Fill(dsPasswords1)//****But here, dsPasswords1 is filled
    again, so the new datarow lost.

    If Session("dsPasswords") Is Nothing Then
    Session("dsPasswords") = dsPasswords1
    End If

    DataGrid1.DataSource = dsPasswords1
    DataGrid1.DataBind()

    End Sub

    So you should update vdaPasswords after you add a new datarow to
    dsPasswords1.

    Please check these articles for more information:
    Updating the Database with a DataAdapter and the DataSet
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm[/url]
    l/cpconupdatingdatabasewithdataadapterdataset.asp

    Working with a Typed DataSet
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm[/url]
    l/cpconworkingwithtypeddataset.asp


    Hope this helps.

    Best Regards,
    Lewis Wang

    This posting is provided "AS IS" with no warranties, and confers no rights.


    --------------------
    | From: "Aaron Ackerman" <no@spam.com>
    | Subject: Adding to a Bound DataGrid: What the @#$!% am I doing wrong!
    | Date: Wed, 23 Jul 2003 12:40:21 -0400
    | Lines: 76
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
    | Message-ID: <eNxk3kTUDHA.2008@TK2MSFTNGP11.phx.gbl>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: ric-64-83-27-134-serial-sta.t1.cavtel.net 64.83.27.134
    | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
    | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:161436
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried
    | everything and I am at a loss. The using goes into add mode with the add
    | button adds his data then updates with the update button, seems simple.
    | I am using ALL visual controls (supposedly to simplify things. If I was
    not
    | using the visual controls and calling an ExecuteNonQuery no prob.
    | Please look at my code and tell me what I am doing wrong. Also, what are
    the
    | advatages and disadvantages of using the visual controls and typed
    Datasets.
    | THANKS SO MUCH!!!!
    |
    |
    | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Load
    | 'Put user code to initialize the page here
    | If IsPostBack Then
    | If Not (Request.Form("btnAdd") Is Nothing) Then
    | Add()
    | End If
    | Else
    | BindGrid()
    | End If
    | End Sub
    |
    |
    | Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
    | System.EventArgs)
    | End Sub
    |
    | Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles btnUpdate.Click
    | Update()
    | End Sub
    |
    |
    |
    | ' Bind the database to the table
    | Private Sub BindGrid()
    | vdaPasswords.Fill(dsPasswords1)
    |
    | If Session("dsPasswords") Is Nothing Then
    | Session("dsPasswords") = dsPasswords1
    | End If
    |
    | DataGrid1.DataSource = dsPasswords1
    | DataGrid1.DataBind()
    |
    | End Sub
    |
    | ' The "Add" sub
    | Private Sub Add()
    |
    | Dim dr As dsPasswords.PasswordsRow
    |
    | If dsPasswords1 Is Nothing Then
    | dsPasswords1 = Session("dsPasswords")
    | End If
    |
    | dr = dsPasswords1.Tables("Passwords").NewRow()
    | dr("NUMBER") = 80
    | dr("USERNAME") = "SDASD"
    | dsPasswords1.Tables("Passwords").Rows.InsertAt(dr, 0)
    | DataGrid1.EditItemIndex = 0
    | BindGrid()
    |
    | End Sub
    |
    | ' User clicked "update".
    | Sub Update()
    | ' Either insert a new row or update the existing row here,
    | ' depending on whether adding or not.
    | DataGrid1.EditItemIndex = -1
    | BindGrid()
    | End Sub
    |
    |
    |
    |
    |

    Lewis Wang [MSFT] Guest

  4. #3

    Default Re: Adding to a Bound DataGrid: What the @#$!% am I doing wrong!

    Hi Aaron,

    Thank you for your reply. I wrote a sample code and it works fine on my
    machine. You can test it on your machine to see if it helps.

    Please let me know if it helps. Thank you.

    Lewis,

    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    | From: "Aaron Ackerman" <no@spam.com>
    | References: <eNxk3kTUDHA.2008@TK2MSFTNGP11.phx.gbl>
    | Subject: Re: Adding to a Bound DataGrid: What the @#$!% am I doing wrong!
    | Date: Mon, 28 Jul 2003 09:50:10 -0400
    | Lines: 85
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
    | Message-ID: <O9oDJ9QVDHA.3232@tk2msftngp13.phx.gbl>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: ric-64-83-27-134-serial-sta.t1.cavtel.net 64.83.27.134
    | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
    | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:162588
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | That doesn't work.
    |
    | "Aaron Ackerman" <no@spam.com> wrote in message
    | news:eNxk3kTUDHA.2008@TK2MSFTNGP11.phx.gbl...
    | > I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried
    | > everything and I am at a loss. The using goes into add mode with the add
    | > button adds his data then updates with the update button, seems simple.
    | > I am using ALL visual controls (supposedly to simplify things. If I was
    | not
    | > using the visual controls and calling an ExecuteNonQuery no prob.
    | > Please look at my code and tell me what I am doing wrong. Also, what are
    | the
    | > advatages and disadvantages of using the visual controls and typed
    | Datasets.
    | > THANKS SO MUCH!!!!
    | >
    | >
    | > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | > System.EventArgs) Handles MyBase.Load
    | > 'Put user code to initialize the page here
    | > If IsPostBack Then
    | > If Not (Request.Form("btnAdd") Is Nothing) Then
    | > Add()
    | > End If
    | > Else
    | > BindGrid()
    | > End If
    | > End Sub
    | >
    | >
    | > Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
    | > System.EventArgs)
    | > End Sub
    | >
    | > Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
    | > System.EventArgs) Handles btnUpdate.Click
    | > Update()
    | > End Sub
    | >
    | >
    | >
    | > ' Bind the database to the table
    | > Private Sub BindGrid()
    | > vdaPasswords.Fill(dsPasswords1)
    | >
    | > If Session("dsPasswords") Is Nothing Then
    | > Session("dsPasswords") = dsPasswords1
    | > End If
    | >
    | > DataGrid1.DataSource = dsPasswords1
    | > DataGrid1.DataBind()
    | >
    | > End Sub
    | >
    | > ' The "Add" sub
    | > Private Sub Add()
    | >
    | > Dim dr As dsPasswords.PasswordsRow
    | >
    | > If dsPasswords1 Is Nothing Then
    | > dsPasswords1 = Session("dsPasswords")
    | > End If
    | >
    | > dr = dsPasswords1.Tables("Passwords").NewRow()
    | > dr("NUMBER") = 80
    | > dr("USERNAME") = "SDASD"
    | > dsPasswords1.Tables("Passwords").Rows.InsertAt(dr, 0)
    | > DataGrid1.EditItemIndex = 0
    | > BindGrid()
    | >
    | > End Sub
    | >
    | > ' User clicked "update".
    | > Sub Update()
    | > ' Either insert a new row or update the existing row here,
    | > ' depending on whether adding or not.
    | > DataGrid1.EditItemIndex = -1
    | > BindGrid()
    | > End Sub
    | >
    | >
    | >
    | >
    |
    |
    |
    Lewis Wang [MSFT] Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139