PC Review


Reply
Thread Tools Rate Thread

DataGrid Colum Names

 
 
Fred Nelson
Guest
Posts: n/a
 
      12th Apr 2004
I have an application in which it would be VERY beneficial if I could obtain
the names of the colums in a datagrid.

For example

dim datagrid1 as new datagrid
datagrid1.datasource = (stored procedure that loads datagrid)
datagrid1.databind()

I am able to obtain the data in the datagrid by its relative number however
I can't figure out how to get the names and in this application they may
change over time.

Any help would be GREATLY appreciated!

Thanks,

Fred


 
Reply With Quote
 
 
 
 
William Ryan eMVP
Guest
Posts: n/a
 
      12th Apr 2004
If it's bound to a grid, then the grid column should correspond with the
columns in the underlying datatable. As such, you can interrogate the
DataTable's column's collection and just lookup the ColumnName property ie
DataTable.Columns[0].ColumnName;

HTH,

bill
"Fred Nelson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have an application in which it would be VERY beneficial if I could

obtain
> the names of the colums in a datagrid.
>
> For example
>
> dim datagrid1 as new datagrid
> datagrid1.datasource = (stored procedure that loads datagrid)
> datagrid1.databind()
>
> I am able to obtain the data in the datagrid by its relative number

however
> I can't figure out how to get the names and in this application they may
> change over time.
>
> Any help would be GREATLY appreciated!
>
> Thanks,
>
> Fred
>
>



 
Reply With Quote
 
Fred Nelson
Guest
Posts: n/a
 
      13th Apr 2004
Bill:

Thanks for your reply - it looks like I may get this to work!

I don't understand one thing - in the example that I provided I bound the
data grid to a class library with a stored procedure that loaded it. I'm
unclear how to reference the underlying data table in this example where
datagrid1 contains the data.

DataGrid1.Colums[0].ColumnName is not valid syntax -

Do I need to create a data table from the stored procedure rather than a
data grid - or is there something I'm missing?

Thanks again for your help!

Fred


"William Ryan eMVP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If it's bound to a grid, then the grid column should correspond with the
> columns in the underlying datatable. As such, you can interrogate the
> DataTable's column's collection and just lookup the ColumnName property ie
> DataTable.Columns[0].ColumnName;
>
> HTH,
>
> bill
> "Fred Nelson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I have an application in which it would be VERY beneficial if I could

> obtain
> > the names of the colums in a datagrid.
> >
> > For example
> >
> > dim datagrid1 as new datagrid
> > datagrid1.datasource = (stored procedure that loads datagrid)
> > datagrid1.databind()
> >
> > I am able to obtain the data in the datagrid by its relative number

> however
> > I can't figure out how to get the names and in this application they may
> > change over time.
> >
> > Any help would be GREATLY appreciated!
> >
> > Thanks,
> >
> > Fred
> >
> >

>
>



 
Reply With Quote
 
William Ryan eMVP
Guest
Posts: n/a
 
      13th Apr 2004
Fred:

You'll reference the DataTable that you bound the grid to. Whatever it's
name is, reference it. How it got filled (ie Stored Proc) doesn't matter.
Depending on how you called DataAdapter.Fill, you may want to use the
Numeric Index of hte Tables collection ie
myDataSet.Tables[0].Columns[0].ColumnName;

conceptually, focus on hitting the datasource with wahtever you want. The
grid is just the presentation of the underlying data obejct. On the Web,
changes won't necessarily be reflected visibly until a postback occurs, but
on the desktop, as soon as you change the underlying table value
programmatically, the change will be visible in the Grid immediately.

HTH,

Bill
"Fred Nelson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bill:
>
> Thanks for your reply - it looks like I may get this to work!
>
> I don't understand one thing - in the example that I provided I bound the
> data grid to a class library with a stored procedure that loaded it. I'm
> unclear how to reference the underlying data table in this example where
> datagrid1 contains the data.
>
> DataGrid1.Colums[0].ColumnName is not valid syntax -
>
> Do I need to create a data table from the stored procedure rather than a
> data grid - or is there something I'm missing?
>
> Thanks again for your help!
>
> Fred
>
>
> "William Ryan eMVP" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > If it's bound to a grid, then the grid column should correspond with the
> > columns in the underlying datatable. As such, you can interrogate the
> > DataTable's column's collection and just lookup the ColumnName property

ie
> > DataTable.Columns[0].ColumnName;
> >
> > HTH,
> >
> > bill
> > "Fred Nelson" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > I have an application in which it would be VERY beneficial if I could

> > obtain
> > > the names of the colums in a datagrid.
> > >
> > > For example
> > >
> > > dim datagrid1 as new datagrid
> > > datagrid1.datasource = (stored procedure that loads datagrid)
> > > datagrid1.databind()
> > >
> > > I am able to obtain the data in the datagrid by its relative number

> > however
> > > I can't figure out how to get the names and in this application they

may
> > > change over time.
> > >
> > > Any help would be GREATLY appreciated!
> > >
> > > Thanks,
> > >
> > > Fred
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Fred Nelson
Guest
Posts: n/a
 
      13th Apr 2004
Bill:

Thanks for your quick answer - I really appreciate it!

I am a newby and so I'm confused about the reply that you gave. I have
loaded my datagrid with the following:

dim myproclib as new datafunctions.datalib

dim datagrid1 as new datagrid
datagrid1.datasource = myproclib.getstates
datagrid1.databind()

So I haven't (or I think I haven't) created a data table and then bound the
grid to it. Should I create a datatable from the stored procedure? If I
create one then can I populate it without knowing the column names I will
receive?

Again, thanks for your help!

Fred

"William Ryan eMVP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Fred:
>
> You'll reference the DataTable that you bound the grid to. Whatever it's
> name is, reference it. How it got filled (ie Stored Proc) doesn't matter.
> Depending on how you called DataAdapter.Fill, you may want to use the
> Numeric Index of hte Tables collection ie
> myDataSet.Tables[0].Columns[0].ColumnName;
>
> conceptually, focus on hitting the datasource with wahtever you want. The
> grid is just the presentation of the underlying data obejct. On the Web,
> changes won't necessarily be reflected visibly until a postback occurs,

but
> on the desktop, as soon as you change the underlying table value
> programmatically, the change will be visible in the Grid immediately.
>
> HTH,
>
> Bill
> "Fred Nelson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Bill:
> >
> > Thanks for your reply - it looks like I may get this to work!
> >
> > I don't understand one thing - in the example that I provided I bound

the
> > data grid to a class library with a stored procedure that loaded it.

I'm
> > unclear how to reference the underlying data table in this example where
> > datagrid1 contains the data.
> >
> > DataGrid1.Colums[0].ColumnName is not valid syntax -
> >
> > Do I need to create a data table from the stored procedure rather than a
> > data grid - or is there something I'm missing?
> >
> > Thanks again for your help!
> >
> > Fred
> >
> >
> > "William Ryan eMVP" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > If it's bound to a grid, then the grid column should correspond with

the
> > > columns in the underlying datatable. As such, you can interrogate the
> > > DataTable's column's collection and just lookup the ColumnName

property
> ie
> > > DataTable.Columns[0].ColumnName;
> > >
> > > HTH,
> > >
> > > bill
> > > "Fred Nelson" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > I have an application in which it would be VERY beneficial if I

could
> > > obtain
> > > > the names of the colums in a datagrid.
> > > >
> > > > For example
> > > >
> > > > dim datagrid1 as new datagrid
> > > > datagrid1.datasource = (stored procedure that loads datagrid)
> > > > datagrid1.databind()
> > > >
> > > > I am able to obtain the data in the datagrid by its relative number
> > > however
> > > > I can't figure out how to get the names and in this application they

> may
> > > > change over time.
> > > >
> > > > Any help would be GREATLY appreciated!
> > > >
> > > > Thanks,
> > > >
> > > > Fred
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      13th Apr 2004
Hi Fred,

On the winforms version, you can't bind to a DataReader at all like you can
in ASP.NET which tells you something...

Can it be that your datasource is a datareader?

Than the name of your column can be
Dim William As String = rdr.GetName(0)

(And I wished Bill saw my face when I wrote this and this is the true
situation, however that is between me and him and only fun of course)

And Fred, if this is not clear, feel free to ask further I think we where on
the wrong road.

I hope this helps?

Cor.


 
Reply With Quote
 
Fred Nelson
Guest
Posts: n/a
 
      13th Apr 2004
Cor:

Thanks for your reply - and patience with me - I'm new to this and it will
be worth it when I learn it!!!!

I'm still lost - the code I'm using is as follows

----
dim myproclib as new datafunctions.datalib

dim datagrid1 as new datagrid
datagrid1.datasource = myproclib.getstates (this is a class lib that
calles a stored procedure)
datagrid1.databind()
-----

So I don't think I'm using a data reader or binding.

So: Given the datagrid - is there any way to get the column names?

If I need to build a data table then I assume there is another step?

Thanks,

Fred


"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi Fred,
>
> On the winforms version, you can't bind to a DataReader at all like you

can
> in ASP.NET which tells you something...
>
> Can it be that your datasource is a datareader?
>
> Than the name of your column can be
> Dim William As String = rdr.GetName(0)
>
> (And I wished Bill saw my face when I wrote this and this is the true
> situation, however that is between me and him and only fun of course)
>
> And Fred, if this is not clear, feel free to ask further I think we where

on
> the wrong road.
>
> I hope this helps?
>
> Cor.
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      13th Apr 2004
Hi Fred,

Can you add this to your after that databind
> dim datagrid1 as new datagrid
> datagrid1.datasource = myproclib.getstates (this is a class lib that
> calles a stored procedure)
> datagrid1.databind()


Dim a As String = DataGrid1.DataSource.ToString

And than see in the debugger what is the datasource?

Cor







 
Reply With Quote
 
Fred Nelson
Guest
Posts: n/a
 
      13th Apr 2004
Coor:

There is no .ToString method - I get a crash:

"Cast from type SqlDataReader to Type String is not valid"

Fred


"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi Fred,
>
> Can you add this to your after that databind
> > dim datagrid1 as new datagrid
> > datagrid1.datasource = myproclib.getstates (this is a class lib that
> > calles a stored procedure)
> > datagrid1.databind()

>
> Dim a As String = DataGrid1.DataSource.ToString
>
> And than see in the debugger what is the datasource?
>
> Cor
>
>
>
>
>
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      13th Apr 2004
Hi Fred,

This is weird, I tested it in advance and it did work, however in my
opinion it can be that it shows that your datasource is a datareader and
than you can try my suggestion.

Cor

> Coor:
> There is no .ToString method - I get a crash:
> "Cast from type SqlDataReader to Type String is not valid"
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HYPERLINKING PDF FILES HAVING NAMES IN COLUM TO A PDF FILE IN A FO COOLGUY_IVRCL Microsoft Excel Misc 4 6th Feb 2010 05:54 PM
alphabetize the names colum on the excel =?Utf-8?B?bGVl?= Microsoft Excel New Users 1 1st Jun 2007 05:07 PM
Programically update a colum in a datagrid =?Utf-8?B?Smlt?= Microsoft VB .NET 3 19th Sep 2005 07:55 PM
Datagrid Colum Header Aligment in C# huseyin korkmaz Microsoft C# .NET 2 19th Jul 2005 04:11 PM
Getting Hidden Colum Text DataGrid Pete Davis Microsoft C# .NET 1 22nd Dec 2003 06:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:57 AM.