在运行时创建并填充gridcontrol

时间:2016-01-26 21:11:40

标签: devexpress

我在winform中有一个项目,其中包含一个表单中的各种表单,我需要创建弹出窗口以单击一个按钮,弹出窗口并在此处创建一个GridControl并填充先前创建的数据表。当我这样做时,GridControl显示为空,我该怎么办?

这里我留下了代码

        ToolStripDropDown popup = new ToolStripDropDown();
        popup.Margin = Padding.Empty;
        popup.Padding = Padding.Empty;

        Inventario_MediosDataSet.MedioDataTable m = new Inventario_MediosDataSet.MedioDataTable();
        this.medioTableAdapter1.Fill(m);
        DevExpress.XtraGrid.GridControl ki = new DevExpress.XtraGrid.GridControl();
        DevExpress.XtraGrid.Views.Grid.GridView grid = new DevExpress.XtraGrid.Views.Grid.GridView(ki);
        ki.DataSource = m;
        ToolStripControlHost host = new ToolStripControlHost(ki);
        host.Margin = Padding.Empty;
        host.Padding = Padding.Empty;
        popup.Items.Add(host);
        popup.Show(this, simpleButton7.Location);

1 个答案:

答案 0 :(得分:1)

ToolStripControlHost不为Grid提供BindingContext。您也可以在DataGridView中看到相同的行为。要解决此问题,请将GridControl.BindingContext设置为表单BindingContext。

ToolStripDropDown popup = new ToolStripDropDown();
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
Inventario_MediosDataSet.MedioDataTable m = new Inventario_MediosDataSet.MedioDataTable();
this.medioTableAdapter1.Fill(m);
DevExpress.XtraGrid.GridControl ki = new DevExpress.XtraGrid.GridControl();
DevExpress.XtraGrid.Views.Grid.GridView grid = new DevExpress.XtraGrid.Views.Grid.GridView(ki);
ki.MainView = grid;
// --------------------
ki.BindingContext = this.BindingContext;
// --------------------
ki.DataSource = m;
ToolStripControlHost host = new ToolStripControlHost(ki);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(this, simpleButton1.Location);