views:

485

answers:

3

(VS2008) I'm trying to configure a TableAdapter in a Typed DataSet to return only a certain subset of columns from the main schema of the table on which it is based, but it always returns the entire schema (all columns) with blank values in the columns I have omitted.

The TableAdpater has the default Fill and GetData() methods that come from the wizard, which contain every column in the table, which is fine. I then added a new parameterized query method called GetActiveJobsByCustNo(CustNo), and I only included a few columns in the SQL query that I actually want to be in this table view.

But, again, it returns all the columns in the master table schema, with empty values for the columns I omitted.

The reason I am wanting this, is so I can just get a few columns back to use that table view with AutoGenerateColumns in an ASP.NET GridView. With it giving me back EVERY column i nthe schema, my presentation GridView contains way more columns that I want to show th user. And, I want to avoid have to declare the columns in the GridView.

+1  A: 

Is the strongly typed dataset used in another query that returns all the rows from the table?

What you could do is create a dataview using the strongly typed dataset and expose a data table for your DataGridView.

I'm not sure what your requirements are totally, but this example should help you:

DataView dv = new DataView(ds.<Your_Table>);

// This will create a new data table with the same name,
// But with only two columns from the original table.
// This could then be bound to your data grid. 
DataTable dt = dv.ToTable(false,
                          ds.<Your_Table>.<Your_Column1Column>.ColumnName,
                          ds.<Your_Table>.<Your_Column1Column>.ColumnName);
Justin Bannister
What do you mean by "create a dataview"? That's what I was trying to do by adding a new custom TableAdapeter named GetCustomersWithOpenOrders(CustNo)
MattSlay
No, look up the DataView class, Justin could be right. It sits between the Table and the Grid.
Henk Holterman
+1  A: 

When you add a new query to a given TableAdapter, it is going to assume the schema in which it is attached to, which is why you are getting blank values for the columns you don't want.

Since you mentioned having already created the procedure, what you need to do is use the Server Explorer to connect to the database and simply drag that stored procedure over into your XSD work area. What this will do is create a separate QueryAdapter that will have just the columns you specified (still strongly typed) and you can bind/interact with your GridView using that QueryAdapter instead.

Dillie-O
No stored procedures here. I added another TableAdapter definition in addition to the default ones.
MattSlay
Gotcha. Will the QueryAdapter allow you to specify an existing TableAdapter/DataTable when setting things up? I'm thinking this is still the best route to go.
Dillie-O
A: 

Just delete the columns you don't want at run-time before you bind to your Gridview. The underlying class is still just a DataTable after all.

Carter