Tuesday, March 25, 2008

DataTable DataColumn and DataRow in .Net

DataSet is an offline disconnected Data Store.
DataSet is a collection of DataTables.
A DataTable Contains DataRow and DataColumns.

The following code illustrating creating DataTable, DataRow and DataColumn. This approached can be used for creating a temporary storage of structured data.

using System.Data;

DataTable dt = new DataTable("Purchase");
DataColumn dc;
DataRow dr;


protected void Page_Load(object sender, EventArgs e)
{
dc = new DataColumn("ProductName");

//Adding the column to the Columns colluction of DataTable
dt.Columns.Add(dc);
dc = new DataColumn("Price");

//Adding the column to the Columns colluction of DataTable
dt.Columns.Add(dc);

//Adding a new row to the table
dr = dt.NewRow();
dr["ProductName"] = "Microsoft Mouse";
dr["Price"] = 400;
dt.Rows.Add(dr);

grvPurchase.DataSource = dt;
DataBind();
}









Figure 1






VB .Net

Imports System.Data

Partial Class _Default
Inherits System.Web.UI.Page


Dim dt As New DataTable("Purchase")
Dim dc As DataColumn
Dim dr As DataRow


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dc = New DataColumn("ProductName")
'Adding the column to the Columns colluction of DataTable


dt.Columns.Add(dc)
dc = New DataColumn("Price")

'Adding the column to the Columns colluction of DataTable
dt.Columns.Add(dc)


'Adding a new row to the table
dr = dt.NewRow()
dr("ProductName") = "Microsoft Mouse"
dr("Price") = 400
dt.Rows.Add(dr)
grvPurchase.DataSource = dt
DataBind()
End Sub
End Class

Windows Forms
DataBind() method is not required in the case with Windows Forms. Here I am using DataGrid and not DataGridView. The code work perfectly fine with DataGridView.
















Figure 2


DataSet and DataGrid

DataGrid is better suited to display the relationship between DataSet and DataTable.

DataSet ds = new DataSet();
DataTable dt = new DataTable("Purchase");
DataColumn dc;
DataRow dr;


private void Form1_Load(object sender, EventArgs e)
{
dc = new DataColumn("ProductName");
//Adding the column to the Columns colluction of DataTable
dt.Columns.Add(dc);
dc = new DataColumn("Price");

//Adding the column to the Columns colluction of DataTable
dt.Columns.Add(dc);
//Adding a new row to the table
dr = dt.NewRow();
dr["ProductName"] = "Microsoft Mouse";
dr["Price"] = 400;
dt.Rows.Add(dr);

ds.Tables.Add(dt);
dataGrid1.DataSource = ds;
}















Figure 3

















Figure 4

















Figure 5













Specifying the DataTable of DataSet in DataGridView

ds.Tables.Add(dt);
dataGridView1.DataSource = ds.Tables["Purchase"];

Now the result will be similar to that of Figure 2.




Building Invoice Type Application

By Invoice Type Application I mean those applications used in invoicing module wherein as the user submits temporary data it won't be committed to database. Instead it would be stored temporarily and calculations like totaling done on the local data.

In the example below as the user enters the product details its sub total will be displayed below the grid.






















Here I am using the simplified code for creating DataColums and DataRows.


DataTable dt = new DataTable("Purchase");

private void Form1_Load(object sender, EventArgs e)
{

dt.Columns.Add("ProductName", typeof(string ));
dt.Columns.Add("Price", typeof(int));
}

private void btnAdd_Click(object sender, EventArgs e)
{
int intTotal = 0;
dt.Rows.Add(txtProductName.Text, txtUnitPrice.Text );
ShowData();

foreach (DataRow dr in dt.Rows)
intTotal += Int32.Parse(dr["Price"].ToString());

lblTotal.Text = intTotal.ToString();
}

private void ShowData()
{
dataGridView1.DataSource = dt;
txtProductName.Text = "";
txtUnitPrice.Text = "";
txtProductName.Focus();
}



























Related Blogs
Filling Windows Forms ListView with DataTable
Xml with .Net

3 comments:

  1. specify how to fix this datatable into the table that reside in our database

    ReplyDelete