This is a mirror of official site: http://jasper-net.blogspot.com/

Hierarchical Data Into DataGridView in C#

| Sunday, February 26, 2012
Let us try a new kind of entry for DataGridViews.

What kind of new entry am I talking about?

I am talking about the hierarchical entry.

Is it possible that we can enter hierarchical entry into the DataGrdiView?

Yes of course, it's possible.

How?

Let's see.

Ok. First of all, what kind of hierarchical entry will we enter into the DataGridView?

See the following structure.


We will enter these hierarchical entries into the DataGridView.

Now for the coding part.

First of all we have to enter the columns into the DatGridView.
 

myDataGridView.Columns.Add("myColumn1", "Roll Number");
myDataGridView.Columns.Add("myColumn2", "Course");
myDataGridView.Columns.Add("myColumn3", "Subject");
myDataGridView.Columns.Add("myColumn4", "Marks(Out of 100)");

Here I have creating the four columns, but you can do as you need to.

After that we have to set the DefaultCellStyle for the DataGridView to display the entry in the hierarchical manner.

For that you have to use the WrapMode property. Using the WrapMode Property we can enter the data into the new line in the particular cell.

For that you have to set that WrapMode property as True like below:
 

myDataGridView.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

myDataGridView.Columns[3].DefaultCellStyle.WrapMode = DataGridViewTriState.True;


In our case we have last the columns (column 2 & column 3) in which we will use that WrapMode property as true.

Now start entering the data into the DataGridView one by one.
 

myDataGridView.Rows.Add(new object[] { 1, "Course 1","Sub 1\nSub 2\nSub3", "90\n95\n85" });
myDataGridView.Rows.Add(new object[] { 2, "Course 2", "Sub 1\nSub 2\nSub3", "67\n57\n84" });
myDataGridView.Rows.Add(new object[] { 3, "Course 3", "Sub 1\nSub 2\nSub3", "64\n80\n92" });
myDataGridView.Rows.Add(new object[] { 4, "Course 4", "Sub 1\nSub 2\nSub3", "94\n90\n99" });
myDataGridView.Rows.Add(new object[] { 5, "Course 5", "Sub 1\nSub 2\nSub3", "45\n58\n97" });

Read more: C# Corner
QR: Inline image 1

Posted via email from Jasper-net

0 comments: