C# Winform DataGridView中实现二维表头

C# Winform DataGridView中实现二维表头,第1张

1,继承DataGridView,添加表头信息类。

2,添加CellPainting,代码如下:

private void DataGridViewEx_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex == -1)

{

// int w = dataGridView1.HorizontalScrollingOffset + dataGridView1.TopLeftHeaderCell.Size.Width + dataGridView1.Columns[0].Width + 10

Rectangle newRect = new Rectangle(e.CellBounds.X + 1,

e.CellBounds.Y + 1, e.CellBounds.Width - 4,

e.CellBounds.Height - 4)

using (

Brush gridBrush = new SolidBrush(this.GridColor),

backColorBrush = new SolidBrush(e.CellStyle.BackColor))

{

using (Pen gridLinePen = new Pen(gridBrush))

{

// Erase the cell.

e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

// Draw the grid lines (only the right and bottom lines

// DataGridView takes care of the others).

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,

e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,

e.CellBounds.Bottom - 1)

if (e.ColumnIndex >-1 &&topRow!=null&&topRow.Cells[e.ColumnIndex].ColSpan>1)

{

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,

e.CellBounds.Top + e.ClipBounds.Height / 2, e.CellBounds.Right - 1,

e.CellBounds.Bottom)

}

else

{

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,

e.CellBounds.Top, e.CellBounds.Right - 1,

e.CellBounds.Bottom)

}

// Draw the inset highlight box.

// e.Graphics.DrawRectangle(Pens.Blue, newRect)

int scale = e.CellBounds.Height/3

if (e.ColumnIndex >-1 &&topRow.Cells[e.ColumnIndex].Text != null)

{

scale= e.CellBounds.Height / 2

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - e.CellBounds.Height / 2, e.CellBounds.Right, e.CellBounds.Bottom - e.CellBounds.Height / 2)

}

// Draw the text content of the cell, ignoring alignment.

if (e.Value != null)

{

e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

Brushes.Crimson, e.CellBounds.X + 2,

e.CellBounds.Y + scale+ 2, StringFormat.GenericDefault)

}

if (e.ColumnIndex >-1 && topRow.Cells[e.ColumnIndex].RelateIndex >-1 &&topRow.Cells[e.ColumnIndex].Text!=null)

{

Rectangle recCell = new Rectangle(e.CellBounds.X - 1 - topRow.Cells[e.ColumnIndex].SpanRowWith,

e.CellBounds.Y + 1, topRow.Cells[e.ColumnIndex].SpanRowWith,

e.CellBounds.Height / 2)

StringFormat sf = new StringFormat()

sf.Alignment = StringAlignment.Center

e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Crimson, recCell, sf)

}

e.Handled = true

}

}

}

}

3,调用方法

dataGridViewEx1.TopRow.Cells[2].Text = "入库"

dataGridViewEx1.TopRow.Cells[2].ColSpan = 2

dataGridViewEx1.TopRow.Cells[4].Text = "出库"

dataGridViewEx1.TopRow.Cells[4].ColSpan = 24,效果图

至于表尾合计,也做出了原型。二维表头+表尾合计,基本上满足需求了。

你应该现自定义你的datagridview列的名称(点击datagridview右上方的三角,选择编辑列,进入编辑列的界面,如果还不懂就慢慢摸索一下吧。),然后你再将你定义的列跟你的ds.Table[0]的列绑定。当然还有一个不使用自定义列的方式就是在产生ds.Table[0]这个表时,将Table[0]的各个字段都转换成你要显示的中文的列的名称。

另外你不应该使用语句:

datagridview1.datasource=ds.Table[0].toString()

而应该使用:

datagridview1.datasource=ds.Table[0]

实现思路:把每一列的【SortMode】属性设置为【NotSortTable】

代码实现:

for(int i=0i <this.dataGridView1.Columns.Counti++)

{

this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic

}

添加dataGridView1的ColumnHeaderMouseClick事件:

private int sortOrder = 0

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{

if (sortOrder == 0)

{

this.dataGridView1.Sort(this.dataGridView1.Columns[e.ColumnIndex], ListSortDirection.Descending)

sortOrder++

}

else

{

this.dataGridView1.Sort(this.dataGridView1.Columns[e.ColumnIndex], ListSortDirection.Ascending)

sortOrder--

}

this.dataGridView1.Columns[0].HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.None

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11412553.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存