如何在C#中将ListView与数据表绑定 [英] How can I bind the ListView with the datatable in C#

查看:117
本文介绍了如何在C#中将ListView与数据表绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将ListView Dataset 绑定.
我已经为此编写了代码.
但是,当我运行程序时,它确实为我提供了正确的输出.
ListView 显示的是一列的值,而我在DataTable .....
中输入了4列和5行 请给我解决方案...
我的代码如下....
谢谢你

Hi,

I want to bind the ListView with the Dataset .
I have make the code for that.
But when I run my program it does gives me the proper output.
the ListView shows the values of one one column, whereas I have 4 columns and 5 rows entered in DataTable....
Pls give me the solution...
My code is given below....
Thank your

private void button1_Click(object sender, EventArgs e)
     {
         DataTable dt = clsobj.GetTable();
         ListView listview1 = new ListView();
         string[] str = new string[dt.Columns.Count];
         listview1.Top = 100;
         listview1.Left = 50;
         listview1.Size = new System.Drawing.Size(292, 273);
         listview1.GridLines = true;
         listview1.Scrollable = true;

         int fc = dt.Columns.Count;

         listview1.Columns.Clear();
         listview1.Items.Clear();

         for (int i = 0; i < fc; i++)
         {
             listview1.Columns.Add("Column " + i.ToString());
         }

         foreach (DataRow row in dt.Rows)
         {
             string[] subitems = new string[fc];

             object[] o = row.ItemArray;
             for (int i = 0; i < fc; i++)
             {
                 subitems[i] = o[i].ToString();
             }
             ListViewItem item = new ListViewItem(subitems);
             listview1.Items.Add(item);
         }

         listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
         this.Controls.Add(listview1);
     }

推荐答案

有一个代码项目文章^ ].

应该会有所帮助.
There is a Code Project article here[^].

that should help.


这篇关于如何在C#中将ListView与数据表绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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