If your are not familiar with List visit my blog Windows Forms ListView.
DataTable dt = new DataTable("Shalvin");
DataRow dr;
DataColumn dc;
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.Columns.Add("Blog", 170);
listView1.Columns.Add("Author", 170);
dc = new DataColumn("Blog");
dt.Columns.Add(dc);
dc = new DataColumn("Author");
dt.Columns.Add(dc);
dr= dt.NewRow();
dr["Blog"] = "http://weblogs.asp.net/Scottgu/";
dr["Author"] = "Scott Guthrie";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Blog"] = "DotNetShalvin.blogspot.com";
dr["Author"] = "Shalvin";
dt.Rows.Add(dr);
listView1.Items.Clear();
To make things simple I am creating a DataTable and DataRows by code .Afterward I am iterating through the DataRows collection and adding it into ListView's SubItems collection.
private void Form1_Load(object sender, EventArgs e)
{
..
..
ListViewItem lvbl = new ListViewItem("Add new record");
listView1.Items.Add(lvbl);
}
private void listView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (listView1.FocusedItem.Text.ToString() == "Add new record")
MessageBox.Show("Add new record");
}
VB .Net
Dim dt As New DataTable("Shalvin")
Dim dr As DataRow
Dim dc As DataColumn
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("Blog", 170)
ListView1.Columns.Add("Author", 170)
dc = New DataColumn("Blog")
dt.Columns.Add(dc)
dc = New DataColumn("Author")
dt.Columns.Add(dc)
dr = dt.NewRow()
dr("Blog") = "http://weblogs.asp.net/Scottgu/"
dr("Author") = "Scott Guthrie"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("Blog") = "DotNetShalvin.blogspot.com"
dr("Author") = "Shalvin"
dt.Rows.Add(dr)
For index As Integer = 0 To dt.Rows.Count - 1
Dim dr As DataRow = dt.Rows(index)
Dim lvi As New ListViewItem(dr("Blog").ToString())
ListView1.Items.Add(lvi)
lvi.SubItems.Add(dr("Author"))
Next
Related Blog :
DataTable DataColumn and DataRow (Asp .Net)
DataGridView - Extracting the Contents of Currently Selected Row
Monday, October 27, 2008
Filling Windows Forms ListView with DataTable
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