none
Loading / Binding Data to DataGridView in Form Load Event RRS feed

  • Question

  • Hi,
    I am loading / binding data to DataGridView in Form Load event, but issue which i am facing is untlil data is not loaded / binded to DataGridView, my form is not visible :(.
    But as per my requirement, Form should be visible first and then data should be loded / binded to DataGridView.

    Code Snippet :

    private void myForm_Load(object sender, EventArgs e) {
        this.Cursor = Cursors.WaitCursor;
        FillMyDataGridView();
        this.Cursor = Cursors.Default;
    }

    FillMyDataGridView() : this method bind the data to myDataGridView, but this method takes arround 10 secs to compute and bind data to myDataGridView, till than Form is not visible :(.

    I tried to do the same in Shown Event, Activated Event but the result is same.
    I tried SuspendLayout() and ResumeLayout() still result is same.

    Is there any solution for this kind of issues ?

    Thanks and Best Reragds,
    Nikhil Sharma

    Thursday, October 30, 2008 9:47 AM

Answers

  • Hi,

    you could use asynchronous method calls to let the main form load without waiting the FillMyDataGridView method to execute. So, in other words, call the FillMyDataGridView method asynchronously.

    More information about this can be found from following web page (http://msdn.microsoft.com/en-us/magazine/cc301332.aspx).

    BR,
    Tomi
    • Marked as answer by Harry Zhu Monday, November 3, 2008 8:38 AM
    Thursday, October 30, 2008 10:09 AM
  • Hi Nikhil,

    You need call the this.Cursor=Cursors.Dafault; at UI thread.
    Try the following code,it works well.
      private delegate void MyDelegate();  
            DataTable table = new DataTable("MyDataTable");        
             
            public Form1()  
            {  
                InitializeComponent();  
            }  
     
                   private void FillMyDataGridView()  
            {                   
                    DataColumn column;  
                    DataRow row;                                         
                    column = new DataColumn();                
                    column.ColumnName = "id";                 
                    table.Columns.Add(column);  
                    for (int i = 0; i <= 50000; i++)  
                    {  
                        row = table.NewRow();  
                        row["id"] = i;                  
                        table.Rows.Add(row);  
                    }                         
                           
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                this.Cursor = Cursors.WaitCursor;  
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(new MyDelegate(FillMyDataGridView)));  
                t.Start();  
                t.Join();  
                this.dataGridView1.DataSource = table;  
                this.Cursor = Cursors.Default;  
     
            }  
             

    Best regards,
    Harry
    • Marked as answer by Harry Zhu Monday, November 3, 2008 8:39 AM
    Monday, November 3, 2008 8:34 AM

All replies

  • Hi,

    you could use asynchronous method calls to let the main form load without waiting the FillMyDataGridView method to execute. So, in other words, call the FillMyDataGridView method asynchronously.

    More information about this can be found from following web page (http://msdn.microsoft.com/en-us/magazine/cc301332.aspx).

    BR,
    Tomi
    • Marked as answer by Harry Zhu Monday, November 3, 2008 8:38 AM
    Thursday, October 30, 2008 10:09 AM
  • Hi Tomi,
    I used Async call as follow :

    Code Snippet :

    private delegate void MyDelegate();

    private void MyForm_Load(object sender, EventArgs e)
    {
        MyDelegate myDelegate = FillMyDataGridView;
        myDelegate.BeginInvoke(null, null);
    }

    private void FillMyDataGridView()
    {
        this.Cursor = Cursors.WaitCursor;
        
    DataTable table = new DataTable("MyDataTable");
        try
        {
            // Filling Data into table
        }
        catch (Exception ex)
        {
        }
        finally
        {
            this.Cursor = Cursors.Default;
        }

        this.myDataGridView.DataSource = table;
    }

    But here i am facing issues :
    1) Untill data appears on DataGridView all the buttons are enabled (it seems that "this.Cursor = Cursors.WaitCursor;" doesn't work here)
    2) Form (as well as application) hangs after data gets filled into DataGridView.

    is there any other solution available or am i missing something in this code.

    Thanks and Best Regards,
    Nikhil Sharma

    Thursday, October 30, 2008 12:32 PM
  • Hi Nikhil,

    You need call the this.Cursor=Cursors.Dafault; at UI thread.
    Try the following code,it works well.
      private delegate void MyDelegate();  
            DataTable table = new DataTable("MyDataTable");        
             
            public Form1()  
            {  
                InitializeComponent();  
            }  
     
                   private void FillMyDataGridView()  
            {                   
                    DataColumn column;  
                    DataRow row;                                         
                    column = new DataColumn();                
                    column.ColumnName = "id";                 
                    table.Columns.Add(column);  
                    for (int i = 0; i <= 50000; i++)  
                    {  
                        row = table.NewRow();  
                        row["id"] = i;                  
                        table.Rows.Add(row);  
                    }                         
                           
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                this.Cursor = Cursors.WaitCursor;  
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(new MyDelegate(FillMyDataGridView)));  
                t.Start();  
                t.Join();  
                this.dataGridView1.DataSource = table;  
                this.Cursor = Cursors.Default;  
     
            }  
             

    Best regards,
    Harry
    • Marked as answer by Harry Zhu Monday, November 3, 2008 8:39 AM
    Monday, November 3, 2008 8:34 AM