How to Bind DataTable to ListView in .Net

The Microsoft .Net Framework is a suite of software tools intended to make application development efficient. You can build a graphical user interface or windowed application, without any programming experience. For example, you can create a form that displays a box of data using the "ListView" object. If you want some programming experience, you can then load it some data using the "DataTable" object. Then your program will display data from the "DataTable" in the "ListView."

Things You'll Need

  • Visual Studio 2010
Show More

Instructions

    • 1

      Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File," "New" and "Project" and click "Visual C#" and "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 tab labeled "Toolbox," which is on the right side of the main editor window. The "Toolbox" lists all of the graphical user interface objects you can add to the Windows Form. Select "ListView" and click on it.

    • 3

      Drag the mouse pointer over to the Windows Form and release the mouse button. The "ListView" is immediately placed on the Windows Form.

    • 4

      Click "View" from the main Visual Studio menu, located on the top row of the program. Select "Code" from the menu that appears. The main editor window now displays source code.

    • 5

      Locate the line of text that states "InitializeComponent." All of your code will go directly below this line.

    • 6

      Write the following code to create a "DataTable" object:

      DataTable table = new DataTable();

    • 7

      Write the following code to add a column and a row to the table:

      table.Columns.Add("Column", typeof(int));

      table.Rows.Add(25);

    • 8

      Write the following code to create a "Binding," which will use the table as the bound item.

      Binding b = new Binding("name", table, "member");

    • 9

      Write the following code to bind the add to the "ListView."

      listView1.DataBindings.Add(b);

    • 10

      Execute the code by pressing the green "Play" button. A Windows Form appears. It has a "ListView" that displays the table of data you created programmatically.

Related Searches:

References

Comments

Related Ads

Featured