在运行时设置dataGridView对象的数据集 [英] Setting the Dataset of dataGridView object at Runtime

查看:75
本文介绍了在运行时设置dataGridView对象的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在程序中,我需要在运行时设置DatagridView对象的数据集.我使用了下面的代码,但它引发了异常.相反,我使用了在设计时创建的DatagridView控件,它运行良好.我不知道我错过了在运行时遇到异常的原因.你能帮我吗?

非常感谢


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

推荐答案

尝试捕获item_databound事件中的值.

希望对您有帮助.
Hi, try to catch the value in item_databound event.

I hope it will help you.


这篇关于在运行时设置dataGridView对象的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆