Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding to a DataSet to display in a DataGrid 2

Status
Not open for further replies.

ietprofessional

Programmer
Apr 1, 2004
267
US
I'm trying to create a form that has a couple fields and datagrid. What I'd like to do is all the user to enter the data via the text fields and display the data in the datagrid. If they enter data in one and click a button there will be one item in the datagrid. If they add another there will be two items in the datagrid.

Does anyone have any code that shows how to add a row to a dataset using data from a couple fields and then displayed in a datagrid?

Thanks!
 
Assuming you already have a schema in place in the data set, it's really as easy as grabbing a new row from your table, filling it out, then adding the row back to the table.
Code:
dim dr as DataRow = ds.Tables(0).NewRow
dr("field1") = TextBox1.Text
...
ds.Tables(0).Rows.Add(dr)
' and rebind ds to your dg

________________________________________
Andrew

I work for a gift card company!
 
Hi Andrew - This is what I have. It isn't working. It is replacing the first row with the new entry instead of adding a row to the table.

Code:
		DataTable dt = new DataTable();
				
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(Page.IsPostBack)
			{
				dt.Columns.Add(new DataColumn("Test"));
			}
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			DataRow anyRow = dt.NewRow();
			anyRow["Test"] = TextBox1.Text;
			dt.Rows.Add(anyRow);
			DataGrid1.DataSource = dt;
			DataGrid1.DataBind();
		}
 
Your datatable isn't being stored as a session variable, which means that its not being maintained through postbacks.

Every time you launch your form, a new Datatable dt is created...so its not that the row is overwriting the first one, its that the row is being added to a brand new data table.

Put your datatable in Session, and you'll be able to maintain all the rows that you're adding.

D'Arcy
 
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable

        If Not Page.IsPostBack Then
            dt = New DataTable
            dt.Columns.Add("Test")
            Session("DataTable") = dt
        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
        dr = CType(Session("DataTable"), DataTable).NewRow
        dr.Item("Test") = TextBox1.Text
        CType(Session("DataTable"), DataTable).Rows.Add(dr)
        DataGrid1.DataSource = Session("DataTable")
        DataGrid1.DataBind()
    End Sub
 
You're awesome JFrost!

Can you tell me why I can't do this with multiple columns?

Here is the code that I have so far:

I'm only seeing one column of data in the dgService DataGrid.

Thanks in advance!

Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ds As DataSet
        Dim dt As DataTable

        If Not Page.IsPostBack Then
            dt = New DataTable("Services")
            dt.Columns.Add("PartNumber")
            dt.Columns.Add("Application")
            dt.Columns.Add("MKey")
            dt.Columns.Add("CowLocation")
            dt.Columns.Add("ServiceType")
            ds.Tables.Add(dt)
            Session("DataSet") = ds
        End If

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim dr As DataRow
        dr = CType(Session("DataSet"), DataSet).Tables("Services").NewRow
        dr.Item("PartNumber") = txtPartNumber.Text
        dr.Item("Application") = txtApplication.Text
        dr.Item("MKey") = txtMKey.Text
        dr.Item("CowLocation") = txtCowLocation.Text
        dr.Item("ServiceType") = ddlServiceType.SelectedItem.Value
        CType(Session("DataSet"), DataSet).Tables("Services").Rows.Add(dr)
        dgServices.DataSource = Session("DataSet")
        dgServices.DataBind()

    End Sub
 
I tried your code, and I get all the columns showing
(although I had to add a line that instantiates a new dataset, otherwise your code as-is shoudn't be working)

Code:
If Not Page.IsPostBack Then
          [b]ds = New Dataset()
            dt = New DataTable("Services")

Check the properties of your datagrid. Do you have it set to create columns automatically at run time, or are you setting specifically bound columns?

D
 
Oh yeah. I left the dataset out accidently. I've been trying to tweek this code to get it to work without much success.

Here is my datagrid:

Code:
<asp:DataGrid id="dgServices" runat="server" BackColor="#0073BB" Width="301px" ForeColor="#FFCC01"
BorderStyle="Double" BorderColor="#FFCC01" Font-Size="X-Small" >
<HeaderStyle BackColor="#404040"></HeaderStyle>
</asp:DataGrid>
 
That should work fine...I just tried it, and no problems.

Are you using Visual Studio.NET? If so, open the columns collection dialog from the property window, and verify that you don't have just one bound column selected.

D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top