dataGridView demo

Posted on 2007-08-02
Last Modified: 2008-02-01
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

Question by:Micki-Weaver
    LVL 18

    Expert Comment

    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?


    Author Comment

    correct, all by hand, I may even do the dataGridView by hand but I can do that.
    LVL 18

    Accepted Solution

    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


    Author Comment

    thanks that works
    LVL 18

    Expert Comment

    You are welcome


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    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!
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    733 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

    23 Experts available now in Live!

    Get 1:1 Help Now