问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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 &lt; dataGridView1.Rows.Count - 1 &amp;&amp; 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 &lt; 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就行了。

两个excel表格纵向合并 oracle两张表纵向合并 两个数据表怎么合并成一个 excel怎么合并两个图标 将两个文件纵向合并 怎么把两张表合并成一个 oracle两个结果集合并 mysql两个表合并成一个表 两个相同表格合并
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
五羊本田摩托车125c太费油 拖泥带水,束手无策,是什么生肖? 北汽幻速S7两年车都有哪些费用? 新北汽幻速s7这款车后期的维护大致要多少钱?花费大吗? 新学期的打算250字作文。。。急急 一般冷暴力的话有什么? 逻辑学怎么造句 想知道: 松原市 从长岭县到集体乡胜利村怎么坐公交 孕期不同阶段进行自然胎教的方法 香港路•浅水湾配套信息 【急啊】船是钢铁做的(密度比水大),为什么船还会浮在水面? 请问两全保险是包含什么的? 两全保险的险种都有哪些? 汽车发电机充电量过大会充坏电瓶吗 皮卡车电瓶充电量太大 电瓶爆了怎么回事? 摩托车充电量太大怎么回事, 我的电瓶因充电电流过大造成电解液几乎干涸。且好几天没发现。请问怎么办? 摩托车充电量过大怎么办? 给水电瓶充电6个孔要打开吗 这种水电瓶充电直接充电可以吗?还是把盖子打开充电啊 幼师培训经历怎么填写 三月份绣球萌发后又遇到降温会冻死吗 大花绣球越冬技巧,光照控温增强水肥管理 春季零下1度绣球会死吗 商务英语BEC中级写作模板:投诉信 客户投诉除用8D回复,还可用什么格式回复 接到客诉后品质部回复应该用怎样的格式? 八年级语文《核舟记》教学设计 为什么13不能将照片存储为视频这个按钮 用QQ 表情怎么写字??? 关于c#窗体中datagridView合并单元格问题... C#Winform中DataGridView合并单元格的问题? 九寨沟有哪九个寨 恐龙是怎么灭绝的啊? 什么是法身大士? 什么叫初住菩萨/什么叫初住菩萨 华严经中说41位法身大士都是报身,那么圆教七信位到十信位这些已经出轮回的菩萨是什么身? 形容让人一见就害怕的成语 形容让人看了就害怕的词语 ◇02-19飞傲K5推HD650到底怎么样,有 开车每小时2O公里是什么意思? 一跟轻质弹簧竖直直立在水平地面上 如图所示,一根轻弹簧竖直立在水平面上,下端固定.在弹簧正上方有一个物块从高处自由下落到弹簧上端,将 如题所示,一轻质弹簧竖直放置,下端被固定在水平面上,弹簧自然长度为L0 如图所示,一根轻弹簧下端固定,竖立在水平面上 如图所示,劲度系数为k的轻弹簧竖直固定在水平面上 (1)如图所示,一轻质弹簧竖直立在水平地面上,弹簧一端固定在地面上.一小球从高处自由下落到弹簧上端 如图所示,一轻质弹簧竖直放置,下端被固定于水平地面上,弹簧自然长度为L0,先将一质量为m的A球放于弹簧 酒驾判一个月缓两个月有案底吗 醉驾无事故拘役一个月缓两月调销驾驶证五年五年内刑事拘留轻伤二级属累犯