//int[] array = { 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 0, 2, 2 };
//int current = array[0];
//int m = 0;
//for (int n = 0; n < array.Length; n++)
//{
// // Console.WriteLine("Array:{0}",array[n]);
// if (array[n] == current)
// {
// m++;
// }
// else
// {
// Console.WriteLine("current:{0},count:{1}", current, m);
// current = array[n];
// m = 1;
// }
//}
//Console.WriteLine("current:{0},count:{1}", current, m);
//var result = from a in array
// group a by a into g
// select new { g.Key, Num = g.Count() };
//result.ToList().ForEach(p => Console.WriteLine("元素:{0}有{1}个", p.Key, p.Num));
string SQL = "SELECT TS.[SID], TS.SNAME, te.Sex_Descpriton, TS.Age, TG.NGrade, TC.CNAME FROM Tb_Cource tc, Tb_Grade tg, Tb_Studens ts, tb_sex te WHERE TG.[SID] = TS.[SID] AND TC.CID = TG.CID AND te.sex_id = ts.SEX ";
DataTable dt_Students = SingleTon<SqlDataAccess>.Instance.getTable(SQL);
SetGrid(dt_Students);
CreateGridHeader();
int rowsCount=2;
string current_columns01 = dt_Students.Rows[0][0].ToString(); //记录第一行第一列数据
int m01 = 0;
foreach (DataRow row in dt_Students.Rows)
{
for (int column = 0; column < dt_Students.Columns.Count + 1; column++)
{
if (column == 0) grid1[rowsCount, column] = new SourceGrid.Cells.RowHeader(rowsCount);
//行数
#region 合并第一列相同的数据
else if (column == 1)
{
if (row[0].ToString() == current_columns01)
{
m01++;
}
else
{
//新建单元格,合并
grid1[rowsCount - m01, column] = new SourceGrid.Cells.Cell(current_columns01);
grid1[rowsCount - m01, column].RowSpan = m01;
current_columns01 = row[column-1].ToString();
m01 = 1;
}
}
#endregion
else
{
grid1[rowsCount, column] = new SourceGrid.Cells.Cell(row[column - 1]);
}
}
rowsCount++;
}
//合并最后一条相同的数据(因为是写在ELSE里)
grid1[rowsCount - m01, 1] = new SourceGrid.Cells.Cell(current_columns01);
grid1[rowsCount - m01, 1].RowSpan = m01;
}
void CreateGridHeader()
{
grid1[0, 0] = new SourceGrid.Cells.ColumnHeader("序号");
grid1[0, 0].RowSpan = 2;
string[] Header00 = new string[] { "个人资料", "成绩" };
string[] Header01 = new string[] { "SID","姓名","性别","年龄","分数","课程"};
grid1[0, 1] = new SourceGrid.Cells.ColumnHeader(Header00[0]);
grid1[0, 1].ColumnSpan = 4;
grid1[0, 5] = new SourceGrid.Cells.ColumnHeader(Header00[1]);
grid1[0, 5].ColumnSpan =2;
(grid1[0, 5] as SourceGrid.Cells.ColumnHeader).AutomaticSortEnabled = (grid1[0, 1] as SourceGrid.Cells.ColumnHeader).AutomaticSortEnabled = false;
for (int i = 1; i <= Header01.Length; i++)
{
grid1[1, i] = new SourceGrid.Cells.ColumnHeader(Header01[i-1]);
(grid1[1, i] as SourceGrid.Cells.ColumnHeader).AutomaticSortEnabled = false;
}
//SourceGrid.Cells.Header l_00Header = new SourceGrid.Cells.Header(null);
//SourceGrid.Cells.Header l_01Header = new SourceGrid.Cells.Header(null);
//grid1[0, 0] = l_00Header;
//grid1[1, 0] = l_01Header;
//int i=1;
//foreach (DataColumn column in dt.Columns)
//{
// grid1[0, i] = new SourceGrid.Cells.ColumnHeader(column.ColumnName);
// i++;
//}
}
sourcegrid合并单元格查询绑定数据
最新推荐文章于 2020-08-20 15:53:50 发布