How to put data not coming from a database in a DataGrid

URL: http://www.poirrier.be/~jean-etienne/info/csharp/datagrid.php

Article written on February 16th, 2006

A DataGrid accepts many other things than data from a database. Valid data sources includes database-related elements (DataTable, DataView, DataSet, DataViewManager) but also single dimension arrays and any component that implements the IListSource or IList interface.

A simple example would have been one with a single dimension arrays. But, here, I want to display pairs of elements. Each pair is composed of one property and one value for this property (for example, "property1" <-> "value1", "property2" <-> "value2", ...). For that purpose, we will use a DataTable (ok, it's usually database-related).

In this example (in C#), I will use an object (from a simple class called Hello) and I will fill the datagrid with pairs coming from the object.

The Form

The main form is very simple. I put two buttons:

  • one button (btPut) that will retrieve data and put it in the grid;
  • one button (btQuit) that will close the application.

Of course, I also put a DataGrid (myDG). The main form looks like this one:

Form DataGrid

THe Hello class and how to use it

The Hello class is also very simple. In the constructor, we are creating a new DataTable with two new columns. With a for, we fill our data in the DataTable by adding rows. Finally, we put the table in a DataView.

public Hello()
{
	// Constructor -> Initialize table
	myTable = new DataTable("myTable");
	colItem1 = new DataColumn("Properties",Type.GetType("System.String"));
	colItem2 = new DataColumn("Values",Type.GetType("System.String"));

	myTable.Columns.Add(colItem1);
	myTable.Columns.Add(colItem2);

	for(int i = 0; i <5; i++)
	{
		NewRow = myTable.NewRow();
		NewRow["Properties"] = "Property " + i.ToString();
		NewRow["Values"] = "Value " + i.ToString();
		myTable.Rows.Add(NewRow);
	}
	myView = new DataView(myTable);
}

Of course, to be able to use this constructor, you need to declare these variables before (outside the constructor):

private DataTable myTable;
private DataColumn colItem1;
private DataColumn colItem2;
private DataRow NewRow;
private DataView myView;

Then, we have the only method. This method prepares the DataView (not to allow the edition of the data) and returns it.

public DataView getView()
{
	myView.AllowDelete = false;
	myView.AllowEdit = false;
	myView.AllowNew = false;
	return myView;
}

In the code associated with the Form, I simply declare an object from the Hello class with:

Hello bonjour; // bonjour = Hello, in French

Filling data from the object

Finally, when the user will click on the btPut button, the code behind this button will fill the datagrid with the setDataBinding method:

private void btPut_Click(object sender, System.EventArgs e)
{
	myDG.SetDataBinding(bonjour.getView(), "");
}

In fact, the second argment should be a DataMember but the documentation said to put an empty string with a DataView (our case, here) or a DataTable.

The final application

Finally, the application should look like this:

DataGrid application running

You can download the source code here (18ko).