By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Adding datarow to datarow and datragrid

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.