Solved

Using only part of the data in a datagrid

Posted on 2004-09-24
6
171 Views
Last Modified: 2010-04-23
Hi,

     I'm filling a dataset with an entire table (because my code encounters errors if I don't).  Then I want only 4 columns of that table to appear in a datagrid.  Here's where I'm messed up:

I use a dataadapter to define a select * on my table, supplies_trans.  Then I generate a dataset from that.

I use this code to try and start up:

 Dim DRGrid As New SqlClient.SqlDataAdapter("select * from supplies_trans order by trans_date", SqlConnection1)
        DRGrid.Fill(DataSet_Supplies1, "Supplies_Trans")

        DBGrid1.DataSource = DataSet_Supplies1
        DBGrid1.DataMember = "Supplies_Trans"

The DRGrid.Fill crashes me in these cases:

1) if the statement above it has select item, item alias, item etc (so I have to use select *)

2) if I've created a dataadapter using such a select statement other than select *

So I don't know if I can narrow it down somehow before connecting datasource/member to it.  Maybe make another dataset and pull only the info I want (but I don't know the commands for this (or rather methods/etc)) or to be able to somehow say DBGrid1.only show these columns

JP
0
Comment
Question by:gleznov
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:KGreg
ID: 12147053
Try this..  Does this work?

"SELECT supplies_trans.trans_date, supplies_trans.item FROM supplies_trans ORDER BY supplies_trans.trans_date"

KGreg
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12167075
You can add (in editon time) a new TableStyle in the TableSyleCollection of your DataGrid and map it to the name of you table in DataSet, and then add as new DataGridColumnStyles as you need and map them to the columns (names) you want to display.
0
 

Author Comment

by:gleznov
ID: 12168564
Can you be more specific?  How exactly do I use that?  It sounds like what I'm looking for.

JP
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:gleznov
ID: 12168653
I created a tablestyle with the columns I want using the GUI - the grid is mapped to the table, then I made tablestyles.  Is this wrong?  I still get the full table output (all columns w/ their original names)

JP
0
 

Author Comment

by:gleznov
ID: 12169273
I figured it out - I don't know what was wrong with my original but when I tweaked the select statements around and redid stuff, it worked fine.  I think maybe some minor non-matchup in the select statement for the dataadapter and the one to fill the control were causing my main problems.

JP
0
 
LVL 4

Accepted Solution

by:
eozz_2000 earned 125 total points
ID: 12171110
OK, this is the explanation:

1) If you are setting the dataSource and dataMember of your dataGrid at runtime then go to 4)
2)Set the dataSource of your dataGrid to the dataSet.
3)Set the dataMember of your dataGrid to the table you want to display.
4)In the TableStyles property add a new one DataGridTableStyle and set the property mappingName with the name of your table (exactly the same name that it has in dataSet).
5)In the GridColumnStyles property add as several ones as you want to show and set those mappingName property with the name of the columns mapped (exactly the same that it has in dataSet).  If you had settng the dataSource and dataMember of your dataGrid at edit time then that was all, if not then follow with the explanation.
6)In the code, set the dataSource of your dataGrid to dataSet and set the dataMember or your dataGrid to the name of the table you want to display (exactly the same name that it has in dataSet).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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