1、最简单的方法就是到网上找个控件。
2、如果用DataGridView,主要处理2个问题:表头重绘、数据绑定。
比较简单的方法是:
把“周一、周二……”当成Column,把“工作代码、专案代码……”以及数据当成数据,这样重绘的工作最少。
这是我示例的运行效果,还有些细节估计你自己能处理了:
代码写得很乱,只是提供一个思路,你根据自己实际情况修改吧:
Column按钮代码:
string cTitle = null;
for (int i = 1; i <= 7; i++)
{
switch (i)
{
case 1:
cTitle = "週一";
break;
case 2:
cTitle = "週二";
break;
case 3:
cTitle = "週三";
break;
case 4:
cTitle = "週四";
break;
case 5:
cTitle = "週五";
break;
case 6:
cTitle = "週六";
break;
case 7:
cTitle = "週日";
break;
}
dataGridView1.Columns.Add("wCode" + i.ToString(), cTitle);
dataGridView1.Columns.Add("dCode" + i.ToString(), cTitle);
dataGridView1.Columns.Add("timeSpace" + i.ToString(), cTitle);
dataGridView1.Columns.Add("remark" + i.ToString(), cTitle);
}
//隱藏RowHeader
dataGridView1.RowHeadersVisible = false;
//設置自動換行
foreach(DataGridViewColumn c in dataGridView1.Columns)
c.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
//設置自動行高
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
Row按钮代码(这里数据只是为了看效果,你根据自己情况选择合适的处理方式):
string[][] data = new string[3][];
data[0] = new string[28] { "工作代碼", "專案代碼", "工時", "說明", "工作代碼", "專案代碼", "工時", "說明", "工作代碼", "專案代碼", "工時", "說明", "工作代碼", "專案代碼", "工時", "說明", "工作代碼", "專案代碼", "工時", "說明", "工作代碼", "專案代碼", "工時", "說明", "工作代碼", "專案代碼", "工時", "說明" };
data[1] = new string[28] { "A3", "", "0.5h", "晨會", "A3", "", "0.5h", "晨會", "A3", "", "0.5h", "晨會", "A3", "", "0.5h", "晨會", "A3", "", "0.5h", "晨會", "A3", "", "0.5h", "晨會", "A3", "", "0.5h", "晨會" };
data[2] = new string[4] { "C6", "SHEIM1106.01", "2h", "被劃掉的說明,內容挺多的……" };
dataGridView1.RowCount = data.Length;
for (int i = 0; i < data.Length; i++)
{
for (int j = 0; j < data[i].Length; j++)
dataGridView1.Rows[i].Cells[j].Value = data[i][j];
}
dataGridView1.Rows[0].DefaultCellStyle.BackColor =
dataGridView1.Rows[0].DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
dataGridView1.Rows[0].DefaultCellStyle.SelectionForeColor = Color.Black;
最主要的是DataGridView的CellPainting事件处理:
int left = 0;
int width1 = 0;
int width2 = 0;
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1)
{
using (Brush gridBrush = new SolidBrush(Color.Black), backColorBrush = new SolidBrush(Color.Maroon))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
if (e.ColumnIndex % 4 == 0)//这里用了硬编码……
{
//左侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, //x1,y1,x2,y2
e.CellBounds.Top, e.CellBounds.Left - 1,
e.CellBounds.Bottom - 1);
left = e.CellBounds.Left;
width1 = e.CellBounds.Width;
width2 = 0;
for (int i = 1; i <= 3; i++)
width2 += dataGridView1.Columns[e.ColumnIndex + i].Width;
}
//标题
SizeF sf = e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font);
float lstr = (width1 + width2 - sf.Width) / 2;
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.White,
left + lstr,e.CellBounds.Y + 2,StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
如果刷新有问题,可在DataGridView的Scroll、ColumnWidthChanged等事件中加上刷新的处理。
我做B/S的 datagridview好久没用了,不过你的思路有点意思,谢谢......
绑定数据源不行吗?