发布网友 发布时间:2024-10-22 08:34
共1个回答
热心网友 时间:2024-11-08 23:41
黑体字下面是要这样吗? vb的代码 我不太懂,只会c#的, 但是给你个思路, 就是纵向合并单元格嘛。
我贴一段c#的代码,。, 您理解下, 应该不困难。。。
代码 把AllowUserToAddRows设置为false
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//纵向合并
if (this.dataGridView1.Columns["Name"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
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);
/**/
////绘制线条,这些线条是单元格相互间隔的区分线条,
////因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
if (e.RowIndex != this.dataGridView1.RowCount - 1)
{
try
{
if (e.Value.ToString() != this.dataGridView1.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);//下边缘的线
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}
catch (Exception ex)
{
}
}
else
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
//右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Handled = true;
}
}
}
}
下面是我用的datatable 这个dataset是网上copy的
DataSet dataset = new DataSet();//库
//表
DataTable myfriends = new DataTable("myfriends");
//列
DataColumn dcName = new DataColumn();
dcName.ColumnName = "Name";
dcName.DataType = typeof(string);
dcName.MaxLength = 8;
DataColumn dcPhone = new DataColumn();
dcPhone.ColumnName = "telephone";
dcPhone.DataType = typeof(string);
dcPhone.MaxLength = 11;
//添加列
myfriends.Columns.Add(dcName);
myfriends.Columns.Add(dcPhone);
//添加1行
DataRow row = myfriends.NewRow();//得到空行
row["Name"] = "小明";
row["telephone"] = "13888888888";
DataRow row1 = myfriends.NewRow();//得到空行
row1["Name"] = "小明";
row1["telephone"] = "15859859888";
DataRow row2 = myfriends.NewRow();//得到空行
row2["Name"] = "猪猪";
row2["telephone"] = "15999999999";
myfriends.Rows.Add(row);
myfriends.Rows.Add(row1);
myfriends.Rows.Add(row2);
//添加表到dataset
dataset.Tables.Add(myfriends);
dataGridView1.DataSource = myfriends;