Solved

Using only part of the data in a datagrid

Posted on 2004-09-24
6
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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