dataGridView 单击主标头排序或单击后收缩子标头

bombn25 2011-04-07 09:30:15
我写的代码可以实现dataGridView 像TreeView样能展开收缩(可折叠) (比如 一列中主标头 单位 子标头是福州公司,上海公司)但是点下福州公司下的数据再点下“单位”主标头, 那些福州公司下的数据无法折叠 直接串到第二行。 有没办法当单击“单位”时候 能把福州公司下的数据折叠,或者 禁止点击“单位”标头。 小弟水平有限请高手补充几行代码实现. 谢谢!
public Form1()
{
InitializeComponent();
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.HeaderText = "单位";
collapseDataGridView1.Columns.Insert(0,column);

column = new DataGridViewTextBoxColumn();
column.HeaderText = "联系人";
collapseDataGridView1.Columns.Insert(1, column);

column = new DataGridViewTextBoxColumn();
column.HeaderText = "销售额";
collapseDataGridView1.Columns.Insert(2, column);

}

private void Form1_Load(object sender, EventArgs e)
{
InitDataGridView(this.collapseDataGridView1);
}
private void InitDataGridView(DataGridView dgv)
{
/*第一行可折叠行*/
CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
collapseRow.IsCollapse = true;

DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
cell.Value = "福州01科技有限公司";
collapseRow.Cells.Add(cell);

cell = new DataGridViewTextBoxCell();
cell.Value = "王小二";
collapseRow.Cells.Add(cell);

cell = new DataGridViewTextBoxCell();
cell.Value = "10000.0000";
collapseRow.Cells.Add(cell);

/*第二行可折叠行*/
CollapseDataGridViewRow collapseRow2 = new CollapseDataGridViewRow();
collapseRow2.IsCollapse = true;

DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
cell2.Value = "国横有限公司";
collapseRow2.Cells.Add(cell2);

cell2 = new DataGridViewTextBoxCell();
cell2.Value = "张国";
collapseRow2.Cells.Add(cell2);

cell2 = new DataGridViewTextBoxCell();
cell2.Value = "10000.0000";
collapseRow2.Cells.Add(cell2);

/*第三行可折叠行*/
CollapseDataGridViewRow collapseRow3 = new CollapseDataGridViewRow();
collapseRow3.IsCollapse = true;

DataGridViewTextBoxCell cell3 = new DataGridViewTextBoxCell();
cell3.Value = "飞鸿有限公司";
collapseRow3.Cells.Add(cell3);

cell3 = new DataGridViewTextBoxCell();
cell3.Value = "陈宝";
collapseRow3.Cells.Add(cell3);

cell3 = new DataGridViewTextBoxCell();
cell3.Value = "60000.0000";
collapseRow3.Cells.Add(cell3);


/*子行的行头标题*/
DataGridViewRow headerRow = new DataGridViewRow();
cell = new DataGridViewTextBoxCell();
cell.Value = "订单号";
headerRow.Cells.Add(cell);

cell = new DataGridViewTextBoxCell();
cell.Value = "订单日期";
headerRow.Cells.Add(cell);

cell = new DataGridViewTextBoxCell();
cell.Value = "订单金额";
headerRow.Cells.Add(cell);

cell = new DataGridViewTextBoxCell();
cell.Value = "业务员";
headerRow.Cells.Add(cell);


//将标题行添加至可折叠行的子行中
collapseRow.Rows.Add(headerRow);
collapseRow2.Rows.Add(headerRow);


DataGridViewRow dataRow3 = new DataGridViewRow();
cell3 = new DataGridViewTextBoxCell();
cell3.Value = "XSDD075503";
dataRow3.Cells.Add(cell3);
collapseRow3.Rows.Add(headerRow);
collapseRow3.Rows.Add(dataRow3);


for (int i = 0; i < 5; i++)
{
DataGridViewRow dataRow = new DataGridViewRow();
cell = new DataGridViewTextBoxCell();
cell.Value = "XSDD075501" + i.ToString();
dataRow.Cells.Add(cell);


cell = new DataGridViewTextBoxCell();
cell.Value = "2011-4-1" + i.ToString();
dataRow.Cells.Add(cell);

cell = new DataGridViewTextBoxCell();
cell.Value = i * 1000;
dataRow.Cells.Add(cell);

cell = new DataGridViewTextBoxCell();
cell.Value = "SZMMY0755";
dataRow.Cells.Add(cell);
//将订单的数据行添加至可折叠行的子行中
collapseRow.Rows.Add(dataRow);

collapseRow2.Rows.Add(dataRow);

}

//将可折叠的行添加至GridView
dgv.Rows.Add(collapseRow);
dgv.Rows.Add(collapseRow2);
dgv.Rows.Add(collapseRow3);


}
...全文
154 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bombn25 2011-04-08
  • 打赏
  • 举报
回复
我发给你看看吧 不过是手动绑定数据的。 你要是解决了问题 记得告诉我声或发回我邮箱
贪玩的老鼠 2011-04-08
  • 打赏
  • 举报
回复
dataGridView 像TreeView样能展开收缩
可以吗,能否给我一点代码64236247@qq.com
bombn25 2011-04-08
  • 打赏
  • 举报
回复
没人么?
bombn25 2011-04-07
  • 打赏
  • 举报
回复
我在代码后面添加了代码
int j =(int)dgv.Columns.Count;
for (int k = 0; k < j; k++)
{ dgv.Columns[k].SortMode = DataGridViewColumnSortMode.NotSortable; }

禁止主标头点击可以成功了。 要是不用禁止主标头 使子标头下的行能自动折叠 如何实现?? 我自己在做做看 有哪个人懂这点小技术的 教我下

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧