DataGridColumnName based on DataGrid.CurrentCell

  • Thread starter Thread starter Strahimir Antoljak
  • Start date Start date
S

Strahimir Antoljak

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,
 
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
 
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

from here get your currentrowindex
 
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
 
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

William Ryan eMVP said:
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
 
Cor,

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