将Devexpress GridControl动态添加到C#windows应用程序

时间:2009-12-07 05:56:09

标签: devexpress xtragrid

我想动态添加Devexpress GridControl。在运行时,我想显示Filter Row。另外我想在窗体上有一个按钮,它有动态创建的GridControl,当单击按钮时,它应该显示网格控件的Filter Dialog弹出窗口。

1 个答案:

答案 0 :(得分:6)

提供的样本可以满足您的要求。

  • 创建一个名为Form1的表单。
  • 创建一个名为button1的按钮并将其停靠 表格的顶部。
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();
        }
    }
}