Solved

C# how to write to DataTable

Posted on 2013-02-05
6
428 Views
Last Modified: 2013-02-10
I have created a DataSet and a DataTable inside my C# application so I can create a report.  I do not have a SQL database or an Access database.  I want to know how to pass the information from a text box and/or list box to the DataTable that is inside my application.
0
Comment
Question by:millerje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Assisted Solution

by:Rose Babu
Rose Babu earned 250 total points
ID: 38858780
Have a look on the code below to add data into DataTable.

Controls used in the form

//TextBox txtData
//ListBox lstBoxData

//Button btnAdditToDt
//Button btnAdditToList
//Button btnAddLstDataToGrid

//DataGridView dataGridView1

Add Data into DataTable Form Design
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinAppCSHARP
{
    public partial class frmEETxtToDt : Form
    {
        public frmEETxtToDt()
        {
            InitializeComponent();
        }

		// Initialise DataTable
        DataTable dt = new DataTable();
        DataRow dr;

        private void frmEETxtToDt_Load(object sender, EventArgs e)
        {
			// Add a temporary column
            dt.Columns.Add("Data");

            BindGrid();
        }

        void BindGrid()
        {
            dataGridView1.Columns.Clear();
            dataGridView1.DataSource = null;

            dataGridView1.DataSource = dt;
        }
		
		// Add TextBox value to DataTable
        private void btnAdditToDt_Click(object sender, EventArgs e)
        {			
            if (txtData.Text.Trim().Length > 0)
            {
                dr = dt.NewRow();
                dr["Data"] = txtData.Text;

                dt.Rows.Add(dr);
            }

            BindGrid();
        }

		// Add TextBox value to ListBox
        private void btnAdditToList_Click(object sender, EventArgs e)
        {
            if (txtData.Text.Trim().Length > 0)
            {
                lstBoxData.Items.Add(txtData.Text.Trim());
            }
        }

		// Populate ListBox value to DataGridView
        private void btnAddLstDataToGrid_Click(object sender, EventArgs e)
        {
            dt.Rows.Clear();

            for (int i = 0; i < lstBoxData.Items.Count - 1; i++)
            {
                dr = dt.NewRow();
                dr["Data"] = lstBoxData.Items[i].ToString();

                dt.Rows.Add(dr);
            }

            BindGrid();
        }
    }
}

Open in new window

Hope this code will help you a lot.
0
 
LVL 4

Expert Comment

by:Srinivasulu Muppala
ID: 38858834
using System;
using System.Data;

class Program
{
    static void Main()
    {
      // Create two DataTable instances.
      DataTable table1 = new DataTable("patients");
      table1.Columns.Add("name");
      table1.Columns.Add("id");
      table1.Rows.Add("sam", 1);
      table1.Rows.Add("mark", 2);

      DataTable table2 = new DataTable("medications");
      table2.Columns.Add("id");
      table2.Columns.Add("medication");
      table2.Rows.Add(1, "atenolol");
      table2.Rows.Add(2, "amoxicillin");

      // Create a DataSet and put both tables in it.
      DataSet set = new DataSet("office");
      set.Tables.Add(table1);
      set.Tables.Add(table2);

      // Visualize DataSet.
      Console.WriteLine(set.GetXml());
    }
}
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 4

Expert Comment

by:Srinivasulu Muppala
ID: 38858848
if you want to store data whatever added in data table(s) use this code

//it will write xml file
set.WriteXml("filename.xml");

//it will read xml file
ds.ReadXml("filename.xml");

rebind the dataset once you read xml file, so you can load saved data.
0
 
LVL 1

Author Comment

by:millerje
ID: 38860043
I think all the above information is good.  I want to make sure my original question was clear though.  In my project I have added a new item and selected "dataset" and named it DataSet_SysInfo.xsd.  In the dataset design view I created a "DataTable" and named it AssetInfo and added columns to it.  I have then designed my report.rdlc to point to that datatable.  So on my report form, in the report viewer I pointed it to report.rdlc. So how to I add information to the datatable I have alread created in design view?
0
 
LVL 9

Accepted Solution

by:
rg20 earned 250 total points
ID: 38873593
If it gets stored in the datatable properly, then you could use
DataTable dt = results.tables[0];
then cycle through the rows

something like
DataTable table = GetTable(); // Get the data table.
      foreach (DataRow row in table.Rows) // Loop over the rows.
      {
          Console.WriteLine("--- Row ---"); // Print separator.
          foreach (var item in row.ItemArray) // Loop over the items.
          {
            Console.Write("Item: "); // Print label.
            Console.WriteLine(item); // Invokes ToString abstract method.
          }
      }
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question