Home > Archive > ASP .NET > December 2004 > datagrid row problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
datagrid row problem
|
|
|
| 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>
| |
| John Saunders 2004-12-30, 8:57 pm |
| "Bala" <Bala@discussions.microsoft.com> wrote in message
news:C6D46476-1E37-40BD-827F-84BCBE6A4243@microsoft.com...
> 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
| |
|
| its always adding in the first row. its not adding second row.
bala
"John Saunders" wrote:
> "Bala" <Bala@discussions.microsoft.com> wrote in message
> news:C6D46476-1E37-40BD-827F-84BCBE6A4243@microsoft.com...
>
> I don't see why your code wouldn't work. Does the new row not show up at
> all?
>
> John Saunders
>
>
>
| |
| John Saunders 2004-12-30, 8:57 pm |
| "Bala" <Bala@discussions.microsoft.com> wrote in message
news:B0C0306C-5834-487A-A24B-01E18E609071@microsoft.com...
> 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...
[color=darkred]
> "John Saunders" wrote:
>
| |
|
| if possible can you post the code. i am new to asp.net.
bala
"John Saunders" wrote:
> "Bala" <Bala@discussions.microsoft.com> wrote in message
> news:B0C0306C-5834-487A-A24B-01E18E609071@microsoft.com...
>
> 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 2004-12-30, 8:57 pm |
| "Bala" <Bala@discussions.microsoft.com> wrote in message
news:E97C2464-F3C0-4CBB-AC30-B7ABB555A06D@microsoft.com...
> 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
|
|
|
|
|