Unbound DataGridView: Typical Problems

by Igor Katenov, the lead developer at 10Tec

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 iGrid.NET control which is positioned as a DataGridView replacement.

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 at Scrolling, Rendering, Filtering and 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 filtering functionality is trivial as you can harness 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 this grid by several columns or need the autofilter functionality like in Microsoft Excel.

The related article DataGridView Filter in C# on this website examines this 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 replacement, cells with images and text

Sorting unbound DataGridView by multiple columns

DataGridView provides us with the special overloaded version of its Sort method we can use to sort unbound DataGridView by multiple columns. An example of multi-column sorting in unbound DataGridView can be found in another article Sort DataGridView in C# on this website.

To briefly outline the benefits of iGrid compared to DataGridView if we talk about sorting, let's look at the following screenshot depicting how multiple columns sort look in iGrid:

Multiple column sort in unbound DataGridView replacement

To sort iGrid by multiple columns, you simply click the required column headers holding down one of the modifier keys (Ctrl, Shift or Alt). The sort glyphs in the sorted columns are followed by numbers indicating the order of columns in the current sorting. What is also important, is that the multi-column sort feature in iGrid is available out-of-the–box with no coding, and it works for any data types uploaded into this grid.


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 »