Dynamically adding Devexpress GridControl to C# windows application

南楼画角 提交于 2020-12-08 17:22:43

问题


I want to add Devexpress GridControl dynamically. At runtime I want to show the Filter Row. Also I want to have a button on the same form that has the dynamically created GridControl. When the button is clicked it should show the Filter Dialog popup for the grid control.


回答1:


The provided sample does what you ask for.

  • Create a Form called Form1.
  • Create a Button called button1 and Dock it to the top of the form.
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

namespace Samples
{
    public partial class Form1 : Form
    {
        private GridControl grid;
        private GridView view;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            view.ShowFilterPopup(view.Columns[0]);                      
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            grid = new GridControl();
            view = new GridView();

            grid.Dock = DockStyle.Fill;
            grid.ViewCollection.Add(view);
            grid.MainView = view;

            view.GridControl = grid;
            view.OptionsView.ShowAutoFilterRow = true;
            GridColumn column = view.Columns.Add();
            column.Caption = "Name";
            column.FieldName = "Name";
            column.Visible = true;

            // The grid control requires at least one row 
            // otherwise the FilterPopup dialog will not show
            DataTable table = new DataTable();
            table.Columns.Add("Name");
            table.Rows.Add("Hello");
            table.Rows.Add("World");
            grid.DataSource = table;

            this.Controls.Add(grid);
            grid.BringToFront();
        }
    }
}



来源:https://stackoverflow.com/questions/1858077/dynamically-adding-devexpress-gridcontrol-to-c-sharp-windows-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!