首页 新闻 会员 周边 捐助

求救,关于gridview数据分组显示!!!!!格式如下图

1
悬赏园豆:200 [已解决问题] 解决于 2011-11-10 11:40

在线等啊.......

yaokoowp的主页 yaokoowp | 初学一级 | 园豆:8
提问于:2011-10-26 16:35
< >
分享
最佳答案
1

1、最简单的方法就是到网上找个控件。
2、如果用DataGridView,主要处理2个问题:表头重绘、数据绑定。

比较简单的方法是:
把“周一、周二……”当成Column,把“工作代码、专案代码……”以及数据当成数据,这样重绘的工作最少。
这是我示例的运行效果,还有些细节估计你自己能处理了:

代码写得很乱,只是提供一个思路,你根据自己实际情况修改吧:

Column按钮代码:

View Code
            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按钮代码(这里数据只是为了看效果,你根据自己情况选择合适的处理方式):

View Code
            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事件处理:

View Code
        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等事件中加上刷新的处理。



收获园豆:100
Higel | 菜鸟二级 |园豆:464 | 2011-10-27 15:05

我做B/S的 datagridview好久没用了,不过你的思路有点意思,谢谢......

yaokoowp | 园豆:8 (初学一级) | 2011-10-28 12:55
其他回答(2)
0

绑定数据源不行吗?

喬喬AI | 园豆:996 (小虾三级) | 2011-10-26 19:42
0
收获园豆:100
邀月 | 园豆:25475 (高人七级) | 2011-10-27 09:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册