Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView Row.Frozen Property
 

DataGridView Row.Frozen Property

For a C# 2005DataGridView, I need to keep a particular row of data fixed at position 0. It doesn't matter to meif it scrolls out of the display area if the user scrolls down, or if it stays "frozen" at the top of the grid.

It seems the best way to do this is to use the Row.Frozen property.

Therefore, if I set Rows[0].Frozen = true, the first row in the DataGridView is frozen, which is what I want. However, if the user presses the sort header and resorts the grid, the previously frozen row is sorted to someplace else, and a different row becomes frozen. Also, if they enter data into the row, it is sorted to another location. How can I keep the same exact row frozen in position [0] regardless of the sort? I do not want to disable sorting, as that is needed for all rows except Row[0].

Since my Grid is bound to a DataTable as the datasource, I could modify the data by adding a hidden column and forcing the particular row to always be sorted to index 0, but I do not want to modify the data source or add any additional columns.

For example, is there someway to override the default sort method to ignore a row?, etc. Custom sorting seems to be not allowed when bound to a datasource.

amosz42  Thursday, February 02, 2006 6:06 PM

Frozen property doesn't know anything about the data in the rows. Your solution for adding a hidden column is the only way. When databound the DataGridView doesn't perform any sort -- it asks the datasource to sort, so there isn't really much of a way to override the default sort.

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

Mark Rideout  Thursday, February 02, 2006 7:43 PM

Frozen property doesn't know anything about the data in the rows. Your solution for adding a hidden column is the only way. When databound the DataGridView doesn't perform any sort -- it asks the datasource to sort, so there isn't really much of a way to override the default sort.

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

Mark Rideout  Thursday, February 02, 2006 7:43 PM

I want exactly the same for a fixedrow filter much like in Excel.

I use Syncfusion grid at the moment, this handles it well. The frozen rows essentially become the extended header area. Sorting does not interfere with it.

I've been asked to move away from Syncfusion. The only way I can replicate what I've done here is to enlarge the vertical header area and render a drop down combox neatly at the bottom of the header row. This will giveme the desired results coupled with some laborious coding to simulate the combo. But of course I will need to make sure the header/caption text is shown correctly,so Iwill code this manually and override the default rendering.

It's also right pain not having multisort on the grid, so I'll have to manually code this and again render my 3 level sorting glyphs. All in all Syncfusion Grid is stillsuperior.

Another way I was thing of creating an inherited class that performs the mapping manually override the data bound base behaviors. This is a big job for a simple effect.

Matthew McEwan  Thursday, July 26, 2007 10:28 AM

I would like just a little more clarification on this topic if we may.I have a datagridview that the rows and columns are completely built by me (through code, not bound to any data source).

Is it possible to freeze the first row and have the column sorting NOT sort the frozen row?

Michael6556  Saturday, March 08, 2008 7:48 PM
Yes, Michael, this is possible.

I hope you've long since found a solution to this, but for the sake of the forum, here is my solution to keeping a row always at the top of a datagridview with no binding to data source:

Override the SortCompare Event, and just add code that says if the row index is 0, then the event is handled, and row 0 never gets sorted

private void grid_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
try
{
if (e.RowIndex1 == 0)
e.Handled = true;
if (e.RowIndex2 == 0)
e.Handled = true;
return;
}
catch (Exception ex)
{
ex.ToString();
}
}

I'd like a way to absolutely freeze a row so that is can't be moved, but... this has worked for me so far.
Erky  Thursday, August 28, 2008 5:20 PM
Is it possible to achieve the same behavior of making a column frozen in VS 2003 - Winforms DataGrid?

right now I need to achieve thisrequirementusing VB.NET 1.1
TRKReddy  Friday, August 28, 2009 12:57 PM

You can use google to search for other answers

Custom Search

More Threads

• getting error while binding data to textfield.
• Changing the index of combo box programmatically based on text box value
• Accessing the BindingContext of a control in the constructor
• hierarchical dataviewgrids
• Microsoft Script Control
• How do I create a GridView/edit screen?
• A filter with ALL in a combobox ???
• Combo Box problem
• List Object-->BindingSource = But BindingNavigator wont work
• Removing row programatically using Rows.Remove() removes then adds a blank row... HELP!