如何将特定的数据集列绑定到datagridview [英] How to bind particular columns of dataset to a datagridview

查看:114
本文介绍了如何将特定的数据集列绑定到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



在我的应用程序中,我从Web服务获取数据到数据集。

此数据集包含12列。



但是在datagridview中显示我只想显示6个特定列。



请告诉我怎么做。



谢谢

Hi all

In my application, i am getting data from a web service to a dataset.
this dataset contain 12 columns.

But while display in datagridview i just want to show only 6 particular columns.

Please tell me how to do this.

Thank you

推荐答案

using (DataTable dt = new DataTable())
                   {
                       sda.Fill(dt);

                       //Set AutoGenerateColumns False
                       dataGridView1.AutoGenerateColumns = false;

                       //Set Columns Count
                       dataGridView1.ColumnCount = 3;

                       //Add Columns
                       dataGridView1.Columns[0].Name = "CustomerId";
                       dataGridView1.Columns[0].HeaderText = "Customer Id";
                       dataGridView1.Columns[0].DataPropertyName = "CustomerID";

                       dataGridView1.Columns[1].HeaderText = "Contact Name";
                       dataGridView1.Columns[1].Name = "Name";
                       dataGridView1.Columns[1].DataPropertyName = "ContactName";

                       dataGridView1.Columns[2].Name = "Country";
                       dataGridView1.Columns[2].HeaderText = "Country";
                       dataGridView1.Columns[2].DataPropertyName = "Country";
                       dataGridView1.DataSource = dt;
                   }
               }







来源 - 检查链接



http://aspsnippets.com/Articles/Show-bind-only-some-specific-certain-columns-of-DataGridView- control-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx [ ^ ]


static DataTable FilterDataTatbleColumns()
      {
          DataTable table = new DataTable("Orders");
          table.Columns.Add("OrderID", typeof(Int32));
          table.Columns.Add("OrderQuantity", typeof(Int32));
          table.Columns.Add("CompanyName", typeof(string));
          table.Columns.Add("Date", typeof(DateTime));

          DataRow newRow = table.NewRow();
          newRow["OrderID"] = 1;
          newRow["OrderQuantity"] = 3;
          newRow["CompanyName"] = "NewCompanyName";
          newRow["Date"] = "1979, 1, 31";

          // Add the row to the rows collection.
          table.Rows.Add(newRow);

          DataRow newRow2 = table.NewRow();
          newRow2["OrderID"] = 2;
          newRow2["OrderQuantity"] = 2;
          newRow2["CompanyName"] = "NewCompanyName1";
          table.Rows.Add(newRow2);

          DataRow newRow3 = table.NewRow();
          newRow3["OrderID"] = 3;
          newRow3["OrderQuantity"] = 2;
          newRow3["CompanyName"] = "NewCompanyName2";
          table.Rows.Add(newRow3);

          //Getting Two Columns("OrderID", "CompanyName") from the table
          DataTable NewTable = table.DefaultView.ToTable(false, "OrderID", "CompanyName");

          return NewTable;
      }


DataSet ds = <-- Get data from Webservice here
DataTable dt = ds.Tables[0];
DataView view = new DataView(dt);
DataTable resultTable = view.ToTable(false, "Column1", "Column2","Column3","Column4","Column5","Column6");
dataGridView1.DataSource = resultTable ;





我假设您将数据输入数据集ds。

数据集实际上包含表格。

您只需从数据集中获取数据表中的数据。





我希望它能工作为你。



I am assuming you are getting data into dataset ds.
Dataset actually contains tables.
you just have take the data in datatable from data set.


I Hope it will work for you.


这篇关于如何将特定的数据集列绑定到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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