Reader Level:
ARTICLE

Hierarchical Data Into DataGridView in C#

Posted by Ghanashyam Nayak Articles | Visual C# February 23, 2012
In this article you will learn how to enter hierarchical data into a DataGridView. You will also learn how to format a DataGridView cell.
  • 0
  • 0
  • 19695
Download Files:
 

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.
 

Roll No. Course Subjects Marks
1 Course 1 Sub 1 90
    Sub 2 95
    Sub 3 85
2 Course 2 Sub 1 67
    Sub 2 57
    Sub 3 84
3 Course 3 Sub 1 64
    Sub 2 80
    Sub 2 92
4 Course 4 Sub 1 94
    Sub 2 90
    Sub 2 99
5 Course 5 Sub 1 45
    Sub 2 58
    Sub 2 97

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" }); 


After that I used the SelectionMode property for how to select data from the DataGridView either FullRow or FullColumn or Cell.

Here I set as FullRow.
 

myDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 

Ok. You also can format your every cell.

How?

Using the "CellFormatting" event of the DataGridView.

Here I have set the SelectionBackColor & SelectionForeColor properties.

You can set these properties to a particular cell. Here I have set it to the Column2 & Column3 which are the last two Columns.
 

if (e.ColumnIndex == 2 || e.ColumnIndex ==3)

{
       e.CellStyle.SelectionBackColor=Color.White;
       e.CellStyle.SelectionForeColor = Color.Black;
} 


See the whole code:
 
using System;

using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace DataGridView
{
    public partial class Form1 : 
Form
    {
        private System.Data.DataSet dataSet;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myDataGridView.Columns.Add("myColumn1""Roll Number");
            myDataGridView.Columns.Add("myColumn2""Course");
            myDataGridView.Columns.Add("myColumn3""Subject");
            myDataGridView.Columns.Add("myColumn4""Marks(Out of 100)");

            myDataGridView.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            myDataGridView.Columns[3].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
 
            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" });

            myDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2 || e.ColumnIndex ==3)
            {
                e.CellStyle.SelectionBackColor=Color.White;
                e.CellStyle.SelectionForeColor = Color.Black;
            }
        }
     }
} 

See the following image:

01.png

When I select the particular Row then it will display the selected the first two columns only. Because I have used the CellFormating Event & it will format the last two column's cells to SelectionBackColor & SelectionForeColor as White. 

See the following image:

02.png

COMMENT USING

Trending up