• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1085
  • Last Modified:

dataGridView demo

can I get a demo on how to load a DataGridView  (note not dataGrid)

so far:
1. make dataSet
2. make dataTable - connect to dataSet
3. make dataColumn - connect to dataTable
4. make dataRows - connect to dataTable
5. make the dataSet the DataGridView.DataSource

  • 3
  • 2
1 Solution
Do you mean that you want to create dataset, datatable and datacolumn manually through code, and also add data manually through code (do not want to pull data from database?

Micki-WeaverAuthor Commented:
correct, all by hand, I may even do the dataGridView by hand but I can do that.
To create a DataTable (example from MSDN)

DataTable workTable = new DataTable("Customers"); // this line creates DataTable object

DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32)); // this adds first column, type integer
workCol.AllowDBNull = false; // this line forbids null value to be added to this first column
workCol.Unique = true; // this line is saying that every value in this first column will be unique

workTable.Columns.Add("CustLName", typeof(String)); // add 2nd column, type string
workTable.Columns.Add("CustFName", typeof(String)); // add 3rd column, type string
workTable.Columns.Add("Purchases", typeof(Double)); // add 4th column, type double

Now that we have created DataTable, lets add some values in it

DataRow workRow = workTable.NewRow(); // we do this to create a new row that we will later insert in DataTable

workRow["CustID"] = 1; // we add value to 1st column
workRow["CustLName"] = "Smith"; // we add value to 2nd column
workRow["CustFName"] = "John"; // we add value to 3rd column
workRow["Purchases"] = 925.30; // we add value to 4th column

workTable.Rows.Add(workRow); // we finally add this row to DataTable

To add this DataTable to DataSet, we first need to create DataSet

DataSet ds = new DataSet(); // we create new DataSet object
ds.Tables.Add(workTable); // we add DataTable object that we previously created to DataSet

Now, to set this DataTable to datagridview datasource

DataGridView1.DataSource = workTable;

or, using DataSet we just created

DataGridView1.DataSource = ds.Tables["Customers"];

Here are link were you can read more


Micki-WeaverAuthor Commented:
thanks that works
You are welcome


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now