Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted

C#
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;
                   }
               }



Source - Check this link

http://aspsnippets.com/Articles/Show-bind-only-some-specific-certain-columns-of-DataGridView-control-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx[^]
 
Share this answer
 
v2
Comments
Black_Rose 9-Sep-14 6:10am    
Please read the question..i asked for dataset not datatable.
Prasad Avunoori 9-Sep-14 6:15am    
Hello Black Rose. We don't have columns in Dataset. Datatable of Dataset only can have columns. How would you expect columns in Dataset????????????
Gihan Liyanage 9-Sep-14 6:20am    
Better if you can go through this link and , better to have idea about DS and DT's

http://www.c-sharpcorner.com/Blogs/12509/difference-between-datareader-dataset-dataadapter-and-data.aspx[^]
Sinisa Hajnal 9-Sep-14 6:13am    
This is better solution then #1, you don't change the available data, just bind the columns you need. Just replace datatable with dataset in using and fill, and set datasource to dataset.tables[0].
Prasad Avunoori 9-Sep-14 6:16am    
But lines of code matters.
C#
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;
      }
 
Share this answer
 
Comments
Black_Rose 9-Sep-14 6:10am    
Please read the question..i asked for dataset not datatable.
Prasad Avunoori 9-Sep-14 6:15am    
Hello Black Rose. We don't have columns in Dataset. Datatable of Dataset only can have columns. How would you expect columns in Dataset????????????
Sinisa Hajnal 9-Sep-14 6:11am    
Quite the same, dataset contains one or more datatables.

Better yet, bind only the columns you need with added advantage of having your own header texts by disabling AutoGenerateColumns (set to false)
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 ;


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.
 
Share this answer
 
v2
Bind full dataset (12 clmn ) to gridview and display only 6 .
For ex: if your dataset contain id and name , you just want to display only name then id don't include in design

<asp:gridvew runtat="server" ......="" xmlns:asp="#unknown">

<columns> <asp:templatefield headertext="NAME">
<headerstyle width="40">
<itemstyle horizontalalign="Center">
<itemtemplate>
<%#Eval("Name"%>




 
Share this answer
 
Comments
Sinisa Hajnal 9-Sep-14 6:32am    
This still means you have to turn off AutoGenerateColumns
[no name] 11-Sep-14 7:01am    
yes..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900