Solved

C# how to write to DataTable

Posted on 2013-02-05
6
416 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
6 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
Comment Utility
0
 
LVL 16

Assisted Solution

by:Rose Babu
Rose Babu earned 250 total points
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:Srinivasulu Muppala
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now