Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# how to write to DataTable

Posted on 2013-02-05
6
Medium Priority
?
432 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 16

Assisted Solution

by:Rose Babu
Rose Babu earned 1000 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

877 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