Groups | Blog | Home
all groups > vb.net > april 2004 >

vb.net : DataGridColumnName based on DataGrid.CurrentCell


Strahimir Antoljak
4/15/2004 11:27:18 PM
Is it possible (and how?) to get the data grid column name
based on the DataGrid.CurrentCell?

The DataGrid has assigned a TableStyle comprising
selected columns (DataGridTextBoxColumn) from
the source DataTable (DataGrid.DataSource=DataTable).
I would like to be able to get the column name of the
datagrid based on the DataGrid current cell so I can reference
the appropriate column in the source DataTable.

Thanks,

--
Strah @ Langan

William Ryan eMVP
4/16/2004 3:27:23 AM
You can use the ColumnIndex of the CurrentCell property to reference the
underlying datatable's colum.columnName property. So if ColumnIndex of the
current cell is 1, then you can use DataTable.Columns(1).ColumnName to get
this info. You can just substitute it where I hard coded 1.

HTh,

Bill
[quoted text, click to view]

adamz5 NO[at]SPAM hotmail.com
4/16/2004 4:05:38 AM
use the mouse down event on the datagrid

and use System.Windows.Forms.DataGrid.HitTestInfo to get pos of column

something like:

objHitTest = Dgd.HitTest(e.X, e.Y)
Select Case objHitTest.Type
Case System.Windows.Forms.DataGrid.HitTestType.Cell
strPartHit = "Cell"
Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
strPartHit = "ColumnHeader"
Case System.Windows.Forms.DataGrid.HitTestType.ColumnResize
strPartHit = "Resize"
Case System.Windows.Forms.DataGrid.HitTestType.Caption
strPartHit = "Caption"
Case System.Windows.Forms.DataGrid.HitTestType.ParentRows
strPartHit = "ParentRows"
Case System.Windows.Forms.DataGrid.HitTestType.RowHeader
strPartHit = "RowHeader"
If e.Clicks = 2 Then
'run your code here
End If
Case System.Windows.Forms.DataGrid.HitTestType.RowResize
strPartHit = "RowResize"
Case System.Windows.Forms.DataGrid.HitTestType.None
strPartHit = "None"
Case Else
strPartHit = "Unknown"
End Select

Strahimir Antoljak
4/16/2004 9:01:15 AM
thanks Hth but this would not work in my
case as I am using a TableStyle comprising
selected columns from the DataTable, which
means ColumnIndex property of the CurrentCell
does not necessarily match the column index
in the DataTable; the DataTable has 11 columns
while TableStyle can have any number of columns
between 2 and 11 - so I create TableStyle that
contains 2nd, 4th and 8th column in the DataTable,
but in DataGrid they have indices 0, 1, 2.

Thanks again,

--
Strah @ Langan

[quoted text, click to view]

Strahimir Antoljak
4/16/2004 9:16:54 AM

Thanks adamz5.... I am testing it right now,
looks good.

--
Strah @ Langan

[quoted text, click to view]

Strahimir Antoljak
4/16/2004 9:19:40 AM
Cor,

this is not first time you helped me with elegant solutions.
It works like a charm. Many thanks.

--
Strah @ Langan

[quoted text, click to view]

Cor Ligthert
4/16/2004 2:04:02 PM
Hi Strah,

Can you try this one (it is one long sentence)
Dim name As String =
DataGrid1.TableStyles(0).GridColumnStyles(DataGrid1.CurrentCell.ColumnNumber
).MappingName

I hope this works easy?

Cor

AddThis Social Bookmark Button