C# DataGrid合并单元格
1.栏位枚举
private enum DataGridColumn { ROWNUM = 0, EMPID, EMPNAME, SEX, SALARY, ADRRESS, PHONE, TEL, POSITIION, REMARK }
2.合并函数
private void SpanRow(DataGrid dg) { int i = 0; int j = 0; int rowNum = 0;//序号 int rowSpan; string SEX = ""; //相同SEX,合并SEX单元格 for (i = 0; i < dg.Items.Count; i++) { rowSpan = 1; SEX = dg.Items[i].Cells[(int)DataGridColumn.SEX].Text; for (j = i + 1; j < dg.Items.Count; j++) { if (string.Compare(SEX, dg.Items[j].Cells[(int)DataGridColumn.SEX].Text) == 0) { rowSpan += 1; dg.Items[i].Cells[(int)DataGridColumn.SEX].RowSpan = rowSpan; dg.Items[j].Cells[(int)DataGridColumn.SEX].Visible = false; dg.Items[i].Cells[(int)DataGridColumn.EMPNAME].RowSpan = rowSpan; dg.Items[j].Cells[(int)DataGridColumn.EMPNAME].Visible = false; dg.Items[i].Cells[(int)DataGridColumn.EMPNAME].RowSpan = rowSpan; dg.Items[j].Cells[(int)DataGridColumn.EMPNAME].Visible = false; dg.Items[i].Cells[(int)DataGridColumn.ROWNUM].RowSpan = rowSpan; dg.Items[j].Cells[(int)DataGridColumn.ROWNUM].Visible = false; } else { break; } } rowNum = rowNum + 1; dg.Items[i].Cells[(int)DataGridColumn.ROWNUM].Text = System.Convert.ToString(rowNum); i = j - 1; } }
3.调用
SpanRow(dgDataGrid);
作者:Adolf Ye 出处:http://www.cnblogs.com/dt520/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
不多试试,你又怎么会甘心