DataGridView怎样合并单元格?

Dreamdimension 2005-12-20 04:16:00
ru ti
...全文
1130 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dreamdimension 2005-12-24
  • 打赏
  • 举报
回复
好,Thanks
singlepine 2005-12-20
  • 打赏
  • 举报
回复
http://singlepine.cnblogs.com/articles/282990.html
Cnapollo 2005-12-20
  • 打赏
  • 举报
回复
合并列:参见下面合并第一列(此列内容我只取了第一列内容)
private void DataGrid1_PreRender(object sender, System.EventArgs e)
{
//合并2.3行,同时合并第一列
for(int i=0;i<DataGrid1.Items.Count;i++)
{
//合并2.3行
TableCell cell1 =DataGrid1.Items[i].Cells[1];
TableCell cell2 =DataGrid1.Items[i].Cells[2];
cell1.Text +=cell2.Text;
cell1.ColumnSpan =2;
cell2.Visible =false;
//合并第一列
TableCell cell =DataGrid1.Items[i].Cells[0];
cell.RowSpan =DataGrid1.Items.Count;
if(i>0)
cell.Visible=false;

}


}
Cnapollo 2005-12-20
  • 打赏
  • 举报
回复

此例合并第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;

}

}
Aallonlin 2005-12-20
  • 打赏
  • 举报
回复
up

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧