Solved

Using only part of the data in a datagrid

Posted on 2004-09-24
6
168 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

770 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