How to Bind a DataSet to a DataGrid

The Microsoft .Net framework allows you to quickly and easily create programs with appealing visual interfaces. For instance, you can use .Net to display data in the form of a grid using the "DataGridView" class. In order to effectively use the "DataGridView," you will need to feed it some data. You can bind the "DataGridView" to a "DataSet" using just a few lines of code. You can accomplish this project with little coding experience, and it only takes a few minutes.

Things You'll Need

  • Computer with Visual Studio 2010 installed
Show More

Instructions

    • 1

      Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File" followed by "New" and "Project." Click "Visual C#" and then "Windows Forms Application." A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.

    • 2

      Locate the panel labeled "Toolbox," which is usually located on the left-hand or right-hand side of the screen. The "Toolbox" features a list of graphical components, such as "DataGrid."

    • 3

      Locate and click on the item "DataGridView" and "DataSet" in the "Toolbox." Drag the mouse cursor over to the Windows Form, and then release the mouse button to place the "DataGrid." Repeat this for the "DataSet," which creates a pop-up window. Click "OK" to proceed.

    • 4

      Click on the tiny black arrow, which is located to the upper-right side of the "DataGridView" component. A menu opens. Click on "Add Column" to add a column.

    • 5

      Click on the menu item labeled "View," which is located on the top row of buttons in the Visual Studio editor. A menu appears. Click on "Code" to switch from graphical view mode to text mode. A text file containing your project's source code appears.

    • 6

      Locate the text that reads "InitializeComponent();." Write the code that binds data from the "DataSet" to the "DataGridView" in the space directly beneath the statement located in the previous step.

    • 7

      Create a new "DataTable," which is used by "DataSets" to store data. Write the following statement:

      DataTable table = new DataTable("table");

    • 8

      Add the table to the "DataSet" by writing the next statement:

      dataSet1.Tables.Add(table);

    • 9

      Bind the data from the "DataSet" to the "DataGridView" by issuing the following statement:

      dataGridView1.DataSource = dataSet1;

    • 10

      Execute your program by pressing the green "Play" button. A Windows Form appears, which has a "DataGridView" embedded into it.

Related Searches:

References

Comments

Related Ads

Featured