When I usually display data in a datagrid, I have a DataSet, i fill the dataset, and then assign it to the datasource of the datagrid. But what if you need to control the way the records are displayed in the datagrid?
For example, if i have a table called “Employees”, and i bind the dataset to the datagrid, the results will be displayed like this:
ID Name
1 Daniel
2 Lesseps
3 Isaacs
4 Diaz
But what if you would like to have control of the data and display it like this:
ID Name ID Name
1 Daniel 2 Lesseps
3 Isaacs 4 Diaz
This can be done setting the datagridview in a Virtual Mode. Virtual Mode according to the Visual Studio description “indicates whether you have provided your own data-management operations for the DataGridView Control”.
In order to display data in a datagridView in virtual mode you need to set the VirtualMode property in the datagridview to True and handle the dataGridView1_CellValueNeeded event.
In the following example I have a struct which will hold data about an employee , specifically their name and id. I’m going to initialize the data, create the columns of the datagridview, and finally I’m going to implement the event that is going to display the data as I want it.
private void InitializeData() private void CreateColumns() private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
public partial class Form1 : Form
{
private List< Employee > m_Employees = new List< Employee >();
public Form1()
{
InitializeComponent();
InitializeData();
CreateColumns();
}
///
/// and add them to a List.
///
{
Employee emp = new Employee();
emp.id = 1;
emp.name = "Daniel";
Employee emp2 = new Employee();
emp2.id = 2;
emp2.name = "Lesseps";
Employee emp3 = new Employee();
emp3.id = 3;
emp3.name = "Isaacs";
Employee emp4 = new Employee();
emp4.id = 4;
emp4.name = "Diaz";
m_Employees.Add(emp);
m_Employees.Add(emp2);
m_Employees.Add(emp3);
m_Employees.Add(emp4);
}
///
/// a name and finally I add two rows
///
{
// create four columns
dataGridView1.ColumnCount = 4;
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[2].Name = "ID";
dataGridView1.Columns[3].Name = "Name";
// add two rows
dataGridView1.Rows.Add();
dataGridView1.Rows.AddCopies(0, 1);
}
///
/// If it's the first column and the first row I add the first record id.
/// If it's the first column and the second row I add the third record id.
/// If it's the third column and the first row I add the second record id.
/// If it's the third column and the second row I add the fourth record id.
///
{
if (e.ColumnIndex == 0)
{
if (e.RowIndex == 0)
{
e.Value = m_Employees[e.RowIndex].id;
}
else
{
e.Value = m_Employees[e.RowIndex + 1].id;
}
}
else if (e.ColumnIndex == 1)
{
if (e.RowIndex == 0)
{
e.Value = m_Employees[e.RowIndex].name;
}
else
{
e.Value = m_Employees[e.RowIndex + 1].name;
}
}
else if (e.ColumnIndex == 2)
{
if (e.RowIndex == 0)
{
e.Value = m_Employees[e.RowIndex + 1].id;
}
else
{
e.Value = m_Employees[e.RowIndex + 2].id;
}
}
else if (e.ColumnIndex == 3)
{
if (e.RowIndex == 0)
{
e.Value = m_Employees[e.RowIndex + 1].name;
}
else
{
e.Value = m_Employees[e.RowIndex + 2].name;
}
}
}
}
struct Employee
{
public int id;
public string name;
}
I believe the code is pretty easy to understand, I would only like to point out that in the event that handles the way the data is displayed, in this line for example:
e.Value = m_Employees[e.RowIndex].id;
The ID of the employee is added to a cell in the datagridView.
Hope this was helpful to you. If there are any questions or observations , they will be highly appreciated.
