Simple Question - Datagrid and Typed dataset

S

Saber

I did a lot of searches and read something about
datagrids. But I couldn't find the answer of my simple
question, how can I show only my desired columns of
a table? for example I wrote this sql query:

OleDbDataAdapter1.SelectCommand.CommandText = & _
"Select illNameE From tblIllness"
OleDbDataAdapter1.Fill(DsIllness1)

But in my datagrid, I get (null) for other ccolumns instead
of hiding undesired columns.

Another question: Is datagrid best choice in windows form
to show a list of "clickable" data?

TIA
 
W

W.G. Ryan MVP

With a datagrid, if you set the column width of the columns you want hidden
to 0 or you can just not provide a ColumnMapping for them. Check out this
class http://www.knowdotnet.com/articles/cgrid.html - the code that's
included with it will show you how to do most anything you will want to do
with a grid.. There's also a NullText property you can set for each Column
that has DataGridColumnStyle that will substitute the value you specify
instead of showing (Null) although frm the sound of yoru entire question,
you don't want a value there, you want the column hidden.

Anyway, as far as 'best' goes, that depends on a lot of things but as far as
a 'list of clickable data', actually, the grid probably isnt' that great of
a choice. Out of the box it has sorting capabilities, but if you bind to a
DataView instead of a DataTable, it's easy enough to implement through code.
The reason I say this is b/c if you only have one column in a list, the grid
has a lot of extra hoops you need to jump through to get a given value that
a ListBox or ComboBox doesn't (not that I'm saying it's 'hard', I just think
that for clickable funcionality in a list - that the ListBox or ComboBox is
probably a better choice in most cases. A Listbox is probably a better
choice for a single column list b/c you can just reference what they click
with SelectedItem and SelectedValue (remember to set the DisplayMember and
ValueMembers - this way, you can show one value but use a different one -
something which is a pain to do in a grid)
 
S

Saber

Thanks for your complete and precise answer.

Just in some cases I need to hide columns and the default
view includes all columns or more than one column.
alhtough, there is a multi-column list box anc combox I can
use as an alternative:
http://www.codeproject.com/cs/combobox/multicolumnlistbox.asp
http://www.codeproject.com/vb/net/MultiColumnFlatCombo.asp

--
Saber S.
http://maghalat.com

W.G. Ryan MVP said:
With a datagrid, if you set the column width of the columns you want
hidden to 0 or you can just not provide a ColumnMapping for them. Check
out this class http://www.knowdotnet.com/articles/cgrid.html - the code
that's included with it will show you how to do most anything you will
want to do with a grid.. There's also a NullText property you can set for
each Column that has DataGridColumnStyle that will substitute the value
you specify instead of showing (Null) although frm the sound of yoru
entire question, you don't want a value there, you want the column hidden.

Anyway, as far as 'best' goes, that depends on a lot of things but as far
as a 'list of clickable data', actually, the grid probably isnt' that
great of a choice. Out of the box it has sorting capabilities, but if you
bind to a DataView instead of a DataTable, it's easy enough to implement
through code. The reason I say this is b/c if you only have one column in
a list, the grid has a lot of extra hoops you need to jump through to get
a given value that a ListBox or ComboBox doesn't (not that I'm saying it's
'hard', I just think that for clickable funcionality in a list - that the
ListBox or ComboBox is probably a better choice in most cases. A Listbox
is probably a better choice for a single column list b/c you can just
reference what they click with SelectedItem and SelectedValue (remember to
set the DisplayMember and ValueMembers - this way, you can show one value
but use a different one - something which is a pain to do in a grid)
 
S

Saber

Thanks Earl,
As I said before, if I don't select those columns in query, I get
columns with (null) text instead of hidden columns.
But MappingType.Hidden is good for me, Thanks for that.

--
Saber S.
http://maghalat.com
Earl said:
I'm speculating that you are referring to situations where you pull more
than one column of data.

You could simply not Select those columns in your query.

But if you do, you could use MappingType.Hidden to hide those in the grid:

ds.Tables("dtCosts").Columns("SalesID").ColumnMapping = MappingType.Hidden
 
E

Earl

I'm speculating that you are referring to situations where you pull more
than one column of data.

You could simply not Select those columns in your query.

But if you do, you could use MappingType.Hidden to hide those in the grid:

ds.Tables("dtCosts").Columns("SalesID").ColumnMapping = MappingType.Hidden
 
S

Saber

What's wrong here?

DsIllness1.tblIllness.illIDColumn.ColumnMapping = MappingType.Hidden
DsIllness1.tblIllness.illDescColumn.ColumnMapping = MappingType.Hidden
OleDbDataAdapter1.Fill(DsIllness1)

Datagrid shows illDescColumn and illIDColumn yet :(
 
E

Earl

You can't hide something you haven't filled yet.

Saber said:
What's wrong here?

DsIllness1.tblIllness.illIDColumn.ColumnMapping = MappingType.Hidden
DsIllness1.tblIllness.illDescColumn.ColumnMapping = MappingType.Hidden
OleDbDataAdapter1.Fill(DsIllness1)

Datagrid shows illDescColumn and illIDColumn yet :(
 
E

Earl

da.Fill(ds, "dtCosts")

ds.Tables("dtCosts").Columns("SalesID").ColumnMapping = MappingType.Hidden
ds.Tables("dtCosts").Columns("LaborCostsID").ColumnMapping =
MappingType.Hidden
....
 
S

Saber

It is untyped dataset, what about typed dataset?
I've no DataTable.


--
Saber S.
http://maghalat.com
Earl said:
da.Fill(ds, "dtCosts")

ds.Tables("dtCosts").Columns("SalesID").ColumnMapping = MappingType.Hidden
ds.Tables("dtCosts").Columns("LaborCostsID").ColumnMapping =
MappingType.Hidden
...

Saber said:
But the last line of code fills:
OleDbDataAdapter1.Fill(DsIllness1)


--
Saber S.
http://maghalat.com
Earl said:
You can't hide something you haven't filled yet.

"Saber" <saber[.AT.]oxin.ir> wrote in message
What's wrong here?

DsIllness1.tblIllness.illIDColumn.ColumnMapping = MappingType.Hidden
DsIllness1.tblIllness.illDescColumn.ColumnMapping = MappingType.Hidden
OleDbDataAdapter1.Fill(DsIllness1)

Datagrid shows illDescColumn and illIDColumn yet :(
 
C

Cor Ligthert [MVP]

Saber,

Did you try it,

A typed dataset inherits from an untyped dataset

It tells by instance that the property 'ds.dtCosts' is the same as
'ds.Tables("dtCosts")'

I hope this helps,

Cor
 
S

Saber

Thanks Earl and Cor,
It has been done by using a DataTable instead of Dataset:
Dim dt As New DataTable
OleDbDataAdapter1.Fill(dt)
dt.Columns("illID").ColumnMapping = MappingType.Hidden
dg.DataSource = dt.DefaultView

I don't know it is a good method or not, but it works..
 

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