简体   繁体   中英

Adjusting Rowheader properties on Datagridview

In Winforms DataGridView, how do I:

  1. Remove the arrow on the row header? I need to display the row header text, so I can't simply set RowHeadersVisible = false .
  2. Adjust the width of the row header programmatically? I'm setting the row headers by code so I need the width to adjust to show the row header text upon change.

First of all override the function the DataGridView known as

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    e.PaintHeader(DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground);
}

On button click add value to row header

private void button1_Click(object sender, EventArgs e)
{
    string a = "khan yousafzai";
    dataGridView1.RowHeadersWidth = dataGridView1.RowHeadersWidth +(7* a.Length);
    dataGridView1.Rows[0].HeaderCell.Value = a;
    dataGridView2.Rows.Add();
}
  1. Do you need to still allow sorting but not show the arrow? If not, just set each column SortMode to NotSortable . If you need to sort but not show the arrow, set the column SortMode to Programmatic , and manually sort the data source in the column Click or MouseDown event.

  2. After you set the column header to whatever text you need, get the width of the text using the form Graphics class and then set the column width accordingly:

     Graphics g = this.CreateGraphics(); int w = (int)g.MeasureString(dataGridView1.Columns[0].HeaderText, dataGridView1.Font).Width; this.dataGridView1.Columns[0].Width = w;

First of all, how to add number line RowHeadersWidth to DataGridView in C# .Net 2.0 and above:

// On Form_Load add the numeration to DataGridView Row Header
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
}

Now, you can resize our row header dynamically depending of its size, just like in Microsoft Excel.

// Dinamically adjust row header size to max current width available (like Microsoft Excel does)
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    int firstDisplayedCellIndex = dataGridView1.FirstDisplayedCell.RowIndex;
    int lastDisplayedCellIndex = firstDisplayedCellIndex + dataGridView1.DisplayedRowCount(true);
    
    Graphics Graphics = dataGridView1.CreateGraphics();
    int measureFirstDisplayed = (int)(Graphics.MeasureString(firstDisplayedCellIndex.ToString(), dataGridView1.Font).Width);
    int measureLastDisplayed = (int)(Graphics.MeasureString(lastDisplayedCellIndex.ToString(), dataGridView1.Font).Width);
                
    int rowHeaderWitdh = System.Math.Max(measureFirstDisplayed, measureLastDisplayed);
    dataGridView1.RowHeadersWidth = rowHeaderWitdh + 35;
}

This solution its only for .Net Framework 2.0 and above, not for CF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM