Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Setting DataGridView Row Height
 

Setting DataGridView Row Height

I'm having difficulties setting the row heights in the DataGridView control. I'm currently using the control in non-Databound mode (I started using a Dataset but stopped using it due to similar difficulties).

I'm displaying multi-line text data. Each cell is set to have a WrapMode of true through the various default style mechanisms. Initially, I started with AutoSizeRowsMode set to AllCells and no specific code to size rows. I found that the result was too slow and tried switching to an AutoSizeRowsMode of Displayed Cells. I found that the row heights didn't match their multi-line content so I tried to AutoResizeRow the rows after they were added andfound no change. I've tried programmatically setting the Row height using "(mainDataGridView.Rows[mainDataGridView.Rows.Count - 1]).Height = height;". This also didn't change the row height.

In case it's relevant,when I've finished adding rows, I set the CurrentCell to be the last row but the last row isn't visible; it's a few rows further down than the last visible row. I've also noticed that when AutoResizeMode is set to DisplayedCells and I display 200 rows of test data, the heights of rows 182-193 seem to have appropriate heights while the others are smaller than they should be,

Any suggestions?

Also, can anyone explain why DataGridView row sizing is affected by which rows are displayed? It would seem that the row height would (normally anyway) be the Y component of the tallest cell in the row's Preferred size.

Thanks

Mike

In case it helps, here's the DataGridView configuration generated by the Designer.

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();

this.mainDataGridView = new System.Windows.Forms.DataGridView();

((System.ComponentModel.ISupportInitialize)(this.mainDataGridView)).BeginInit();

this.SuspendLayout();

//

// mainDataGridView

//

this.mainDataGridView.AllowUserToAddRows = false;

this.mainDataGridView.AllowUserToDeleteRows = false;

this.mainDataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;

this.mainDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;

dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;

dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window;

dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;

dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;

dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;

dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;

this.mainDataGridView.DefaultCellStyle = dataGridViewCellStyle1;

this.mainDataGridView.Location = new System.Drawing.Point(18, 12);

this.mainDataGridView.Name = "mainDataGridView";

this.mainDataGridView.ReadOnly = true;

dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;

this.mainDataGridView.RowsDefaultCellStyle = dataGridViewCellStyle2;

this.mainDataGridView.RowTemplate.DefaultCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;

this.mainDataGridView.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False;

this.mainDataGridView.Size = new System.Drawing.Size(648, 452);

this.mainDataGridView.TabIndex = 0;

mikebk  Wednesday, February 08, 2006 5:17 PM

After populating the grid, this is what I did in a similar situation:

for (int i = 0; i < this.dataGridView1.Rows.Count; i++)

{

this.dataGridView1.AutoResizeRow(i);

}

JohnnyAV  Wednesday, August 02, 2006 5:24 AM

After populating the grid, this is what I did in a similar situation:

for (int i = 0; i < this.dataGridView1.Rows.Count; i++)

{

this.dataGridView1.AutoResizeRow(i);

}

JohnnyAV  Wednesday, August 02, 2006 5:24 AM

Thanks. I had tried something similar but was concerned about impact on performance of sizing each row each time.

In case it helps you, I suspect thatunderlying cause of my problem was settingthis.mainDataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;. I'm currently using DataGridViewAutoSizeRowsMode.None.

Mike

mikebk  Wednesday, August 02, 2006 1:11 PM

I tired the similar method to set the height of every row:

            int numRows = this.dataGridView1.Rows.Count;
            for(int i = 0; i < numRows; i++)
            {
                this.dataGridView1.RowsIdea.Height = height;
            }

It takes about 30 sec to update DataGridView that has 6000 rows. Any method to do this faster? 

 

 

 

 

 

 

 

Daisuke Mori  Wednesday, August 16, 2006 10:43 PM

I decided to only update the height of the rows that were visible in the scrolling region.

I loop through the rows on a scroll event setting the height on each row (using DataGridViewCell.MeasureTextHeight) until the sum of the heights of the individual rows I've resized is larger than the height of the dgv's client area.

Earlier, I had tried setting FirstDisplayedScrollingRowIndextotop row that was visible then looping through rows until I found one that wasn't visible. Unfortunately, it appears that the Visible propertyreally just means that the row isn't Hidden so I had to switch to measuring the text height.

Mike

mikebk  Thursday, August 17, 2006 1:50 PM

Why not consident to define the CellPainting event?

private void ucDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex != -1 && _defaultRowHeight != 0)
{
ucDataGridView.Rows[e.RowIndex].Height = _defaultRowHeight;
}
}

and it's faster than the loop of the Rows.

zhengjs  Thursday, June 14, 2007 2:55 AM

Sorry to resurrect an old thread, but I found a solution I liked better than any given:

Add a handler for DataGridView.RowPrePaint:

Code Snippet

dataGridView1.RowPrePaint += new DataGridViewRowPrePaintEventHandler(dataGridView1_RowPrePaint);

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
dataGridView1.AutoResizeRow(e.RowIndex);
}

This works well if your grid is in VirtualMode as it does not require every row to be 'touched' when the data is bound.

Tergiver  Saturday, November 08, 2008 3:40 PM

I tired the similar method to set the height of every row:

int numRows = this.dataGridView1.Rows.Count;
for(int i = 0; i < numRows; i++)
{
this.dataGridView1.RowsIdea.Height = height;
}

It takes about 30 sec to update DataGridView that has 6000 rows. Any method to do this faster?

No, no, no, using a looping is not good idea, if user want all cell have same height, just try this:

DataGridView1.RowTemplate.Height = 17 'Do this before you binding a data source, once you binding the data, the height is fixed, you have to do it one by one

P/S: Thank for all the kind man in MSDN that are helpful, I decided to do what you poeple do, to contribute a little bit of my knowledge on the same problem we are facing.

ThankMSDNveryMuch  Monday, July 27, 2009 5:29 PM

You can use google to search for other answers

Custom Search

More Threads

• Overwriting BindingNavigator AddNew
• Determine which control caused CurrencyManager.CurrentChanged event?
• How to Add,Delete and update from datagridview
• setting DataGridView.CurrentCell threw IndexOutOfRangeException error
• Derived DataGrid Questions (multiple selections and rgba brush question)
• A combo box in a Datagrid
• Font style in DataGridView problem
• Image resizing in DataGridView
• FK constraint error when filling tableadaptors
• DateTimePicker and DataBinding