c#关于单元格合并居中显示等基础操作

单元格内容过多,一行显示改为自动换行,行高自适应

点击datagridview控件,在属性框中
设置DataGridView的RowsDefaultCellStyle属性点开,在WrapMode选项中改为true
设置DataGridView的AutoSizeRowsMode属性改为DisplayedCellsExceptHeaders
表格数据居中设置

//设置每行数据居中
dataTable.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//设置列名居中
dataTable.ColumnHeadersDefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleCenter;

关于鼠标悬浮表格,气泡显示该单元格所有数据
1、设置DataGridView的ShowCellToolTips 属性为true(不设置会出现NULL报错)
2、添加触发事件方法

//在CellMouseEnter触发事件中添加
private void dataTable_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{ 
      if (e.ColumnIndex < 0 || e.RowIndex < 0 || dataTable.Rows.Count <= 0) return;
      dataTable.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = (dataTable.Rows[e.RowIndex].Cells[e.ColumnIndex].Value ?? string.Empty).ToString();
}        

关于合并上下相邻数据相同的单元格
1、设置DataGridView的AllowUserToAddRows属性为true
2、添加触发事件方法

//在CellPainting触发事件中添加
 private void dataTable_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 { 
     //e.ColumnIndex == 1 || e.ColumnIndex == 2 || e.ColumnIndex == 3(最后一列不绘制) 
     if (e.RowIndex >= 0 && (e.ColumnIndex == 1 || e.ColumnIndex == 2 || e.ColumnIndex == 3) && e.Value.ToString() != string.Empty)
     { 
         #region
                int UpRows = 0;//上面相同的行数
                int DownRows = 0;//下面相同的行数
                int count = 0;//总行数
                int cellwidth = e.CellBounds.Width;//列宽
                for (int i = e.RowIndex; i < this.dataTable.Rows.Count; i++) //获取下面的行数
                { 
                    if (this.dataTable.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(e.Value.ToString()))
                    { 
                        DownRows++;
                    }
                    else
                    { 
                        break;
                    }
                }
                for (int i = e.RowIndex; i >= 0; i--) //获取上面的行数
                { 
                    if (this.dataTable.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(e.Value.ToString()))
                    { 
                        UpRows++;
                    }
                    else
                    { 
                        break;
                    }
                }
                count = UpRows + DownRows - 1;//总行数 
                using (Brush gridBrush = new SolidBrush(this.dataTable.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                { 
                    using (Pen gridLinePen = new Pen(gridBrush))
                    { 
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds); //清除单元格
                        if (e.Value != null)
                        { 
                            int cellheight = e.CellBounds.Height;
                            SizeF size = e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font);
                            e.Graphics.DrawString((e.Value).ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + (cellwidth - size.Width) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - size.Height) / 2, StringFormat.GenericDefault);
                        }
                        //如果下一行数据不等于当前行数据,则画当前单元格底边线
                        if (e.RowIndex < this.dataTable.Rows.Count - 1 && this.dataTable.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
                        { 
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        }
                        if (e.RowIndex == this.dataTable.Rows.Count - 1)
                        { 
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            count = 0;
                        }
                        //画grid右边线 
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                        e.Handled = true;
               }
           }
           #endregion
      }
}       
    原文作者:十韶
    原文地址: https://blog.csdn.net/weixin_43509549/article/details/119179763
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞