subset of the fields stored in a dataset

L

labrynth

I have recently started coding in .net technologies. Here is what I
want to do -

I have a dataset that consists of all the rows and columns of a table
(say emp).

I also have a datagrid control on my windows form.

I would like to extract all the row values for a particular field (say
empname) from the dataset, i.e without connecting to the database and
display it in my datagrid.

Can someone tell me how to do this -

here is the code -

OleDbDataConnection cn = new OleDbDataConnection(connectionstring);
OleDbDataAdapter da = new OleDbDataAdapter("select * from emp" , cn);

DataSet ds = new DataSet();

da.Fill(ds, "e");
From ds, I would like all the row values of the column/field empname in
either a seperate table, or view, or an array - something that I can
directly use as a datasource for my datagrid.

Thanks,
Lb
 
C

Cor Ligthert [MVP]

Labrynth,

Be aware that the dataset is a wrapper around datatables and datarelations.

Therefore you show the rows of a datatable in your grid.
There are more posibilities in your case it can be just.

DataGrid1.DataSource = ds.Tables(0);

(I would never name your table "e". That is one of the worst names for a
variable in Net. It is the standard for the result of an event. "emp" is
better in this case.

I hope this helps,

Cor
 
L

labrynth

Thank you Cor, for the reply.

My question was, how do I display all the rows pertaining to a single
"column/field" of a dataset (which as you said is a wrapper over
multiple tables and relations in a database).


DataGrid1.DataSource = ds.Tables[0] would get me all the rows
pertaining to all the columns (as opposed to just the one column
empname) from the dataset table 'emp'.

I need all the row values pertaining to just one column from the
dataset table 'emp' displayed in the datagrid.

Basically I have a form , that lets you select a table, and then
displays all the fields of that table in a list box, and upon selection
of fields from teh list box, displays all the row values for the fields
selected, in a data grid.
 
C

Cor Ligthert [MVP]

Labrynth
My question was, how do I display all the rows pertaining to a single
"column/field" of a dataset (which as you said is a wrapper over
multiple tables and relations in a database).
DataGrid1.DataSource = ds.Tables[0]
ds.Tables[0].DefaultView.Rowfilter = "Emp = 'Whatever'"; // for a string
DataGrid1.DataSource = ds.Tables[0].DefaultView;

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top