对于dev express中的gridview控件,IsHandleCreated Prop。始终为false

时间:2014-07-31 09:27:02

标签: devexpress devexpress-windows-ui

我的Windows窗体在​DevExpress.XtraGrid.GridControl上包含Form1,类似的方式还有第二个类称为Form2。在Form1上我正在从数据库加载数据。当我双击网格行时,它会分配给Form2。 on Form1 gridControl1_DoubleClick事件IsHandleCreated prop为true(Form2继承自Form1

    void gridControl1_DoubleClick(object sender, EventArgs e)
    {
        if (gridControl1.IsHandleCreated)
        {
        }
        Form2 obj = new Form2();
        obj.Display();
    }

所以我在Form1上创建了一个属性

    public GridControl GridControl1
    {
        get { return gridControl1; }

    }

但是当我调用Form2的Display()方法并检查Form2上的IsHandleCreated道具是否为假。

public void Display()
    {
        if (handleCreated)
        {

        }           
    }

完整代码如下**Form1**

public partial class Form1 : Form
    {

        public GridControl GridControl1
        {
            get { return gridControl1; }

        }

        public bool handleCreated
        {
            get { return gridControl1.IsHandleCreated; }
        }

        public Form1()
        {
            InitializeComponent();
            gridControl1.DataSource = CreateTable(20);
            gridControl1.DoubleClick += gridControl1_DoubleClick;

        }

        void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridControl1.IsHandleCreated)
            {
            }
            Form2 obj = new Form2();
            obj.Display();
        }


        private DataTable CreateTable(int rowCount)
        {
            DataTable table = new DataTable();
            table.Columns.Add("String", typeof(string));
            table.Columns.Add("Int", typeof(int));
            table.Columns.Add("Date", typeof(DateTime));
            for (var i = 0; i < rowCount; i++)
            {
                table.Rows.Add(string.Format("Row {0}", i), i, DateTime.Today.AddDays(i));
            }
            return table;
        }
}

**Form2**
public class Form2 : Form1
    {
        public Form2()
        {
        }
        public void Display()
        {
            if (handleCreated)
            {

            }
            //Form1 obj = new Form1();

            //if (obj.handleCreated)
            //{
            //}
        }
    }

在Form2 handleCreated中它始终是假的我不知道为什么? 请帮帮我

1 个答案:

答案 0 :(得分:1)

这是因为您的form2对象仅被初始化。只有在创建窗口后,控件才会获得它的控制权,从而显示该控件。因此,您需要致电form2.Show()form2.ShowDialog(),然后检查gridControl1.IsHandleCreated 您可以使用以下代码简单地测试此行为:

Form2 obj = new Form2();
MessageBox.Show("Created: " + obj.handleCreated);

obj.Show();
MessageBox.Show("Shown: " + obj.handleCreated);