为什么rowspan换行符会没有用

首先:rowspan是跨行符,

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>合并单元格</title>
	</head>
	<body>
		<table border="1"> <!-- colspan 单元格跨列 -->	
		<thead>
			<tr>
				<th>语文</th></th>
				<th colspan="2">数学</th>
				<th rowspan="2">123</th>
				</thead>
			</tr>
			<tr>
				<td>80</td>
				<td >100</td>
				<td>90</td>
			</tr>
		</table>
	</body>
</html>

上面代码显示出来的效果是:在这里插入图片描述
可以看出rowspan跨行符没有效果,colspan列符有用,
如果取消<thead>可以来看一看:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>合并单元格</title>
	</head>
	<body>
		<table border="1"> <!-- colspan 单元格跨列 -->	
			<tr>
				<th>语文</th></th>
				<th colspan="2">数学</th>
				<th rowspan="2">123</th>
			</tr>
			<tr>
				<td>80</td>
				<td >100</td>
				<td>90</td>
			</tr>
		</table>
	</body>
</html>

显示效果是:
在这里插入图片描述
这里可以看出rowspan是有用的:
<thead控制了表头,我目前只知道去了<thead>就可以实现rowspan的功能,具体出于什么原因期待解答,

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果C#的DataGridView控件没有提供RowSpan属性,那么您可以使用以下方法来合并第二行数据相同的列并且居中: ```csharp private void MergeRows() { int rowCount = dataGridView1.Rows.Count; for (int i = rowCount - 2; i >= 0; i--) { DataGridViewRow currentRow = dataGridView1.Rows[i]; DataGridViewRow previousRow = dataGridView1.Rows[i + 1]; for (int j = 0; j < dataGridView1.Columns.Count; j++) { if (currentRow.Cells[j].Value == previousRow.Cells[j].Value) { // 设置当前单元格的高度为前一行单元格的高度 currentRow.Cells[j].Style.Padding = new Padding(0, 0, 0, 0); currentRow.Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter; currentRow.Cells[j].Style.BackColor = previousRow.Cells[j].Style.BackColor; currentRow.Cells[j].Style.ForeColor = previousRow.Cells[j].Style.ForeColor; DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell(); cell.Value = ""; cell.Style.BackColor = dataGridView1.BackgroundColor; currentRow.Cells[j] = cell; // 隐藏前一行单元格 previousRow.Cells[j].Visible = false; } } } // 居中显示 DataGridViewCellStyle style = new DataGridViewCellStyle(); style.Alignment = DataGridViewContentAlignment.MiddleCenter; foreach (DataGridViewColumn col in dataGridView1.Columns) { col.DefaultCellStyle = style; } } ``` 在这个方法中,我们首先获取DataGridView中行的数量,并在倒序循环中比较相邻的两个行的单元格值是否相同。如果相同,我们设置当前单元格的高度为前一行单元格的高度,并使用一个空的单元格替换当前单元格,以达到合并单元格的效果。最后,我们将所有列的单元格对齐方式设置为居中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值