Adding datarow to datarow and datragrid

D

DotNetJunkies User

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

Ken Tucker [MVP]

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

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

Top