Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old June 7th, 2007, 10:37 AM
Member
 
Join Date: May 2007
Location: Chennai
Posts: 99
Default Make all the rows height even in DataGridView in .Net 2.0

This article explains how to make all the rows height the same in DataGridView in .Net 2.0 while using AutoSizeRowMode

In DataGridView 2.0,there is a option to resize row to fit the content of the cell.This will increase the row height.
Expand|Select|Wrap|Line Numbers
  1. this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
But the row heights will not be the same through out the DataGridView (some row's height will be smaller than another row's height )

To make all of the rows in your DataGridView appear the same size, take the following the steps to set the max row height and set it to be the height for all the rows in dataGridView:
Step 1:
Find out the maximum row height and set the height to DataGridView preferred rowheight
Expand|Select|Wrap|Line Numbers
  1. void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
  2.       {
  3.  
  4.          int rowHeight = e.Row.Height;
  5.          if (this.dataGridView1.RowTemplate.Height < rowHeight)
  6.          {
  7.             this.dataGridView1.RowTemplate.Height = rowHeight;
  8.  
  9.          }
  10.     }
  11.  
Step 2
Set the AutoSizeRowsMode to none,then apply the maximum row height to all the rows
Expand|Select|Wrap|Line Numbers
  1.   private void dgLocation_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  2.       {
  3.          int rowHeight=this.dataGridView1.RowTemplate.Height;
  4.  
  5.          this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
  6.          int numRows = this.dataGridView1.Rows.Count;
  7.          for (int i = 0; i < numRows; i++)
  8.          {
  9.             this.dataGridView1.Rows[i].Height = rowHeight;
  10.  
  11.          }
  12.  
  13.       }
  14.  
Reply



  #2  
Old August 17th, 2007, 03:45 PM
Member
 
Join Date: Jul 2007
Posts: 38
Default

How do you call these methods in a windows form?
Reply
  #3  
Old November 27th, 2007, 07:10 AM
Member
 
Join Date: May 2007
Location: Chennai
Posts: 99
Default

These are the datagridview events.

this.dataGridView1.RowHeightChanged += new System.Windows.Forms.DataGridViewRowEventHandler(t his.dataGridView1_RowHeightChanged);

this.dataGridView1.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEvent Handler(dataGridView1_RowPostPaint);
Reply
  #4  
Old March 14th, 2008, 07:15 PM
Newbie
 
Join Date: Mar 2008
Location: delhi
Age: 22
Posts: 6
Default

how we connect a database to datagrid view in vb.net language??
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.