datagrid row problem

Discussion in 'ASP .Net' started by =?Utf-8?B?QmFsYQ==?=, Dec 30, 2004.

  1. Hi,

    i have sample page contain 2 textboxes,1 datagrid and 1 command button.

    when ever i click the command button the text box values should goes to
    datagrid new row. but its not working. can anyone please check the code.

    Thanks
    bala

    code:

    <%@ Import Namespace="System.Data" %>
    <%@ Page Language="VB" AutoEventWireup="True" %>

    <HTML>
    <script language="VB" runat="server">
    Dim Cart As DataTable
    Dim CartView As DataView
    dim i as integer

    Sub Page_Load(sender As Object, e As EventArgs)
    Cart = New DataTable()
    Cart.Columns.Add(New DataColumn("Item", GetType(String)))
    Cart.Columns.Add(New DataColumn("Price", GetType(String)))

    CartView = New DataView(Cart)
    ShoppingCart.DataSource = CartView
    ShoppingCart.DataBind()

    End Sub 'Page_Load

    Sub add_click(sender As Object, e As System.EventArgs)
    Dim dr As DataRow = Cart.NewRow()
    dr(0) = txt1.text
    dr(1) = txt2.text
    Cart.Rows.Add(dr)

    ShoppingCart.DataBind()
    End Sub 'Grid_CartCommand

    </script>
    <body>
    <form runat="server" ID="Form1">
    <h3>DataGrid Columns Example</h3>
    <table cellpadding="5">
    <tr valign="top">
    <td> <asp:TextBox ID=txt1 Runat=server ></asp:TextBox></td>
    <td> <asp:TextBox ID="txt2" Runat=server ></asp:TextBox></td>
    <td>
    <b>Product List</b>
    <asp:Button ID=add Runat =server OnClick ="add_click" ></asp:Button>
    </td>
    <td>
    <b>Shopping Cart</b>
    <asp:DataGrid id="ShoppingCart" runat="server" BorderColor="black"
    BorderWidth="1" GridLines="Both"
    ShowFooter="false" CellPadding="3" CellSpacing="0">
    <HeaderStyle BackColor="#00aaaa"></HeaderStyle>
    </asp:DataGrid>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>
     
    =?Utf-8?B?QmFsYQ==?=, Dec 30, 2004
    #1
    1. Advertising

  2. "Bala" <> wrote in message
    news:...
    > Hi,
    >
    > i have sample page contain 2 textboxes,1 datagrid and 1 command button.
    >
    > when ever i click the command button the text box values should goes to
    > datagrid new row. but its not working. can anyone please check the code.


    I don't see why your code wouldn't work. Does the new row not show up at
    all?

    John Saunders
     
    John Saunders, Dec 30, 2004
    #2
    1. Advertising

  3. its always adding in the first row. its not adding second row.

    bala

    "John Saunders" wrote:

    > "Bala" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > i have sample page contain 2 textboxes,1 datagrid and 1 command button.
    > >
    > > when ever i click the command button the text box values should goes to
    > > datagrid new row. but its not working. can anyone please check the code.

    >
    > I don't see why your code wouldn't work. Does the new row not show up at
    > all?
    >
    > John Saunders
    >
    >
    >
     
    =?Utf-8?B?QmFsYQ==?=, Dec 30, 2004
    #3
  4. "Bala" <> wrote in message
    news:...
    > its always adding in the first row. its not adding second row.


    You are recreating the DataTable on each PostBack. After you add the first
    row, you come back and create an empty DataTable, to which you add the
    second row...

    > "John Saunders" wrote:
    >
    >> "Bala" <> wrote in message
    >> news:...
    >> > Hi,
    >> >
    >> > i have sample page contain 2 textboxes,1 datagrid and 1 command button.
    >> >
    >> > when ever i click the command button the text box values should goes to
    >> > datagrid new row. but its not working. can anyone please check the
    >> > code.

    >>
    >> I don't see why your code wouldn't work. Does the new row not show up at
    >> all?
    >>
    >> John Saunders
    >>
    >>
    >>
     
    John Saunders, Dec 30, 2004
    #4
  5. if possible can you post the code. i am new to asp.net.

    bala

    "John Saunders" wrote:

    > "Bala" <> wrote in message
    > news:...
    > > its always adding in the first row. its not adding second row.

    >
    > You are recreating the DataTable on each PostBack. After you add the first
    > row, you come back and create an empty DataTable, to which you add the
    > second row...
    >
    > > "John Saunders" wrote:
    > >
    > >> "Bala" <> wrote in message
    > >> news:...
    > >> > Hi,
    > >> >
    > >> > i have sample page contain 2 textboxes,1 datagrid and 1 command button.
    > >> >
    > >> > when ever i click the command button the text box values should goes to
    > >> > datagrid new row. but its not working. can anyone please check the
    > >> > code.
    > >>
    > >> I don't see why your code wouldn't work. Does the new row not show up at
    > >> all?
    > >>
    > >> John Saunders
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?QmFsYQ==?=, Dec 30, 2004
    #5
  6. "Bala" <> wrote in message
    news:...
    > if possible can you post the code. i am new to asp.net.


    This should work, assuming that you don't need to put the data into a
    database:

    Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    #Region " Web Form Designer Generated Code "
    ' ...
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    #End Region

    Protected table As DataTable

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Session("table") Is Nothing Then
    table = New DataTable("table")
    table.Columns.Add("column1", GetType(String))
    table.Columns.Add("column2", GetType(String))
    Session("table") = table
    Else
    table = DirectCast(Session("table"), DataTable)
    End If

    If Not Page.IsPostBack Then
    BindGrid()
    End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click
    Dim dr As DataRow = table.NewRow()
    dr("column1") = TextBox1.Text.Trim()
    dr("column2") = TextBox2.Text.Trim()
    table.Rows.Add(dr)
    '
    BindGrid()
    End Sub

    ' Separate sub is useful in case this gets fancier,
    ' with a DataView and sorting, for instance
    Private Sub BindGrid()
    DataGrid1.DataSource = table
    DataGrid1.DataBind()
    End Sub
    End Class
     
    John Saunders, Dec 30, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig
    Replies:
    2
    Views:
    2,878
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Nov 2, 2004
  2. =?Utf-8?B?U3VyZXNo?=
    Replies:
    1
    Views:
    468
    Saravana
    Nov 22, 2004
  3. Replies:
    0
    Views:
    363
  4. Replies:
    0
    Views:
    422
  5. D
    Replies:
    0
    Views:
    217
Loading...

Share This Page