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

M

Moondog

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 address removed)
 
I

Imran Koradia

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.
 
C

Cor Ligthert

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
 
G

Gene Hubert

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
 
M

Moondog

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 address removed)
 
C

Cor Ligthert

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
 
M

Moondog

Hubert,
I think your solution is the best workaround. I will try that.

Thanks for your help.
Dominic Isaia
(e-mail address removed)
 
M

Moondog

gwhubert,

I just implemented your solution. It works great!

Thanks again,
Moondog
(e-mail address removed)
 

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