datagrid row problem

G

Guest

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>
 
J

John Saunders

Bala said:
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
 
J

John Saunders

Bala said:
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...
 
J

John Saunders

Bala said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top