1

我使用带有高级带状网格视图的 devexpress xtragrid。网格将在运行时以编程方式创建。我写了代码,它工作。但是现在我需要使用循环创建网格,我的意思是我不知道列号,这取决于用户。

这是我的第一个代码(它有效):

DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView advBandedGridView1 = new DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView();
        DevExpress.XtraGrid.Views.BandedGrid.GridBand gridBand1 = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
        DevExpress.XtraGrid.Views.BandedGrid.GridBand gridBand2 = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
        DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn colTrademark = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
        DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn colModel = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
        DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn colTrademark2 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
        DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn colModel2 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();       

        advBandedGridView1.Bands.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.GridBand[] { gridBand1, gridBand2 });
        //advBandedGridView1.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] {colTrademark,colModel});       

        advBandedGridView1.GridControl = xgc;
        advBandedGridView1.Name = "advBandedGridView1";
        xgc.MainView = advBandedGridView1;

        gridBand1.Caption = "Model";
        gridBand1.Columns.Add(colTrademark);
        gridBand1.Columns.Add(colModel);
        gridBand1.Name = "gridBand1";
        gridBand1.Width = 217;

        gridBand2.Caption = "Model2";
        gridBand2.Columns.Add(colTrademark2);
        gridBand2.Columns.Add(colModel2);
        gridBand2.Name = "gridBand2";
        gridBand2.Width = 217;

        colTrademark.Caption = "Trademark";
        colTrademark.FieldName = "Trademark0";
        colTrademark.Name = "colTrademark";
        colTrademark.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
        colTrademark.Visible = true;
        colTrademark.Width = 113;

        colModel.Caption = "Model";
        colModel.FieldName = "Model0";
        colModel.Name = "colModel";
        colModel.Visible = true;
        colModel.Width = 104;

        colTrademark2.Caption = "Trademark2";
        colTrademark2.FieldName = "Trademark1";
        colTrademark2.Name = "colTrademark2";
        colTrademark2.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
        colTrademark2.Visible = true;
        colTrademark2.Width = 113;

        colModel2.Caption = "Model2";
        colModel2.FieldName = "Model1";
        colModel2.Name = "colModel2";
        colModel2.Visible = true;
        colModel2.Width = 104;

        DataTable dtCars = new DataTable("Cars");
        dtCars.Columns.Add("Trademark0");
        dtCars.Columns.Add("Model0");
        dtCars.Columns.Add("Trademark1");
        dtCars.Columns.Add("Model1");

        DataRow drCars = dtCars.NewRow();
        drCars["Trademark0"] = "BMW";
        drCars["Model0"] = "530i";
        drCars["Trademark1"] = "aaaa";
        drCars["Model1"] = "111";
        dtCars.Rows.Add(drCars);

        drCars = dtCars.NewRow();
        drCars["Trademark0"] = "Cadillac";
        drCars["Model0"] = "Seville";
        drCars["Trademark1"] = "cccc";
        drCars["Model1"] = "2222";
        dtCars.Rows.Add(drCars);

        drCars = dtCars.NewRow();
        drCars["Trademark0"] = "Cadillac";
        drCars["Model0"] = "Deville";
        drCars["Trademark1"] = "ddddd";
        drCars["Model1"] = "3333";
        dtCars.Rows.Add(drCars);

        xgc.DataSource = dtCars;

和循环一个(它不起作用):

 DevExpress.XtraGrid.Views.BandedGrid.GridBand[] gridBand = new DevExpress.XtraGrid.Views.BandedGrid.GridBand[2];
            for (int i = 0; i < 2; i++)
            {
                gridBand[i] = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();

                DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn colTrademark = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
                DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn colModel = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();

                colTrademark.Caption = "Trademark" + i.ToString();
                colTrademark.FieldName = "Trademark" + i.ToString();
                colTrademark.Name = "colTrademark" + i.ToString();
                colTrademark.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
                colTrademark.Visible = true;
                colTrademark.Width = 113;

                colModel.Caption = "Model" + i.ToString();
                colModel.FieldName = "Model" + i.ToString();
                colModel.Name = "colModel" + i.ToString();
                colModel.Visible = true;
                colModel.Width = 104;

                gridBand[i].Caption = "Model" + i.ToString();
                gridBand[i].Columns.Add(colTrademark);
                gridBand[i].Columns.Add(colModel);
                gridBand[i].Name = "gridBand" + i.ToString();
                gridBand[i].Width = 217;
            }
            advBandedGridView1.Bands.AddRange(gridBand);
            advBandedGridView1.GridControl = xgc;
            advBandedGridView1.Name = "advBandedGridView1";
            xgc.MainView = advBandedGridView1;

DataTable dtCars = new DataTable("Cars");
            dtCars.Columns.Add("Trademark0");
            dtCars.Columns.Add("Model0");
            dtCars.Columns.Add("Trademark1");
            dtCars.Columns.Add("Model1");

            DataRow drCars = dtCars.NewRow();
            drCars["Trademark0"] = "BMW";
            drCars["Model0"] = "530i";
            drCars["Trademark1"] = "aaaa";
            drCars["Model1"] = "111";
            dtCars.Rows.Add(drCars);

            drCars = dtCars.NewRow();
            drCars["Trademark0"] = "Cadillac";
            drCars["Model0"] = "Seville";
            drCars["Trademark1"] = "cccc";
            drCars["Model1"] = "2222";
            dtCars.Rows.Add(drCars);

            drCars = dtCars.NewRow();
            drCars["Trademark0"] = "Cadillac";
            drCars["Model0"] = "Deville";
            drCars["Trademark1"] = "ddddd";
            drCars["Model1"] = "3333";
            dtCars.Rows.Add(drCars);

            xgc.DataSource = dtCars;

我该如何修复第二个?

4

1 回答 1

1

我解决了这个问题。它可能会有所帮助..

DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView advBandedGridView1 = new DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView();
        DevExpress.XtraGrid.Views.BandedGrid.GridBand[] bandArray = new DevExpress.XtraGrid.Views.BandedGrid.GridBand[2];
        DataTable dtAralik = (DataTable)xgcAralik.DataSource;
        for (int i = 0; i < bandArray.Length; i++)
            bandArray[i] = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
        advBandedGridView1.Bands.AddRange(bandArray);

        advBandedGridView1.GridControl = xgc;
        advBandedGridView1.Name = "advBandedGridView1";
        xgc.MainView = advBandedGridView1;
        for (int i = 0; i < bandArray.Length; i++)
        {
            bandArray[i].Caption = "Aralık" + i.ToString();
            bandArray[i].Columns.Add(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn());
            bandArray[i].Columns.Add(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn());
            bandArray[i].Name = "gridBand" + i.ToString();
            bandArray[i].Width = 217;
        }

        for (int i = 0; i < bandArray.Length; i++)
        {
            bandArray[i].Columns[0].Caption = "TradeMark";
            bandArray[i].Columns[0].FieldName = "Trademark" + i.ToString();
            bandArray[i].Columns[0].Name = "colTrademark" + i.ToString();
            bandArray[i].Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            bandArray[i].Columns[0].Visible = true;
            bandArray[i].Columns[0].Width = 113;

            bandArray[i].Columns[1].Caption = "Model";
            bandArray[i].Columns[1].FieldName = "Model" + i.ToString();
            bandArray[i].Columns[1].Name = "colModel" + i.ToString();
            bandArray[i].Columns[1].Visible = true;
            bandArray[i].Columns[1].Width = 113;
        }

        DataTable dtCars = new DataTable("Cars");
        dtCars.Columns.Add("Trademark0");
        dtCars.Columns.Add("Model0");
        dtCars.Columns.Add("Trademark1");
        dtCars.Columns.Add("Model1");

        DataRow drCars = dtCars.NewRow();
        drCars["Trademark0"] = "BMW";
        drCars["Model0"] = "530i";
        drCars["Trademark1"] = "aaaa";
        drCars["Model1"] = "111";
        dtCars.Rows.Add(drCars);

        drCars = dtCars.NewRow();
        drCars["Trademark0"] = "Cadillac";
        drCars["Model0"] = "Seville";
        drCars["Trademark1"] = "cccc";
        drCars["Model1"] = "2222";
        dtCars.Rows.Add(drCars);

        drCars = dtCars.NewRow();
        drCars["Trademark0"] = "Cadillac";
        drCars["Model0"] = "Deville";
        drCars["Trademark1"] = "ddddd";
        drCars["Model1"] = "3333";
        dtCars.Rows.Add(drCars);

        xgc.DataSource = dtCars;
于 2013-01-30T15:59:55.557 回答