PC Review


Reply
Thread Tools Rate Thread

Adding datarow to datarow and datragrid

 
 
DotNetJunkies User
Guest
Posts: n/a
 
      15th Jul 2004
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">
<aspataGrid 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>
</aspataGrid>
</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.
 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      15th Jul 2004
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:(E-Mail Removed)...
> 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">
> <aspataGrid 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>
> </aspataGrid>
> </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.



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is DataRow[string] uses DataRow[int] and DataRow[int] much efficient than DataRow[string]? Ryan Liu Microsoft C# .NET 3 2nd Feb 2008 06:14 AM
Distinguish 2 situations when a DataRow in Detached state; Or why not make the data row intact after being added then removed from a dataRow collection? Ryan Liu Microsoft Dot NET 0 7th Jun 2006 03:04 PM
Distinguish 2 situations when a DataRow in Detached state; Or why not make the data row intact after being added then removed from a dataRow collection? Ryan Liu Microsoft C# .NET 0 7th Jun 2006 02:14 AM
create new DataRow, init fields, then set DataRow to UnChanged? Les Caudle Microsoft ADO .NET 2 4th May 2004 05:42 PM
Re: Casting Data.Datarow to a strongly typed datarow CJ Taylor Microsoft VB .NET 4 21st Apr 2004 08:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:52 AM.