[原创]XtraGrid自定义多表头

 

[原创]XtraGrid自定义多表头_第1张图片

设计时的图

[原创]XtraGrid自定义多表头_第2张图片

运行效果图

 中间的尺码数量,个数不固定,数据库中表结构为:

ID,颜色ID,尺码ID,插色一ID,插色二ID,数量,仓库ID

Code
        private DataTable ShowInfo(int DepartmentID, int MaterielID)
        
{
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
"Color"typeof(string));

            dt.Rows.Add(dt.NewRow());
            dt.Rows[
0]["Color"= "颜色\\尺码";
            SqlDataReader myReader 
= bllRep.GetSize(MaterielID, DepartmentID);

            
int i = 1;
           
            ColorList.Clear();
            ColorOneList.Clear();
            ColorTwoList.Clear();
            SizeList.Clear();
            ColorList.Add(
0);
            SizeList.Add(
0);
            ColorOneList.Add(
0);
            ColorTwoList.Add(
0);
            advBandedGridView2.Bands.Clear();
            advBandedGridView2.Columns.Clear();
            advBandedGridView2.OptionsView.ShowColumnHeaders 
= false;

            
while (myReader.Read())
            
{
                
int m = 0;
                
if (i == 1)
                    m 
= 1;
                
else
                    m 
= i * 2 - 1;
                dt.Columns.Add(
"Columns" + i);
                dt.Columns.Add(
"Columnss" + i);
                dt.Rows[
0][m] = myReader[0].ToString();
                SizeList.Add(
int.Parse(myReader[1].ToString()));
                SizeNameList.Add(myReader[
0].ToString());
                i
++;
            }

            myReader.Close();

            dt.Columns.Add(
"SumNum"typeof(string));
            dt.Rows[
0]["SumNum"= "库存";
            dt.Columns.Add(
"GoSum"typeof(string));
            dt.Rows[
0]["GoSum"= "出库";
            dt.Columns.Add(
"ColorOne"typeof(string));
            dt.Columns.Add(
"ColorTwo"typeof(string));
            dt.Rows[
0]["ColorOne"= "插色一";
            dt.Rows[
0]["ColorTwo"= "插色二";
            
for (int j = 0; j < dt.Columns.Count - 4; j++)
            
{
                advBandedGridView2.Bands.Add();
                advBandedGridView2.Columns.Add();
                advBandedGridView2.Columns[j].FieldName 
= dt.Columns[j].ColumnName;
                advBandedGridView2.Bands[j].Columns.Add((advBandedGridView2.Columns[j]) 
as BandedGridColumn);
                
if (j > 0)
                
{
                    
if ((j % 2== 0)
                    
{
                        advBandedGridView2.Bands[j].Caption 
= "发货";
                    }

                    
else
                    
{
                        advBandedGridView2.Bands[j].Caption 
= "库存";
                        advBandedGridView2.Columns[j].OptionsColumn.AllowEdit 
= false;
                    }

                }

                
else
                
{
                    advBandedGridView2.Bands[j].Caption 
= "颜色";
                    advBandedGridView2.Columns[j].OptionsColumn.AllowEdit 
= false;
                }


                advBandedGridView2.Bands[j].AppearanceHeader.TextOptions.HAlignment 
= DevExpress.Utils.HorzAlignment.Center;
                advBandedGridView2.Columns[j].Visible 
= true;
                advBandedGridView2.Columns[j].Width 
= 60;
            }

            
int bCount = advBandedGridView2.Bands.Count;
            
for (int j = 0; j < SizeNameList.Count; j++)
            
{
                advBandedGridView2.Bands.Add();
                advBandedGridView2.Bands[bCount 
+ j].Caption = SizeNameList[j].ToString();

                advBandedGridView2.Bands[bCount 
+ j].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }

            
for (int j = 0; j < SizeNameList.Count; j++)
            
{
                advBandedGridView2.Bands[bCount 
- j].Children.AddRange(new GridBand[] { advBandedGridView2.Bands[1], advBandedGridView2.Bands[2] });
            }

            bCount 
= advBandedGridView2.Bands.Count;
            i 
= 0;
            
for (int j = dt.Columns.Count - 4; j < dt.Columns.Count; j++)
            
{
                advBandedGridView2.Bands.Add();
                advBandedGridView2.Columns.Add();
                advBandedGridView2.Columns[j].FieldName 
= dt.Columns[j].ColumnName;
                advBandedGridView2.Bands[bCount 
+ i].Caption = dt.DefaultView[0][j].ToString();
                advBandedGridView2.Bands[bCount 
+ i].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                advBandedGridView2.Bands[bCount 
+ i].Columns.Add((advBandedGridView2.Columns[j]) as BandedGridColumn);
                advBandedGridView2.Columns[j].Visible 
= true;
                advBandedGridView2.Columns[j].Width 
= 60;
                advBandedGridView2.Columns[j].OptionsColumn.AllowEdit 
= false;
                i
++;
            }

            advBandedGridView2.Bands.Add();
            advBandedGridView2.Bands[advBandedGridView2.Bands.Count 
- 1].Caption = "合计";
            advBandedGridView2.Bands[advBandedGridView2.Bands.Count 
- 1].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            advBandedGridView2.Bands[advBandedGridView2.Bands.Count 
- 1].Children.AddRange(new GridBand[] { advBandedGridView2.Bands[bCount], advBandedGridView2.Bands[bCount + 1] });
            SqlDataReader ColorReader 
= bllRep.GetColor(MaterielID, DepartmentID);
            i 
= 1;
            
while (ColorReader.Read())
            
{
                dt.Rows.Add(dt.NewRow());
                dt.Rows[i][
0= ColorReader[0].ToString();
                dt.Rows[i][dt.Columns.Count 
- 4= ColorReader[2].ToString();
                dt.Rows[i][dt.Columns.Count 
- 2= ColorReader[3].ToString();
                dt.Rows[i][dt.Columns.Count 
- 1= ColorReader[5].ToString();
                ColorList.Add(
int.Parse(ColorReader[1].ToString()));
                ColorOneList.Add(
int.Parse(ColorReader[4].ToString()));
                ColorTwoList.Add(
int.Parse(ColorReader[6].ToString()));
                i
++;
            }

            ColorReader.Close();
            
for (int r = 1; r < SizeList.Count; r++)
            
{
                
for (int c = 1; c < ColorList.Count; c++)
                
{
                    
int m = 0;
                    
if (r == 1)
                        m 
= 1;
                    
else
                        m 
= r * 2 - 1;
                    modRep.ColorID 
= int.Parse(ColorList[c].ToString());

                    modRep.SizeID 
= int.Parse(SizeList[r].ToString());
                    modRep.ColorOneID 
= int.Parse(ColorOneList[c].ToString());
                    modRep.ColorTwoID 
= int.Parse(ColorTwoList[c].ToString());
                    modRep.MaterielID 
= MaterielID;
                    modRep.DepartmentID 
= DepartmentID;
                    dt.Rows[c][m] 
= bllRep.GetValue(modRep);
                    
if (dtInfo.Rows.Count > 0)
                    
{
                        
for (int x = 0; x < dtInfo.Rows.Count; x++)
                        
{
                            
if (dtInfo.DefaultView[x]["ColorID"].ToString() == ColorList[c].ToString() && dtInfo.DefaultView[x]["SizeID"].ToString() == SizeList[r].ToString())
                            
{
                                dt.Rows[c][m 
+ 1= dtInfo.DefaultView[x]["Amount"];
                            }

                        }

                    }


                }

            }

            dt.Rows.RemoveAt(
0);
            
return dt;
        }

从生产管理转行写代码不久,第一次发这样的贴子,欢迎拍砖

你可能感兴趣的:([原创]XtraGrid自定义多表头)