开发者

c# datagridview datasource

I am creating a Datagridview programmatically in a dll file. It's datasource is a Datatable, and my code is as such:

DataGridVie开发者_运维百科w dgv = new DataGridView();
DataSet ds = new DataSet();
ds.ReadXml(rdr);
DataTable dt = ds.Tables[0];

dgv.DataSource = dt;

Why then when I check how many columns and rows the datagrid has, it shows as nothing. Although, when I do the same check on the Datatable it shows that there is data. Does the gridview need to be actually on a form for it to be populated?

Thanks.


Apparently it does. I created a new WinForms app, copied your code into the Form_Load event, and added these two lines immediately after your code and got results.

this.Controls.Add(dgv);
MessageBox.Show(dgv.Rows.Count.ToString()); 

If I comment out the first line the count is 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜