5

我想动态添加 Devexpress GridControl。在运行时,我想显示过滤器行。此外,我希望在具有动态创建的 GridControl 的同一表单上拥有一个按钮。单击按钮时,它应该显示网格控件的过滤器对话框弹出窗口。

4

1 回答 1

6

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

  • 创建一个名为 Form1 的窗体。
  • 创建一个名为 button1 的 Button 并将其停靠在窗体的顶部。
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();
        }
    }
}

于 2009-12-10T15:47:40.390 回答