Results 1 to 11 of 11

Thread: Click on a Datagrid cell, get the Column name and the cell data

  1. #1

    Thread Starter
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206

    Click on a Datagrid cell, get the Column name and the cell data

    How should I do this? I'm going to use the Doubleclick event.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Use DataGrid.HitTestInfo
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    And you better use MouseDown event cause it seems that doubleClick is not fired on cells unless on borders.
    VB Code:
    1. Private Sub dg1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dg1.MouseDown
    2.        
    3.         Dim hti As DataGrid.HitTestInfo = dg1.HitTest(e.X, e.Y)
    4.             MessageBox.Show(dg1(hti.Row, hti.Column))
    5. End Sub
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4

    Thread Starter
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Hey, thanks! I'll try it tomorrow at work!

  5. #5

    Thread Starter
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Lunatic I can't get the Header name, since the hti.Row value can't be below 0, and the column header is the -1 position.
    Any workaround?

  6. #6

    Thread Starter
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    I really need help guys, fast. I have a presentation of the program with the client in 1:30h.

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    hold on i will send you the code
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8

    Thread Starter
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Ok, thanks I got it in extremis.
    VB Code:
    1. str_FieldName = DataGrid1.TableStyles(0).GridColumnStyles(DataGrid1.CurrentCell.ColumnNumber).MappingName

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    That code will make some problems for you. By the way that will return the corresponding name of the coulmn in table not the header text of that column that are not necessaarily the same.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  10. #10
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    This code will be helpful if datagrid datasource is datatable
    VB Code:
    1. Dim hti As DataGrid.HitTestInfo = dg1.HitTest(e.X, e.Y)
    2. Dim htt As DataGrid.HitTestType = hti.Type
    3. Dim dgds As Object = dg1.DataSource
    4. If htt = DataGrid.HitTestType.ColumnHeader Then
    5.      MessageBox.Show(CType(dgds, DataTable).Columns(hti.Column).ColumnName)
    6. ElseIf htt = DataGrid.HitTestType.Cell Then
    7.     MessageBox.Show(hti.Row.ToString & ":" & hti.Column.ToString)
    8.      MessageBox.Show(dg1(hti.Row, hti.Column))
    9.      MessageBox.Show(CType(dgds, DataTable).Columns(hti.Column).ColumnName)
    10. End If
    Last edited by Lunatic3; Apr 8th, 2003 at 04:04 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  11. #11

    Thread Starter
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Thanks for the tip and the code, but don't worry, the code I wrote does exactly what's supposed to do. Because used the mapping name and I built the headers with "nicer" captions, since the field names in the DataSet aren't all pretty and with accents that the portuguese language has. So I made a New DataGridTableStyle, that's why what it interests me to manipulate is the MappingName, not the HeaderText.

    But thank you once again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width