Click here to Skip to main content
16,003,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In a program I need to set the Dataset of a DatagridView object at runtime. I used the code below but it throws an Exception. Instead, I used a DatagridView control created at design time and it worked well. I do not know what I missed that I faced the Exception at runtime. Could you please help me?

Thanks a lot


//-------- Setting the Dataset of a dataGridView object
        // ------- At Runtime which throw an Exception

          DataGrid MydataGrid = new DataGrid();
          DataGridView MyDataGridView = new DataGridView();

          DateTime dueDate = new DateTime();

          MydataGrid = myMethod( some inputs );

          MyDataGridView.DataSource = MydataGrid.DataSource;
          int Row_no1 = MyDataGridView.RowCount ; // ->> it returns 0

          dueDate = (DateTime)MyDataGridView.Rows[0].Cells[0].Value; // ->> throw an Exception


        //-------- Setting the Dataset of a dataGridView Control
        // ------- Created at Design time and it works well

          DataGrid MydataGrid2 = new DataGrid();

          DateTime dueDate2 = new DateTime();

          MydataGrid2 = myMethod( same above inputs );

          dataGridView1.DataSource = MydataGrid2.DataSource;
          int Row_no2 = dataGridView1.RowCount ; // ->> it returns 25

          dueDate2 = (DateTime)dataGridView1.Rows[0].Cells[0].Value; // ->> works well
Posted

1 solution

Hi, try to catch the value in item_databound event.

I hope it will help you.
 
Share this answer
 
Comments
ahhashemi 9-Aug-10 9:43am    
Reason for my vote of 3
Thanks for the reply but the Answer looks a little general and doesn't address the problem clearly. more explanation is expected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900