C#,WinForm, 怎么合并DataGridView 中 纵向的两个单元格??请给出详细说明。
发布网友
发布时间:2022-12-18 08:45
我来回答
共2个回答
热心网友
时间:2023-09-18 04:07
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;
}
}
热心网友
时间:2023-09-18 04:07
此例合并第2.3列
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
DataTable table =tempTable();
DataGrid1.DataSource =table;
DataGrid1.DataBind();
}
}
DataTable tempTable()
{
DataTable table =new DataTable();
table.Columns.Add(new DataColumn( "ID ",typeof(System.Int32)));
table.Columns.Add(new DataColumn( "FirstName ",typeof(System.String)));
table.Columns.Add(new DataColumn( "LastName ",typeof(System.String)));
for(int i=0;i <5;i++)
{
DataRow row =table.NewRow();
row[ "ID "] =i;
row[ "FirstName "]=i.ToString();
row[ "LastName "]=i.ToString();
table.Rows.Add(row);
}
return table;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PreRender += new System.EventHandler(this.DataGrid1_PreRender);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_PreRender(object sender, System.EventArgs e)
{
for(int i=0;i <DataGrid1.Items.Count;i++)
{
TableCell cell1 =DataGrid1.Items[i].Cells[1];
TableCell cell2 =DataGrid1.Items[i].Cells[2];
cell1.Text +=cell2.Text;
cell1.ColumnSpan =2;
cell2.Visible =false;
}
}
C#,WinForm, 怎么合并DataGridView 中 纵向的两个单元格??请给出详...
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...
C#winform datagridview控件怎么合并单元格?
Datagridview合并单元格 源代码和实现说明:http://www.codeproject.com/KB/grid/MergedDataGridViewControl.aspx
C#DataGridView怎么合并单元格
// 如果下一行和当前行的数据不同,则在当前的单元格画一条底边线 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.Cell...
c# datagridview 能否实现单元格合并
完全的可以 使用自定义表格头部,创建一个具有两行的表头,第一行设置其colspan使其跨列,第二行让他和gridview的实际列相等就能实现你的效果了 做法是编辑gridview的RowCreated事件 if (e.Row.RowType == DataControlRowType.Header){ GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType....
关于c#窗体中datagridView合并单元格问题...
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e){ switch (e.Row.RowType){ case DataControlRowType.Header://第一行表头 TableCellCollection tcHeader = e.Row.Cells;tcHeader.Clear();tcHeader.Add(new TableHeaderCell());tcHeader[0].Attributes.Add("rowspan", "3...
Winform DataGridView 合并一行单元格
绑定DataGridView前,先把所有需要合并的行号记录到一个数组变量中 调用DataGridView的Bind方法后,会触发CellPainting事件 在CellPainting事件中,先判断行号是否在数组中 如果在数组中,清除单元格、修改背景色、仅绘制上下边框线 再判断是否为第一列(e.ColumnIndex为0)如果为第一列,设置单元格的内容为...
c#winfrom 将datagridview中的数据通过npoi 2.2.0版本导出到excel,可...
NPOI1.0就带有合并单元格功能,但是使用起来比微软的dll要麻烦一些,//设置一个合并单元格区域,使用上下左右定义CellRangeAddress区域//CellRangeAddress四个参数为:起始行,结束行,起始列,结束列sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));你第一行的表头,就可以写成,sheet....
C#.net DataGridView 合并表头单元格
//第一行单元格的合并为1 gvManualSign.Rows[0].Cells[j].RowSpan = 1;nextCell = 0;//循环遍历GridView中每行的值 for (i = 1; i < gvManualSign.Rows.Count; i++){ //当遍历的值等于第一行中的值时,合并单元格++ if (gvManualSign.Rows[i].Cells[j].Text == gvText){ gv...
C# Winform的dataGridView中单元格怎样显示多行数据
DataGridView单元格显示多行的设置方法 第一、设置RowsDefaultCellStyle的WrapMode属性值为true(表示支持多行显示)第二、设置AllowUserToResizeColumns属性值为true(表示用户拉大行高)第三、设置AutoSizeRowsMode属性值为AllCells(表示所有单元格自动调节单元格高度),属性值为DisplayedCells(表示当前单元格自动...
两个datagridview的数据共享的问题!C#的
新建一个datatable数据结构和datagridview1中的datasource一样的。然后双击datagridview1中的一行。就把该行数据添加到datatable中。然后datagridview2的datasource设置成为datatable就行了。