【问题标题】:Adjusting Rowheader properties on Datagridview在 Datagridview 上调整 Rowheader 属性
【发布时间】:2012-08-30 01:37:57
【问题描述】:

在 Winforms DataGridView 中,我该怎么做:

  1. 删除行标题上的箭头?我需要显示行标题文本,所以我不能简单地设置RowHeadersVisible = false
  2. 以编程方式调整行标题的宽度?我正在通过代码设置行标题,因此我需要调整宽度以在更改时显示行标题文本。

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    首先重写DataGridView的函数

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

    在按钮上单击将值添加到行标题

    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();
    }
    

    【讨论】:

      【解决方案2】:
      1. 是否仍需要允许排序但不显示箭头? 如果没有,只需将每一列SortMode 设置为NotSortable。 如果需要排序但不显示箭头,请将SortMode列设置为Programmatic,并在ClickMouseDown事件列中手动对数据源进行排序。

      2. 将列标题设置为所需的任何文本后,使用Graphics 类的形式获取文本的宽度,然后相应地设置列宽:

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

      【讨论】:

      • 呃.. 两种解决方案都不起作用。我需要以编程方式调整行标题宽度,而不是第一列
      • 您确定您的意思是行标题,而不是列标题?行标题是网格左侧的空白框,不包含任何文本。
      • 我非常肯定。我已经在我的代码中以编程方式设置了他们的文本。
      【解决方案3】:

      首先,如何在C#.Net 2.0及以上版本的DataGridView中添加数字行RowHeadersWidth:

      // 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();
      }
      

      现在,您可以根据行标题的大小动态调整行标题的大小,就像在 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;
      }
      

      此解决方案仅适用于 .Net Framework 2.0 及更高版本,不适用于 CF。

      【讨论】:

        猜你喜欢
        • 2012-11-02
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多