Unbound DataGridView: Typical Problems

Edited by

The stock Microsoft DataGridView control can work in unbound mode. In this article we will consider most typical tasks related to unbound DataGridView (how to populate unbound DataGridView, to create a DataGridView column with different cell types, to sort unbound DataGridView by several columns, etc.). Analyzing unbound DataGridView code examples in C#, we will draw your attention to possible problems and tell you how to solve them. Every solution is also compared to the corresponding functionality of the 10Tec advanced DataGridView alternative, the iGrid.NET control.

Populating unbound DataGridView

The classic way to populate unbound DataGridView recommended in MSDN looks like this C# code example:

songsDataGridView.ColumnCount = 5;

songsDataGridView.Columns[0].Name = "Release Date";
songsDataGridView.Columns[1].Name = "Track";
songsDataGridView.Columns[2].Name = "Title";
songsDataGridView.Columns[3].Name = "Artist";
songsDataGridView.Columns[4].Name = "Album";

string[] row0 = { "11/22/1968", "29", "Revolution 9", 
    "Beatles", "The Beatles [White Album]" };
string[] row1 = { "1960", "6", "Fools Rush In", 
    "Frank Sinatra", "Nice 'N' Easy" };
string[] row2 = { "11/11/1971", "1", "One of These Days", 
    "Pink Floyd", "Meddle" };


The code is clear and straightforward, but the problem is that it starts working slowly as the number of rows to add is increasing. To overcome this problem, you need to create an extra temporary collection of the data rows in memory and populate the unbound DataGridView with one method call – DataGridView.Row.AddRange(). To enhance the performance even more, you can apply some tricks like turning the control redrawing off while it is being populated. All these techniques are discussed in greater detail in another article on this website: Why is DataGridView Slow in Rendering, Filtering, Populating?

If we compare the process of unbound DataGridView population with the same process in 10Tec iGrid.NET, we will see that iGrid works extremely fast compared to DataGridView. iGrid is optimized for unbound mode, and you can populate it with 100’000+ rows directly without any unbound DataGridView performance problems.

Different cell types in a DataGridView column

If you need to mix different cell types in a DataGridView column, one of the easiest ways to do that can be found in an answer in this Stackoverflow thread:

Adding Different DataGridView Cell Types to a Column

The picture below demonstrates how the compiled solution looks:

Different cell types in a DataGridView column

Consider that we need to display a checkbox control in the fourth cell of the second column in an unbound DataGridView. According to the first method from that answer, we should write a code snippet like the following one:

DataGridViewCheckBoxCell checkBoxCell = new DataGridViewCheckBoxCell();
dataGridView1[1, 3] = checkBoxCell;

Actually we replace the cell of the default DataGridView cell type (DataGridViewTextBoxCell) with one of the descendants of the DataGridViewCell class (DataGridViewButtonCell, DataGridViewComboBoxCell, etc.)

This solution works fine, but you are limited to the 7 available DataGridView cell types. The author of the answer suggests another universal way to incorporate any WinForms control into an unbound DataGridView (like the DateTimePicker control in the fourth row on the picture above). The idea is to add the cell control you need to DataGridView as a children control and position it over the cell. Unfortunately, this idea is not feasible as it requires much more support code. For instance, you need to process the scrolling events in your unbound DataGridView and move these external controls together with the cells they belong to, and so on.

To overcome all these problems, DataGridView provides you with the IDataGridViewEditingControl interface. MSDN contains a C# example of a calendar column in unbound DataGridView that demonstrates how to use this interface. This is a complex solution that requires about 250 lines of C# code. In iGrid, you can implement a custom cell editor in just about a dozen lines of code:

internal class NumericUpDownCellEditor : iGCellEditorBase
  private NumericUpDown fNumericUpDown = new NumericUpDown() {
    BorderStyle = BorderStyle.None, Maximum = 10000, Increment = 5};

  public override Control EditControl
       return fNumericUpDown;

  public override void SetBounds(
    Rectangle textBounds, Rectangle suggestedBounds)
    fNumericUpDown.Bounds = suggestedBounds;

Filtering unbound DataGridView

End users need the ability to filter data grids in real-worlds apps. If your DataGridView is bound to a data source like ADO.NET DataTable, the implementation of the DataGridView filtering functionality is trivial as you can exploit the filtering feature of the underlying dataset or the BindingSource object. For instance, in the case of a BindingSource object, you can use its built-in filter functionality this way:

bindingSource1.Filter = String.Format("ColumnName like '%{0}%'", textBoxFilter.Text);

But this does not work if you need to filter unbound DataGridView. The situation is worse if you need to filter DataGridView by several columns or need the autofilter functionality like in Microsoft Excel.

The related article DataGridView Filter in C# on this website examines the DataGridView filtering problem in depth and gives you some practical solutions for real-world apps. If we compare these solutions to the functionality iGrid provides in this field, a one-sentence result of this comparison will be the following. iGrid allows end users to have the built-in autofilter functionality with no troubles and no coding!

DataGridView with an unbound image column

Generally the DataGridViewImageColumn class is used to create a column with images in an unbound DataGridView (or it can be an unbound image column when this control is bound to a data source):

DataGridView with unbound image column

The Image property of the DataGridViewImageColumn class can be used to specify the default image displayed in all cells of the column. If you need different images in cells, you simply use the DataGridView cell’s Value property for this purpose:

dataGridView1[1, 3].Value = Image.FromFile(@"C:\Sample Images\Sun Flower.jpg");

The image can be also set dynamically based on the values from other columns. For instance, if the first column of an unbound DataGridView is used to store full paths to image files on a hard drive, you can load and display them dynamically in the second unbound image column. A typical solution for that you can find in the Internet is based on a CellFormatting event handler like this:

void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  if (e.ColumnIndex == 1)
    e.Value = Image.FromFile((string)dataGridView1[0, e.RowIndex].Value);

However, the CellFormatting event is triggered every time when a cell should be painted on the screen. This can cause unneeded flickering in DataGridView. To avoid it, set the image for a cell only once when this is really required. A good candidate for this can be the CellValueChanged event of DataGridView.

But this does not solve the flickering problem completely. To eliminate flickering in DataGridView (unbound or data-bound – no matter), enable double buffering for the DataGridView control:

Type dgvType = dataGridView1.GetType();
System.Reflection.PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
  System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
pi.SetValue(dataGridView1, true, null);

All this good if we need a DataGridView column with images without additional contents, but in many real-world apps we also need to display images with text in DataGridView cells. To implement this task in DataGridView, you need to write a lot of code. One of the possible solutions is described in this thread:

Add Image to DataGridView Column with text

In a nutshell, you need to implement a custom DataGridView column type and incorporate a RichTextBox control with the required image and text into it. When you look at the code, you will see that it is really not an easy task. From this point of view, iGrid.NET is an ideal choice as its cells allow you to display images with texts and use various alignment options for these cell parts:

Unbound DataGridView alternative, cells with images and text

Sorting unbound DataGridView by multiple columns

One of the overloaded versions of DataGridView’s Sort method allows you to sort unbound DataGridView by multiple columns. You need to use the version that accepts an object implementing the System.Collections.IComparer interface. A classic example of this interface usage demonstrates how to sort unbound DataGridView by two columns:

Sort unbound DataGridView by two columns

The C# source code of the custom comparer used for this DataGridView is the following:

public class DGVComparer : System.Collections.IComparer
  public int Compare(object x, object y)
    DataGridViewRow row1 = (DataGridViewRow)x;
    DataGridViewRow row2 = (DataGridViewRow)y;

    int compareResult = string.Compare(

    if (compareResult == 0)
      compareResult = ((int)row1.Cells[1].Value).CompareTo((int)row2.Cells[1].Value);

    return compareResult;

Below is the corresponding code snippet that demonstrates how to use this custom comparer:

dataGridView1.Sort(new DGVComparer());
dataGridView1.Columns[0].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
dataGridView1.Columns[1].HeaderCell.SortGlyphDirection = SortOrder.Ascending;

Note that we set the sort glyphs manually as our unbound DataGridView knows nothing about the current sort order of its columns.

This approach can be used to sort DataGridView when the user clicks its column headers. Generally this logic is put in an event handler of the ColumnHeaderMouseClick event. But it’s very hard to implement this technique if you want to provide your users with the ability to sort more than two columns in an unbound DataGridView, especially if the columns store values of different data types. Return to the source code of the custom comparer used above and see that we hard-coded the comparison logic for the two columns of the predefined data types.

10Tec iGrid.NET is free from this drawback. You can simply click its column headers holding down the Shift or Ctrl key to sort this unbound DataGridView replacement by multiple columns:

Multiple column sort in unbound DataGridView replacement

Note that the column headers of the sorted columns display numbers after the sort glyphs. These numbers indicate the order of the columns in the current sort criteria.

This iGrid multicolumn sort feature is enabled out-of-the–box. No coding is required from your side.


In the article above we have examined typical situations and problems of unbound DataGridView. As you see, solving these problems can require tedious coding or can be even hardly possible for DataGridView. If you need to implement these tasks in your applications, it will be a smart idea to consider the 10Tec WinForms grid control as an advanced replacement for the .NET DataGridView control in unbound mode.

Read more about 10Tec unbound DataGridView alternative »