PC Review


Reply
Thread Tools Rate Thread

How do I get the column index of a datagrid from the column title?

 
 
Moondog
Guest
Posts: n/a
 
      1st Sep 2004
I am referencing a datagrid item like this:

strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0)


The Item Number is in the first column; column 0. It works fine
except this is not a very good programming practice. What if someday
someone decides to move the "item number" column to the fourth or
fifth position?


I'm looking for a way to reference the column index using the column
name, so I could write the statement something like this:


strItemNo = grdItems.Item(grdItems.CurrentRowIndex(),
grdItems.Column("Item Number"))


That way it doesn't matter what position the "Item Number" column is
in, the line will still work. Is there a way to do this?

Thanks,
Dominic Isaia
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Imran Koradia
Guest
Posts: n/a
 
      1st Sep 2004
Are you trying to access the contents of the currently active cell? If so,
would this work for you?

grdItems.Item(grdItems.CurrentCell.RowNumber,
grdItems.CurrentCell.ColumnNumber)

hope this helps..
Imran.

"Moondog" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I am referencing a datagrid item like this:
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0)
>
>
> The Item Number is in the first column; column 0. It works fine
> except this is not a very good programming practice. What if someday
> someone decides to move the "item number" column to the fourth or
> fifth position?
>
>
> I'm looking for a way to reference the column index using the column
> name, so I could write the statement something like this:
>
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(),
> grdItems.Column("Item Number"))
>
>
> That way it doesn't matter what position the "Item Number" column is
> in, the line will still work. Is there a way to do this?
>
> Thanks,
> Dominic Isaia
> (E-Mail Removed)



 
Reply With Quote
 
VJ
Guest
Posts: n/a
 
      1st Sep 2004
There is no direct way to access the column through name.. You could through
the dataset that is bound to the column, i.e if you are using bound data
mode. I am not sure if you do... Please refer to the following and see if
you can find anything

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp

VJ

"Moondog" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am referencing a datagrid item like this:
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0)
>
>
> The Item Number is in the first column; column 0. It works fine
> except this is not a very good programming practice. What if someday
> someone decides to move the "item number" column to the fourth or
> fifth position?
>
>
> I'm looking for a way to reference the column index using the column
> name, so I could write the statement something like this:
>
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(),
> grdItems.Column("Item Number"))
>
>
> That way it doesn't matter what position the "Item Number" column is
> in, the line will still work. Is there a way to do this?
>
> Thanks,
> Dominic Isaia
> (E-Mail Removed)



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      1st Sep 2004
Moondog,

In addition to the others, it is a better way to access a datagrid through
its datasource, where is the it even better to use for that to show the
dataview.

Otherwise you can get problems as well when there the users sorts.

something as rougly typed in this message
\\\
dim dv as new dataview(mydataset.table)
dg.datasource = dv
dim cma as new currencymanger
cma = directcast(bindingcontext(dv), currencymanager)
///
\\\
In a button item
cma.endcurrentedit
whatIwant = dv(cma.postition)("mycolumn")
///

I hope this helps?

Cor

> I am referencing a datagrid item like this:
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0)
>
>
> The Item Number is in the first column; column 0. It works fine
> except this is not a very good programming practice. What if someday
> someone decides to move the "item number" column to the fourth or
> fifth position?
>
>
> I'm looking for a way to reference the column index using the column
> name, so I could write the statement something like this:
>
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(),
> grdItems.Column("Item Number"))
>
>
> That way it doesn't matter what position the "Item Number" column is
> in, the line will still work. Is there a way to do this?
>
> Thanks,
> Dominic Isaia
> (E-Mail Removed)



 
Reply With Quote
 
Gene Hubert
Guest
Posts: n/a
 
      1st Sep 2004
I had the same issue with the datagrid. See below for what I did...

You call BuildNumNameXrefGrid after you've got columnstyles added to
the grid and anytime the set of columnstyles changes (I think I need
do a hashtable.clear to handle changing the set of visible columns as
I haven't tried that yet). Then call NumFromNameGrid with the column
name to get the column number.

I have a similar set of methods that handles all columns in the
underlying datatable (not just the visible columns). This code
resides in an object that holds configuration information for how the
grid is set up (e.g. number and type of columns, column names, etc.)

Gene H.


Private Shared NumNameXrefGrid As New Hashtable

'handles visible columns in grid
Public Shared Sub BuildNumNameXrefGrid(ByVal dg As DataGrid)
Dim i As Integer
Dim dgtbc As DataGridColumnStyle

For Each dgtbc In dg.TableStyles(0).GridColumnStyles
NumNameXrefGrid.Add(dgtbc.MappingName, i)
i += 1
Next
End Sub

Public Shared Function NumFromNameGrid(ByVal name As String)
As Integer
Return CInt(NumNameXrefGrid(name))
End Function



(E-Mail Removed) (Moondog) wrote in message news:<(E-Mail Removed)>...
> I am referencing a datagrid item like this:
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0)
>
>
> The Item Number is in the first column; column 0. It works fine
> except this is not a very good programming practice. What if someday
> someone decides to move the "item number" column to the fourth or
> fifth position?
>
>
> I'm looking for a way to reference the column index using the column
> name, so I could write the statement something like this:
>
>
> strItemNo = grdItems.Item(grdItems.CurrentRowIndex(),
> grdItems.Column("Item Number"))
>
>
> That way it doesn't matter what position the "Item Number" column is
> in, the line will still work. Is there a way to do this?
>
> Thanks,
> Dominic Isaia
> (E-Mail Removed)

 
Reply With Quote
 
Moondog
Guest
Posts: n/a
 
      1st Sep 2004
I guess there's no simple solution.


Imran: No, I'm trying to get the column number from a column name in
the datagrid.


VJ and Cor: I guess this is the only way to do it. I had made those
datasets temporary and local to my fillgrid function, so maybe I
should bump them up to global.


Thanks for your input guys,
(E-Mail Removed)
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      1st Sep 2004
Moondog,

> VJ and Cor: I guess this is the only way to do it. I had made those
> datasets temporary and local to my fillgrid function, so maybe I
> should bump them up to global.


When you want.

You can as well use when you want
directcast(datagrid.dataview,Dataview).etc any property or method

Andof course the type of the datasource you use, it can as well be datatable
or any other you are using.

I hope this gives some ideas?

Cor


 
Reply With Quote
 
Moondog
Guest
Posts: n/a
 
      2nd Sep 2004
Hubert,
I think your solution is the best workaround. I will try that.

Thanks for your help.
Dominic Isaia
(E-Mail Removed)


(E-Mail Removed) (Gene Hubert) wrote in message news:<(E-Mail Removed)>...
> I had the same issue with the datagrid. See below for what I did...
>
> You call BuildNumNameXrefGrid after you've got columnstyles added to
> the grid and anytime the set of columnstyles changes (I think I need
> do a hashtable.clear to handle changing the set of visible columns as
> I haven't tried that yet). Then call NumFromNameGrid with the column
> name to get the column number.
>
> I have a similar set of methods that handles all columns in the
> underlying datatable (not just the visible columns). This code
> resides in an object that holds configuration information for how the
> grid is set up (e.g. number and type of columns, column names, etc.)
>
> Gene H.
>
>
> Private Shared NumNameXrefGrid As New Hashtable
>
> 'handles visible columns in grid
> Public Shared Sub BuildNumNameXrefGrid(ByVal dg As DataGrid)
> Dim i As Integer
> Dim dgtbc As DataGridColumnStyle
>
> For Each dgtbc In dg.TableStyles(0).GridColumnStyles
> NumNameXrefGrid.Add(dgtbc.MappingName, i)
> i += 1
> Next
> End Sub
>
> Public Shared Function NumFromNameGrid(ByVal name As String)
> As Integer
> Return CInt(NumNameXrefGrid(name))
> End Function
>
>
>
> (E-Mail Removed) (Moondog) wrote in message news:<(E-Mail Removed)>...
> > I am referencing a datagrid item like this:
> >
> > strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0)
> >
> >
> > The Item Number is in the first column; column 0. It works fine
> > except this is not a very good programming practice. What if someday
> > someone decides to move the "item number" column to the fourth or
> > fifth position?
> >
> >
> > I'm looking for a way to reference the column index using the column
> > name, so I could write the statement something like this:
> >
> >
> > strItemNo = grdItems.Item(grdItems.CurrentRowIndex(),
> > grdItems.Column("Item Number"))
> >
> >
> > That way it doesn't matter what position the "Item Number" column is
> > in, the line will still work. Is there a way to do this?
> >
> > Thanks,
> > Dominic Isaia
> > (E-Mail Removed)

 
Reply With Quote
 
Moondog
Guest
Posts: n/a
 
      23rd Sep 2004
gwhubert,

I just implemented your solution. It works great!

Thanks again,
Moondog
(E-Mail Removed)
 
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
Getting the column Index of a datagrid control Sudhir Kesharwani Microsoft ASP .NET 0 20th Dec 2006 11:49 AM
Getting the column Index of a datagrid control Sudhir Kesharwani Microsoft ASP .NET 0 20th Dec 2006 11:48 AM
How to change the title text of DataGrid's column? Michael Microsoft Dot NET Framework Forms 2 24th Oct 2005 02:02 AM
How to find the Column Index By Name in an ASP.Net DataGrid DBLWizard Microsoft ASP .NET 1 19th Jul 2005 10:42 AM
How to set dynamic binding datagrid column title£¿ Emaga75 Microsoft C# .NET 0 28th Jul 2004 06:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 PM.