XtraGrid

 这个Demo展示如何以代码的方式设计界面。

代码

  RepositoryItemImageComboBox riImageComBo1 = null;
        RepositoryItemImageEdit riImageEdit1 = null;
        RepositoryItemMemoExEdit riMemoEdit1 = null;
        RepositoryItemComboBox riComboBox1 = null;
        RepositoryItemComboBox riComboBox2 = null;
        RepositoryItemCalcEdit riCalEdit1 = null;
        RepositoryItemSpinEdit riSpinEdit1 = null;

        private void InitGrid()
        {
            CreateEdit();
            gridControl1.UseEmbeddedNavigator = true;
            gridControl1.ShowOnlyPredefinedDetails = true;
            gridControl1.MainView.Dispose();
            //新建一个高级带区网格视图
            AdvBandedGridView advBandedGridView1 = new AdvBandedGridView(this.gridControl1);
            //列宽适应
            advBandedGridView1.OptionsView.ColumnAutoWidth = true;
            //不现实分组面板
            advBandedGridView1.OptionsView.ShowGroupPanel = false; 
            advBandedGridView1.Images = imageList3;
            this.gridControl1.MainView = advBandedGridView1;
            

            //新建带区
            GridBand band1 = new GridBand();
            band1.Caption = "供应商";
            GridBand band2 = new GridBand();
            band2.Caption = "地址信息";

            #region 要在MainView中显示的列
            BandedGridColumn colCompanyName = new BandedGridColumn();
            colCompanyName.Caption = "公司名称";
            colCompanyName.FieldName = "CompanyName";
            colCompanyName.Visible = true;
            colCompanyName.VisibleIndex = 0;
            colCompanyName.RowIndex = 0;
            colCompanyName.ColVIndex = 0;
            //设置列在带区中的位置 也可以用advBandedGridView1.SetColumnPosition
            colCompanyName.RowIndex = 0;
            BandedGridColumn colSupplierID = new BandedGridColumn();
            colSupplierID.Caption = "供应商ID";
            colSupplierID.FieldName = "SupplierID";
            colSupplierID.Visible = true;
            colSupplierID.VisibleIndex = 1;
            colSupplierID.RowIndex = 1;
            colSupplierID.ColVIndex = 0;

            BandedGridColumn colContactName = new BandedGridColumn();
            colContactName.Caption = "负责人";
            colContactName.FieldName = "ContactName";
            colContactName.ImageIndex = 1;
            colContactName.Visible = true;
            colContactName.VisibleIndex = 2;
            colContactName.RowIndex = 1;
            colCompanyName.ColVIndex = 1;

            BandedGridColumn colRegion = new BandedGridColumn();
            colRegion.Caption = "地区";
            colRegion.FieldName = "Region";
            colRegion.Visible = true;
            colRegion.ColumnEdit = riComboBox1;
            BandedGridColumn colContry = new BandedGridColumn();
            colContry.Caption = "国家";
            colContry.FieldName = "Country";
            colContry.Visible = true;
            colContry.ColumnEdit = riComboBox2;
            BandedGridColumn colAddress = new BandedGridColumn();
            colAddress.Caption = "地址";
            colAddress.FieldName = "Address";
            colAddress.VisibleIndex = 5;
            colAddress.Visible = true;
            colAddress.RowIndex = 1;
            BandedGridColumn colCity = new BandedGridColumn();
            colCity.Caption = "城市";
            colCity.FieldName = "City";
            colCity.Visible = true;
            colCity.VisibleIndex = 6;
            colCity.RowIndex = 1;
            BandedGridColumn colPhone = new BandedGridColumn();
            colPhone.Caption = "电话";
            colPhone.FieldName = "Phone";
            colPhone.ImageIndex = 0;
            colPhone.VisibleIndex = 7;
            colPhone.Visible = true;
            colPhone.RowIndex = 1;


            band1.Columns.Add(colCompanyName);
            band1.Columns.Add(colSupplierID);
            band1.Columns.Add(colContactName);
            band2.Columns.Add(colRegion);
            band2.Columns.Add(colContry);
            band2.Columns.Add(colAddress);
            band2.Columns.Add(colPhone);

            advBandedGridView1.Bands.AddRange(new GridBand[] { band1, band2 });

            advBandedGridView1.Columns.AddRange(new BandedGridColumn[]{
               colSupplierID,colCompanyName,colContactName,colRegion,colContry,colAddress,colCity,colPhone
            });
            #endregion

            GridView gridView1 = new GridView(this.gridControl1);
            gridView1.Images = imageList3;
            gridView1.ViewCaption = "产品";

            GridView gridView2 = new GridView(this.gridControl1);
            gridView2.Images=imageList3;

            #region gridView2中的列
            GridColumn colOrderID = new GridColumn();
            colOrderID.Caption = "OrderID";
            colOrderID.FieldName = "OrderID";
            colOrderID.Name = "colOrderID";
            colOrderID.OptionsFilter.AllowFilter = false;
            colOrderID.VisibleIndex = 0;

            GridColumn colQuantity = new GridColumn();
            colQuantity.Caption = "Quantity";
            colQuantity.ColumnEdit = this.riCalEdit1;
            colQuantity.FieldName = "Quantity";
            colQuantity.Name = "colQuantity";
            colQuantity.VisibleIndex = 1;

            GridColumn colUnitPrice = new GridColumn();
            colUnitPrice.Caption = "Unit Price";
            colUnitPrice.ColumnEdit = riCalEdit1;
            colUnitPrice.DisplayFormat.FormatString = "c";
            colUnitPrice.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colUnitPrice.FieldName = "UnitPrice";
            colUnitPrice.ImageIndex = 2;

            GridColumn colSubTotal = new GridColumn();
            colSubTotal.Caption = "Sub Total";
            colSubTotal.DisplayFormat.FormatString = "c";
            colSubTotal.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colSubTotal.FieldName = "SubTotal";
            colSubTotal.ImageIndex = 3;
            colSubTotal.Name = "colSubTotal";
            colSubTotal.OptionsColumn.AllowEdit = false;
            colSubTotal.VisibleIndex = 3;

            gridView2.Columns.Add(colOrderID);
            gridView2.Columns.Add(colQuantity);
            gridView2.Columns.Add(colUnitPrice);
            gridView2.Columns.Add(colSubTotal);
            #endregion
            #region 添加分组
            gridView2.GroupCount = 1;
            gridView2.GroupSummary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Sum, "SubTotal", null, "(Sub Total = {0:c})")});

            gridView2.SortInfo.AddRange(new DevExpress.XtraGrid.Columns.GridColumnSortInfo[] {
            new DevExpress.XtraGrid.Columns.GridColumnSortInfo(colUnitPrice, DevExpress.Data.ColumnSortOrder.Ascending)});
            #endregion


            CardView cardView1 = new CardView();
            cardView1.GridControl = gridControl1;
            cardView1.CardCaptionFormat = "Product Name: {4}";
            #region 在卡片视图中添加列
            GridColumn colCategoryName = new GridColumn();
            colCategoryName.Caption = "分类";
            colCategoryName.FieldName = "CategoryName";
            colCategoryName.Visible = true;
            colCategoryName.VisibleIndex = 0;

            GridColumn colPicture = new GridColumn();
            colPicture.Caption = "图片";
            colPicture.FieldName = "Picture";
            colPicture.Visible = true;
            colPicture.ColumnEdit = riImageEdit1;
            colPicture.VisibleIndex = 1;

            GridColumn colDescription = new GridColumn();
            colDescription.Caption = "备注";
            colDescription.FieldName = "Description";
            colDescription.ColumnEdit = riMemoEdit1;
            colDescription.Visible = true;
            colDescription.VisibleIndex = 2;

            GridColumn colProductName1 = new GridColumn();
            colProductName1.Caption = "产品名称";
            colProductName1.FieldName = "ProductName";
            colProductName1.Visible = true;
            colDescription.VisibleIndex = 3;

            cardView1.Columns.AddRange(new GridColumn[] {
            colCategoryName,
            colPicture,
            colDescription,
            colProductName1});
            #endregion

            GridLevelNode levelNode1 = new GridLevelNode();
            levelNode1.RelationName = "Suppliers_Products";
            levelNode1.LevelTemplate = gridView1;


            GridLevelNode levelNode2 = new GridLevelNode();
            levelNode2.LevelTemplate = gridView2;
            levelNode2.RelationName = "ProductsOrder_Details";
            levelNode1.Nodes.Add(levelNode2);

            GridLevelNode levelNode3 = new GridLevelNode();
            levelNode3.LevelTemplate = cardView1;
            levelNode3.RelationName = "Suppliers_CategoryProducts";

            gridControl1.ViewCollection.Clear();
            gridControl1.ViewCollection.AddRange(new BaseView[]{
               advBandedGridView1,
               gridView1,
               gridView2,
               cardView1
            });

            this.gridControl1.LevelTree.Nodes.AddRange(new GridLevelNode[]{
              levelNode1,levelNode3
            });



            //在运行时刻给特定单元格添加编辑器
            //接管 GridView.CustomRowCellEdit (或 LayoutView.CustomRowCellEdit) 
            //事件把编辑器指派到个别单元格
            gridView1.CustomRowCellEdit += new CustomRowCellEditEventHandler(gridView1_CustomRowCellEdit);
        }

        private void InitData()
        {
            string DBFileName = string.Empty;
            string ConnectionString = string.Empty;
            DBFileName = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, "Data\\nwind.mdb");
            if (DBFileName != string.Empty)
            {
                ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBFileName;

                OleDbDataAdapter oleDBAdapter1 = new OleDbDataAdapter("SELECT * FROM Suppliers", ConnectionString);
                OleDbDataAdapter oleDBAdapter2 = new OleDbDataAdapter("SELECT * FROM Products", ConnectionString);
                OleDbDataAdapter oleDBAdapter3 = new OleDbDataAdapter("SELECT * FROM [Order Details]", ConnectionString);
                OleDbDataAdapter oleDBAdapter4 = new OleDbDataAdapter("SELECT * FROM CategoryProducts", ConnectionString);

                oleDBAdapter1.Fill(dataSet11.Suppliers);

                oleDBAdapter2.Fill(dataSet11.Products);

                oleDBAdapter3.Fill(dataSet11.Order_Details);

                oleDBAdapter4.Fill(dataSet11.CategoryProducts);

                gridControl1.DataSource = dataSet11.Suppliers;
            }
        }

        //添加编辑器
        private void CreateEdit()
        {
            riComboBox1 = new RepositoryItemComboBox();
            riComboBox1.Items.AddRange(new object[] {
            "", "AK", "BC", "CA", "Co. Cork","DF", "ID","Isle of Wight","Lara", "MT", "NM", "Nueva Esparta",
            "OR", "Quebec","RJ", "SP", "Tachira","WA","WY"});

            riComboBox2 = new RepositoryItemComboBox();
            riComboBox2.Items.AddRange(new object[] { "Argentina", "Austria", "Belgium", "Brazil", "Canada", "Denmark", "Finland", "France", "Germany", "Ireland",
            "Italy",  "Mexico", "Norway",  "Poland",   "Portugal",  "Spain", "Sweden",  "Switzerland", "Venezuela"});

            riImageComBo1 = new RepositoryItemImageComboBox();
            riImageComBo1.SmallImages = imageList2;
            riImageComBo1.Items.AddRange(new DevExpress.XtraEditors.Controls.ImageComboBoxItem[] {
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Beverages", 1, 0),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Condiments", 2, 1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Confections", 3, 3),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Dairy Products", 4, 4),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Grains/Cereals", 5, 4),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Meat/Poultry", 6, 5),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Produce", 7, 6),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("Seafood", 8, 7)});

            riCalEdit1 = new RepositoryItemCalcEdit();
            riSpinEdit1 = new RepositoryItemSpinEdit();
            riImageEdit1 = new RepositoryItemImageEdit();
            riMemoEdit1 = new RepositoryItemMemoExEdit();

            this.gridControl1.RepositoryItems.AddRange(new RepositoryItem[] { riComboBox1, riComboBox2, riImageComBo1, riCalEdit1, riSpinEdit1, riImageEdit1, riMemoEdit1 });
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitGrid();
            InitData();
        }

        private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
        {
            //if (e.Column.FieldName == "CategoryID")
            //{
            //    e.RepositoryItem = riImageComBo1;
            //}
            GridView gv = sender as GridView;
            gv.Columns["CategoryID"].ColumnEdit = riImageComBo1;
            gv.Columns["UnitPrice"].DisplayFormat.FormatString = "C";
            gv.Columns["UnitPrice"].DisplayFormat.FormatType = FormatType.Numeric;
            //给特定单元格绑定
            //GridView gv = sender as GridView;
            //string fieldName = gv.GetRowCellValue(e.RowHandle, gv.Columns["CategoryID"]).ToString();
            //switch (fieldName)
            //{
            //    default:
            //        {
            //            e.RepositoryItem = riImageComBo1;
            //            break;
            //        }
            //}           
        }
Demo显示如下图。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值