将datatable datarow插入datagridview [英] insert datatable datarow into datagridview

查看:112
本文介绍了将datatable datarow插入datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据表中的数据行插入到datagridview中。我不想将数据表绑定到datagridview,原因我现在不会厌烦你。我不想迭代每一行和每一行来插入值,因为这需要很长时间。 datatable和datagridview具有相同的列,但列数可以根据用户选择的选项进行更改。



看起来很简单。但我无法让它发挥作用。我在下面发布了一些伪代码。我可以以某种方式将数据行转换为datagridview行吗?



有什么想法吗?



I''m trying to insert data rows from a datatable into a datagridview. I don''t want to bind the datatable to the datagridview for reasons I won''t bore you with right now. I don''t want to iterate through each row and column to insert values because that''s take a long time. The datatable and datagridview have the same columns, but the number of columns can change depeding on options selected by the user.

Seems simple. But I can''t get it to work. I''ve posted some psuedo-code below. Can I somehow cast a datarow to a datagridview row?

Any ideas?

foreach (DataRow dr in datatable.Rows)
{
   datagridview.Rows.Add(dr);
}

推荐答案

你别无选择。你必须做你说过你不想做的事情中的一个或另一个。您无法将数据行转换为datagridviewrow。这是不可能的,因为两者之间没有转换。
You simply have no choice. You have to do one or the other of the things you said you don''t want to do. You cannot cast a datarow to a datagridviewrow. It''s just not possible because there is no conversion between the two.


DataRow [] rows = ds.Table [tablename]。选择(columnA =''+ textbox1.Text +''');

if(rows.Length> 0)dataGridView1.DataSource = rows.CopyToDataTable();
DataRow[] rows = ds.Table["tablename"].Select("columnA=''"+textbox1.Text+"''");
if (rows.Length > 0) dataGridView1.DataSource = rows.CopyToDataTable();


好吧,如果你不想使用数据绑定,也不想手动添加行,除了放弃,让其他人以正确的方式进行操作,你无能为力。
Well if you don''t want to use data binding nor want to add rows manually then there isn''t much you can do except give up and let someone else do it the correct way.


这篇关于将datatable datarow插入datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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