datagrid合并单元格,stringgrid合并单元格

http://www.itjxue.com  2023-01-12 20:42  来源:未知  点击次数: 

Winform DataGridView 合并一行单元格

Windows?Forms?DataGridView?没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和?Graphics.DrawString?自己来“画”。

参考:

具体思路:

绑定DataGridView前,先把所有需要合并的行号记录到一个数组变量中

调用DataGridView的Bind方法后,会触发CellPainting事件

在CellPainting事件中,先判断行号是否在数组中

如果在数组中,清除单元格、修改背景色、仅绘制上下边框线

再判断是否为第一列(e.ColumnIndex为0)

如果为第一列,设置单元格的内容为部门名称(e.Value = “...”)

C#,WinForm, 怎么合并DataGridView 中 纵向的两个单元格??请给出详细说明。

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor);

SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);

SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);

int cellheight;

int fontheight;

int cellwidth;

int fontwidth;

int countU = 0;

int countD = 0;

int count = 0;

// 对第1列相同单元格进行合并

if (e.ColumnIndex == 0 e.RowIndex != -1)

{

cellheight = e.CellBounds.Height;

fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;

cellwidth = e.CellBounds.Width;

fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;

Pen gridLinePen = new Pen(gridBrush);

string curValue = e.Value == null ? "" : e.Value.ToString().Trim();

string curSelected = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value == null ? ""

: this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();

if (!string.IsNullOrEmpty(curValue))

{

for (int i = e.RowIndex; i this.dataGridView1.Rows.Count; i++)

{

if (this.dataGridView1.Rows[i].Cells[0].Value.ToString().Equals(curValue))

{

this.dataGridView1.Rows[i].Cells[0].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

this.dataGridView1.Rows[i].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

countD++;

}

else

{

break;

}

}

for (int i = e.RowIndex; i = 0; i--)

{

if (this.dataGridView1.Rows[i].Cells[0].Value.ToString().Equals(curValue))

{

this.dataGridView1.Rows[i].Cells[0].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

this.dataGridView1.Rows[i].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

countU++;

}

else

{

break;

}

}

count = countD + countU - 1;

if (count 2) { return; }

}

if (this.dataGridView1.Rows[e.RowIndex].Selected)

{

backBrush.Color = e.CellStyle.SelectionBackColor;

fontBrush.Color = e.CellStyle.SelectionForeColor;

}

e.Graphics.FillRectangle(backBrush, e.CellBounds);

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X , e.CellBounds.Y - cellheight * (countU - 1) + (cellheight * count - fontheight) / 2);

if (countD == 1)

{

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

count = 0;

}

// 画右边线

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

e.Handled = true;

}

}

C#DataGridView怎么合并单元格

需要重绘单元格。在下面这个事件里写就可以了,下面这个例子只是对第一列中内容相同的数据合并,你可以根据自己的实际情况做调整,可以写了一个控件,可以随时设定合并哪一列,一次合并几行数据。

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

// 对第1列相同单元格进行合并

if (e.ColumnIndex == 0 e.RowIndex != -1)

{

using

(

Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),

backColorBrush = new SolidBrush(e.CellStyle.BackColor)

)

{

using (Pen gridLinePen = new Pen(gridBrush))

{

// 清除单元格

e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

// 画 Grid 边线(仅画单元格的底边线和右边线)

// 如果下一行和当前行的数据不同,则在当前的单元格画一条底边线

if (e.RowIndex dataGridView1.Rows.Count - 1

dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() !=

e.Value.ToString())

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left+2,

e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,

e.CellBounds.Bottom - 1);

//画最后一条记录的底线

if (e.RowIndex == dataGridView1.Rows.Count - 1)

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left+2,

e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,

e.CellBounds.Bottom - 1);

// 画右边线

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,

e.CellBounds.Top, e.CellBounds.Right - 1,

e.CellBounds.Bottom);

// 画左边线

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left +2,

e.CellBounds.Top, e.CellBounds.Left +2,

e.CellBounds.Bottom);

// 画(填写)单元格内容,相同的内容的单元格只填写第一个

if (e.Value != null)

{

if (e.RowIndex 0

dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==

e.Value.ToString())

{

}

else

{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Black, e.CellBounds.X + 2,

e.CellBounds.Y + 5, StringFormat.GenericDefault);

}

}

e.Handled = true;

}

}

}

}

C#中怎样实现datagridview合并指定单元格?

C#DataGridView合并单元格:

public void dgCellPainting(DataGridView dgStandard, string strColumnName, DataGridViewCellPaintingEventArgs e)

{

if (dgStandard.Rows.Count 0)

{

if (dgStandard.Rows[0].Cells[0].Value.ToString().Trim() != string.Empty)

{

try

{

if (dgStandard.Columns[strColumnName].Index == e.ColumnIndex e.RowIndex = 0)

{

using (

Brush gridBrush = new SolidBrush(dgStandard.GridColor),

backColorBrush = new SolidBrush(e.CellStyle.BackColor))

{

using (Pen gridLinePen = new Pen(gridBrush))

{

// 擦除原单元格背景

e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

if (e.RowIndex != dgStandard.RowCount - 1)

{

if (e.Value.ToString() != dgStandard.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())

{

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线

}

}

else

{

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线

}

//右侧的线

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, //x1,y1,x2,y2

e.CellBounds.Top, e.CellBounds.Right - 1,

e.CellBounds.Bottom - 1);

if (e.RowIndex == 0)

{

//绘制值

if (e.Value != null)

{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Crimson,

e.CellBounds.X +(e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width)/2,

e.CellBounds.Y + 2, StringFormat.GenericDefault);

}

}

else

{

if (e.Value.ToString() != dgStandard.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString())

{

//绘制值

if (string.IsNullOrEmpty(e.Value.ToString().Trim()).Equals(false))

{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Crimson, e.CellBounds.X + (e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width) / 2,

e.CellBounds.Y + 2, StringFormat.GenericDefault);

}

}

}

e.Handled = true;

}

}

}

}

catch { }

}

}

}

参考:

WPF DataGrid 如何合并内容相同的单元格

第二个问题的解决方法我还是觉得用DataGridTemplateColumn比较好解决。拿前台代码说明,后台代码建立也没啥大问题的。

DataGridTemplateColumn

DataGridTemplateColumn.CellTemplate

DataTemplate

TextBlock Text="{Binding Property1}" HorizontalAlignment="Center"/

/DataTemplate

/DataGridTemplateColumn.CellTemplate

/DataGridTemplateColumn

只要在后台设定TextBlock的HorizontalAlignment就可以设置对其方式。

至于第一个问题,合并单元格比较有困难。一般只有第三方控件提供这种方法。wpf的datagrid是做不到的。

c# datagridview 能否实现单元格合并

完全的可以

使用自定义表格头部,创建一个具有两行的表头,第一行设置其colspan使其跨列,第二行让他和gridview的实际列相等就能实现你的效果了

做法是编辑gridview的RowCreated事件

if (e.Row.RowType == DataControlRowType.Header)

{

GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

Literal newCells = new Literal();

newCells.Text =""工资'

TableCellCollection cells = e.Row.Cells;

TableHeaderCell headerCell = new TableHeaderCell();

headerCell.ColumnSpan = 3;

//这条语句使表格的这一列跨了3列

headerCell.Controls.Add(newCells);

rowHeader.Cells.Add(headerCell);

//可以用同样的语句再添加一些单元格

rowHeader.Visible = true;

//添加到 GridView1

GridView1.Controls[0].Controls.AddAt(0, rowHeader);

//这里重复上面的语句可以添加表头第二行

//GridView1.Controls[0].Controls.AddAt(1, rowHeader);

}

(责任编辑:IT教学网)

更多

推荐CGI/Perl教程文章