Solved

Using only part of the data in a datagrid

Posted on 2004-09-24
6
160 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you be more specific?  How exactly do I use that?  It sounds like what I'm looking for.

JP
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:gleznov
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now