How To Populate A DataGridView Control Using OleDbDataReader


Download

Many developers know in order to populate a DataGridView control, all you have to do is set the DataSource property of the control to a DataTable object. This can be achieved in a number of ways with very little code and effort. The code in listing 1.1 below demonstrates how to populate a DataGridView control using an MS Access database.

Listing 1.1

string strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Employees.mdb";

string strSql = "SELECT * FROM tbl_employees";

OleDbConnection con = new OleDbConnection(strProvider);

OleDbCommand cmd = new OleDbCommand(strSql, con);

con.Open();

cmd.CommandType = CommandType.Text;

OleDbDataAdapter da = new OleDbDataAdapter(cmd);

DataTable employees = new DataTable();

da.Fill(employees);

dataGridView1.DataSource = employees;

The above code creates a connection to an Access database and fills a DataTable using a OleDbDataAdapter object with records. It then populates a DataGridView control with the records by setting the DataSource property of the control to the DataTable object.

But what if you need to use the OleDbDataReader to read the records from a table? The problem is you can not use the OleDbDataReader object as the DataSource for the DataGridView control. This means you will need to bind the OleDbDataReader object in code you're self. Lucky for us the OleDbDataReader Class provides the methods necessary to do the binding our self through code.

The Code

The first thing we need to do is set up the DataGridView control with headers. These headers will be the column names from the OleDbDataReader object. But before we can get the column names, we need to know how many columns there are in total. This is easily achieved with the code in listing 1.2.

Listing 1.2

string strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Employees.mdb";

string strSql = "SELECT * FROM tbl_employees";

OleDbConnection con = new OleDbConnection(strProvider);

OleDbCommand cmd = new OleDbCommand(strSql, con);

con.Open();

cmd.CommandType = CommandType.Text;

OleDbDataReader dr = cmd.ExecuteReader();

int columnCount = dr.FieldCount;

So now that we know how many columns there are in total, we can use a simple loop to loop through each column and get the name of the column. The OleDbDataReader Class has a GetName() method. This method takes one argument, which is the index of a column. In the loop we get the column name and add it to the DataGridView control using the DataGridViews's Columns.Add() method. The code in listing 1.3 below shows how to add the headers to the DataGridView control.

Listing 1.3

for (int i = 0; i < columnCount; i++)
{
dgv.Columns.Add(dr.GetName(i).ToString(), dr.GetName(i).ToString());
}

Now we have the headers set up for the DataGridView control, all that remains is to add the records using another loop. However this part is not as straight forward as you may think.

The reason is, when you get data from a column using the OleDbDataReader object, you need to specify the data type. There is a method for each data type. For example if you have a column which is of Integer data type, then you need to return the column data as an Integer, like wise if you have a column data type of Text, then you must return the column data as a String.

What this means is, when we loop through the OleDbDataReader object to get each row data, we need to check the column data type and return the data using the correct method.

But before we move on to reading each row, we need to first declare a String array, this array will hold the column data for a row, it will then be used to add the row to the DataGridView control.

Because we know at this point how many columns there are we can easily set the size of the String array.

string[] rowData = new string[columnCount];

After declaring the String array, we need to loop through the records in the OleDbDataReader object. We do this by using a While loop and the Read() method of the OleDbDataReader object. The Read() method returns a Boolean indicating if there are more Records left in the OleDbDataReader object. When we read each row from the OleDbDataReader object we need to loop through each column to get the data and store it into the String Array.

When we loop through each column, we first check the data type of the column so that we can return the column data using the correct method. For example, if the first column in the table is of type Integer then we need to use the GetInt32() method of the OleDbDataReader object to get the column data as an Integer.

To get the column data type we use the GetFieldType() method of the OleDbDataReader object. This method takes an integer as its argument. The integer is the column index. We use an 'if statement' to check what the data type of the column field is and use the appropriate method to get the column data. All data types get converted into a String to populate the DataGridView control.

Listing 1.4 below shows the complete code. Alternatively you can download the project source files.

Listing 1.4

string strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Employees.mdb";
string strSql = "SELECT * FROM tbl_employees";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataReader dr = cmd.ExecuteReader();

int columnCount = dr.FieldCount;

for (int i = 0; i < columnCount; i++)
{
dgv.Columns.Add(dr.GetName(i).ToString(), dr.GetName(i).ToString());
}

string[] rowData = new string[columnCount];
while (dr.Read())
{
for (int k = 0; k < columnCount; k++)
{
if (dr.GetFieldType(k).ToString() =="System.Int32")
{
rowData[k] = dr.GetInt32(k).ToString();
}

if (dr.GetFieldType(k).ToString() == "System.String")
{
rowData[k] = dr.GetString(k);
}
}

dgv.Rows.Add(rowData);
}




1 Simple DataGridView Example - C#
This example demonstrates how to load data into the DataGridView control using string array's.
2 A Custom Message Box - C#
This example source code demonstrates how you can develop your own messages box.
3 Custom MessageBox 2 - C#
This example source code builds on a previous example of creating a custom MessageBox. New features in this example include different types of buttons and displaying an icon. Also uses different message beep tones for standard MessageBox's and warring MessageBox's.
4 Connect To MSN Messenger Using The MSN Protocol - C#
This sample application demonstrates how to connect to MSN Messenger using the MSN Protocol. It also includes how to generate a Ticket that is used with the ChallengeString. This is a simple authentication example.
5 Send Messages Using The Net Send Command - C#
This sample program demonstrates how to send messages in a network using the Windows Messenger service.
1 Transfer data from CSV file to MySQL - Java
This snippet transfers deliminated data from a CSV file to a MySQL database. You need to edit the code to include your database connection details and also the fields from the CSV and MySQL needs to correspond with your fields.
2 Read Image File As bytes - C#
This sinppet opens an image for reading as bytes. The bytes are then placed into a memorystream which is used to create a bitmap image of the bytes. The image is then set as the forms background image.
3 Retrieve the path of a file, using openfiledialog - C#
This snippet uses the openfiledialog class to get a files path. Useful for opening files from a location on the harddrive.
4 Load web image into picturebox - Visual Basic.Net
Function with picturebox and url as argument to display the passed url to image inside a picturebox I got this from freevbcode a while back.
5 Random Number Generator - Visual Basic.Net
Function that reurns a random non-repeating integer
CY2 Online2.net | CopyRight 2005 - 2008 | All Rights Reserved