?
Solved

Typed dataset select

Posted on 2010-04-06
6
Medium Priority
?
384 Views
Last Modified: 2012-05-09
Hi

I have typed dataset. Created this with XSD in VS 2010, called ProductionData.  Note the data is not from a database, it is a strongly typed.  

To bind it to a gridview on form I do this:

   Dim ds = Me.ProductionData
   DataGridView1.DataSource = ds

This binds all the fields. I'd like to bind only some e.g. Field1, Field2, Field3

Is there a "Select" statement one can do to only select a few fields?

Thanks in advance
0
Comment
Question by:rwallacej
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:RameshS
ID: 29926198
If you are not using  template columns in the GridView and AutoGenerateColumns is set to true, The gridview will display all the column of the dataset. If you want to display only specific columns, you can hide the unwanted columns  using the following statement

GridView.Columns[3].Visible = false;

If you are  using bound and template columns in GridView and AutoGenerateColumns is set to false, the GridView will display only those bound columns though the DataSet has many columns.
0
 

Author Comment

by:rwallacej
ID: 29927154
hi,

I'd like to keep the Gridview AutoGenerateColumns at True, and do a SELECT statement on the dataset (I presume there is a way to do this?)

thanks
0
 
LVL 10

Accepted Solution

by:
RameshS earned 2000 total points
ID: 29927648
I was specifying a solution for GridView which is specific to asp.net. But GridView and DataGridView
have AutoGenerateColumns  property which behaves in the same manner in the above controls. Also both have template columns.  So you can hide unwanted columns in DataGridView as below

DataGridView1.Columns[3].Visible = false;

Alternatively you can filter the columns of a datatable in the following way specied in the code snippet.

Here the DataTable dtNew  will have only records for columns CompanyName and Location. You can try to create a datable from the base datatable and bind it to DataGrid
DataTable dtComp = new DataTable();
        dtComp.Columns.Add("CompanyName", System.Type.GetType("System.String"));
        dtComp.Columns.Add("Ticker", System.Type.GetType("System.String"));
        dtComp.Columns.Add("Location", System.Type.GetType("System.String"));

        dtComp.Rows.Add(new object[] { "Microsoft", "Msft","Location1" });
        dtComp.Rows.Add(new object[] { "Intel", "Intc", "Location2" });
        dtComp.Rows.Add(new object[] { "Dell", "Dell", "Location3" });
        DataView dv = new DataView(dtComp);
        DataTable dtNew = dv.ToTable(false, new string[] { "CompanyName", "Location" });    

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:rwallacej
ID: 29927846
thanks for more info

I see there is a .Select function on the custom dataset


Me.ProductionData.Select (.......)

can this be used to select only particular columns, and so not have to recreate table?

0
 
LVL 10

Expert Comment

by:RameshS
ID: 29928911
The Select method can be used to filter rows not columns. It wil return array of DataRows based on the condition given.

The DataSet/DataTable does not have a built-in feature to filter columns.
0
 

Author Comment

by:rwallacej
ID: 29929947
thank-you for help
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question