【问题标题】:Howto add a row with horizontally merged cells to DataGridView如何将具有水平合并单元格的行添加到 DataGridView
【发布时间】:2019-07-10 08:32:18
【问题描述】:

我正在使用 WinForms。 如何将具有水平合并单元格的行添加到 DataGridView? 下图中的 WorkingTimeTouchdown Count 单元格:

【问题讨论】:

  • 请检查这个帖子,它几乎回答了你的问题stackoverflow.com/questions/16774966/…
  • 如我所见,您建议我在dataGridView1_CellPainting 事件中设置e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None?这不适合水平合并,因为第一列(如果我将 Working Time 字符串放入该行的第一个单元格)将根据其长度自动调整大小。它看起来不像合并列,而是 like that
  • 这不是很好的支持。要解决方法,您可以隐藏一些边框,但除非您拥有它,否则内容不会居中。See here
  • @TaW - 我制作了一些代码,对我有用(不仅是边框,还有居中的内容以及使用自定义 CellPainting 事件)。但我无法回答我自己的问题,因为你关闭了它。请打开它 - 这样就可以添加完整和正确的解决方案。谢谢。

标签: c# winforms datagridview


【解决方案1】:

这是第一行水平合并的工作代码(用于使单元格内容居中和正确绘制边框),初始代码为found on MSDN

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    //mering all cells in a first row
    if (e.RowIndex == 0)
    {
        if (e.ColumnIndex == 0)
        {
            e.PaintBackground(e.ClipBounds, true);
            Rectangle r = e.CellBounds;

            for (int i = 1; i < (sender as DataGridView).ColumnCount; i++)
                r.Width += (sender as DataGridView).GetCellDisplayRectangle(i, 0, true).Width;

            r.Width -= 1;
            r.Height -= 1;

            using (SolidBrush brBk = new SolidBrush(e.CellStyle.BackColor))
            using (SolidBrush brFr = new SolidBrush(e.CellStyle.ForeColor))
            {
                e.Graphics.FillRectangle(brBk, r);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, brFr, r, sf);
            }

            e.Handled = true;
       }
       else
            if (e.ColumnIndex > 0)
            {
                using (Pen p = new Pen((sender as DataGridView).GridColor))
                {
                    //bottom line of a cell 
                    e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
                    //right vertical line of a last cell in a row
                    if (e.ColumnIndex == (sender as DataGridView).ColumnCount - 1)
                        e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                }

                e.Handled = true;
            }
     }
}


private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
     //force redraw first row when scrolling
     for (int i = 0; i < (sender as DataGridView).ColumnCount; i++)
         (sender as DataGridView).InvalidateCell(i, 0);
}

【讨论】:

    猜你喜欢
    • 2013-10-03
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多