468,785 Members | 1,691 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

Adding datarow to datarow and datragrid

I'm trying create a single page app that allows the user to enter text (Serial) via a form and submit and populate that value into a datagrid via a datatable.

It works for ONE value but each time a new row is added, it replaces the 1st.

Somehow I'm reinitalizing the table, but not sure what I'm doing wrong because I'm very new at this stuff.....

Thanks
Devon
:>

Code Here

<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<script runat="server">

' Create a New Table
Dim tblNewBox As DataTable = new DataTable("tblNewBox")
Dim colSerialNO as DataColumn
Dim mydataSet as New Dataset()

Sub Page_Load(Sender As Object, E As EventArgs)

'Create a New Data Column in the Table
colSerialNO = New DataColumn()
colSerialNO.Datatype = System.Type.GetType("System.String")
colSerialNO.ColumnName = "SerialNo"
colSerialNO.ReadOnly = False
colSerialNO.Unique = False
tblNewBox.Columns.Add(colSerialNO)
myDataSet.Tables.Add(tblNewBox)

End Sub

Sub AddRow_Click_1(sender As Object, e As EventArgs)

'Create several Rows in the Table
Dim TempRow as DataRow
TempRow = tblNewBox.newRow()
TempRow.Item("SerialNo") = TextBox1.text
tblNewBox.Rows.Add(TempRow)
' Bind the Dataset to the Datagrid
DataGrid1.datasource = myDataSet
dataGrid1.Databind()

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p align="center">
&nbsp;
</p>
<p align="center">
<font face="Arial" size="2"></font>
</p>
<p align="center">
<font face="Arial" size="2">
<br />
</font>
</p>
<p align="center">
<font face="Arial" size="2"></font>
</p>
<font face="Arial" size="2">
<p align="center">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button id="Button1" onclick="addRow_Click_1" runat="server" Text="Add Item to Table"></asp:Button>
</p>
<p align="center">
</p>
<p align="center">
<asp:DataGrid id="DataGrid1" runat="server" ForeColor="Black" CellPadding="2" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" GridLines="None">
<FooterStyle backcolor="Tan"></FooterStyle>
<HeaderStyle font-bold="True" backcolor="Tan"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="DarkSlateBlue" backcolor="PaleGoldenrod"></PagerStyle>
<SelectedItemStyle forecolor="GhostWhite" backcolor="DarkSlateBlue"></SelectedItemStyle>
<AlternatingItemStyle backcolor="PaleGoldenrod"></AlternatingItemStyle>
</asp:DataGrid>
</p>
</font>
</form>
</body>
</html>
---
Posted using Wimdows.net NntpNews Component -

Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching.
Nov 20 '05 #1
1 2659
Hi,

Only create the new datacolumn when you load the page.
Sub Page_Load(Sender As Object, E As EventArgs) if not me.ispostback then
'Create a New Data Column in the Table
colSerialNO = New DataColumn()
colSerialNO.Datatype = System.Type.GetType("System.String")
colSerialNO.ColumnName = "SerialNo"
colSerialNO.ReadOnly = False
colSerialNO.Unique = False
tblNewBox.Columns.Add(colSerialNO)
myDataSet.Tables.Add(tblNewBox)
end if

End Sub
Ken
------------------------
"DotNetJunkies User" <User@-NOSPAM-DotNetJunkies.com> wrote in message
news:eG**************@TK2MSFTNGP09.phx.gbl... I'm trying create a single page app that allows the user to enter text
(Serial) via a form and submit and populate that value into a datagrid via
a datatable.

It works for ONE value but each time a new row is added, it replaces the
1st.

Somehow I'm reinitalizing the table, but not sure what I'm doing wrong
because I'm very new at this stuff.....

Thanks
Devon
:>

Code Here

<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<script runat="server">

' Create a New Table
Dim tblNewBox As DataTable = new DataTable("tblNewBox")
Dim colSerialNO as DataColumn
Dim mydataSet as New Dataset()

Sub Page_Load(Sender As Object, E As EventArgs)

'Create a New Data Column in the Table
colSerialNO = New DataColumn()
colSerialNO.Datatype = System.Type.GetType("System.String")
colSerialNO.ColumnName = "SerialNo"
colSerialNO.ReadOnly = False
colSerialNO.Unique = False
tblNewBox.Columns.Add(colSerialNO)
myDataSet.Tables.Add(tblNewBox)

End Sub

Sub AddRow_Click_1(sender As Object, e As EventArgs)

'Create several Rows in the Table
Dim TempRow as DataRow
TempRow = tblNewBox.newRow()
TempRow.Item("SerialNo") = TextBox1.text
tblNewBox.Rows.Add(TempRow)
' Bind the Dataset to the Datagrid
DataGrid1.datasource = myDataSet
dataGrid1.Databind()

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p align="center">
&nbsp;
</p>
<p align="center">
<font face="Arial" size="2"></font>
</p>
<p align="center">
<font face="Arial" size="2">
<br />
</font>
</p>
<p align="center">
<font face="Arial" size="2"></font>
</p>
<font face="Arial" size="2">
<p align="center">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button id="Button1" onclick="addRow_Click_1" runat="server" Text="Add
Item to Table"></asp:Button>
</p>
<p align="center">
</p>
<p align="center">
<asp:DataGrid id="DataGrid1" runat="server" ForeColor="Black"
CellPadding="2" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" GridLines="None">
<FooterStyle backcolor="Tan"></FooterStyle>
<HeaderStyle font-bold="True" backcolor="Tan"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="DarkSlateBlue"
backcolor="PaleGoldenrod"></PagerStyle>
<SelectedItemStyle forecolor="GhostWhite"
backcolor="DarkSlateBlue"></SelectedItemStyle>
<AlternatingItemStyle backcolor="PaleGoldenrod"></AlternatingItemStyle>
</asp:DataGrid>
</p>
</font>
</form>
</body>
</html>
---
Posted using Wimdows.net NntpNews Component -

Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup
engine supports Post Alerts, Ratings, and Searching.

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jim Heavey | last post: by
10 posts views Thread by Trevor | last post: by
16 posts views Thread by Geoff Jones | last post: by
2 posts views Thread by Flack | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.