1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How To Make all the rows height even in DataGridView in .Net 2.0

Discussion in 'Programming' started by largn81, Jan 26, 2008.

  1. #1
    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.:)



    Code: ( c )
    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:)

    Code: ( c )
    void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
    {

    int rowHeight = e.Row.Height;
    if (this.dataGridView1.RowTemplate.Height < rowHeight)
    {
    this.dataGridView1.RowTemplate.Height = rowHeight;

    }
    }

    Step 2
    Set the AutoSizeRowsMode to none,then apply the maximum row height to all the rows:)

    Code: ( c )
    private void dgLocation_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
    int rowHeight=this.dataGridView1.RowTemplate.Height;

    this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
    int numRows = this.dataGridView1.Rows.Count;
    for (int i = 0; i < numRows; i++)
    {
    this.dataGridView1.Rows.Height = rowHeight;

    }

    }

    ;)
     
    largn81, Jan 26, 2008 IP