none
RowHeader PROBLEM RRS feed

  • Question

  • hi

    I HAVE DATAGRIDVIEW WITH COLUMN BUTTON .

    I SHOW  RowHeader WHEN I CLICK ON IT SHOW VALUE LIKE CLICK CELL BUTTON.

    HOW TO DESABLE  RowHeader CLIECK

    I USED THIS CODE BUT NOTHING HAPPEN

     Private Sub DATAG_CUSTOM_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DATAG_CUSTOM.RowHeaderMouseClick
                   If e.RowIndex > -1 Then
                DATAG_CUSTOM.Rows(e.RowIndex).ReadOnly = TRUE
            End If
        End Sub


    • Edited by ahmeddc Friday, December 21, 2018 11:58 PM
    Friday, December 21, 2018 11:37 PM

Answers

  • See if this is what you are looking for.

    Public Class Form1
        ''' <summary>
        ''' 1. Columns are created at design time
        ''' 2. The row header works regardless of bound or unbound
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DataGridView1.AutoGenerateColumns = False
    
            AddHandler DataGridView1.CellFormatting, AddressOf DataGridView1_CellFormatting
    
            DataGridView1.RowHeadersWidthSizeMode =
                DataGridViewRowHeadersWidthSizeMode.AutoSizeToFirstHeader
    
            DataGridView1.EnableHeadersVisualStyles = False
    
            Dim dt As DataTable = New DataTable With {.TableName = "MyTable"}
    
            dt.Columns.AddRange(New DataColumn() {
                                                     New DataColumn("ColumnA", GetType(String)),
                                                     New DataColumn("ColumnB", GetType(String)),
                                                     New DataColumn("ColumnC", GetType(String))
                                                 })
    
            dt.Rows.Add(New Object() {"A", "1", "1"})
            dt.Rows.Add(New Object() {"B", "2", "2"})
            dt.Rows.Add(New Object() {"C", "3", "3"})
            dt.Rows.Add(New Object() {"D", "4", "4"})
            dt.Rows.Add(New Object() {"E", "5", "5"})
            dt.Rows.Add(New Object() {"F", "6", "6"})
    
            DataGridView1.DataSource = dt
    
    
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
            If Not (DataGridView1.Rows(e.RowIndex).IsNewRow) Then
                DataGridView1.Rows(e.RowIndex).HeaderCell.Value = Convert.ToString(e.RowIndex + 1)
            End If
        End Sub
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) _
            Handles DataGridView1.CellContentClick
    
            If e.ColumnIndex < 0 Then
                Exit Sub
            End If
    
            Dim senderGrid = DirectCast(sender, DataGridView)
    
            If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.RowIndex >= 0 Then
                MessageBox.Show($"Row: {e.RowIndex + 1} -- Data: {String.Join(",", CType(DataGridView1.DataSource, DataTable).Rows(e.RowIndex).ItemArray)}")
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Saturday, December 22, 2018 7:43 PM
    Saturday, December 22, 2018 11:23 AM
    Moderator

All replies

  • Hi

    Will you please stop using random upper case in your posts!

    I do not understand your question.

    Do you want to show row headers but ignore mouse clicks on them?


    Regards Les, Livingston, Scotland

    Saturday, December 22, 2018 12:45 AM
  • As already recommended, please stop using upper case in your text. You need to be more detailed in your question also.

    Any ways is this what you are referring too, the green area being clicked is doing what the button does? If so we will need to see your code as per the example below clicking row header will not invoke the click event of the button.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, December 22, 2018 2:40 AM
    Moderator
  • Are you talking about the celection highlight appears across the entire row like this:

    If you change it to highlight only the selected cell as shown below then that row highlight stops.

    dgv.SelectionMode = DataGridViewSelectionMode.CellSelect

    Now clicking the row header does nothing as shown below:



    Saturday, December 22, 2018 10:18 AM
  • Hi

    Will you please stop using random upper case in your posts!

    I do not understand your question.

    Do you want to show row headers but ignore mouse clicks on them?


    Regards Les, Livingston, Scotland

    Sorry for my frequent mistakes leshay

    Do you want to show row headers but ignore mouse clicks on them? yes

    row headers I just want to show the row number.


    • Edited by ahmeddc Saturday, December 22, 2018 10:28 AM
    Saturday, December 22, 2018 10:26 AM
  • As already recommended, please stop using upper case in your text. You need to be more detailed in your question also.

    Any ways is this what you are referring too, the green area being clicked is doing what the button does? If so we will need to see your code as per the example below clicking row header will not invoke the click event of the button.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sorry for my frequent mistakes Karen

    row headers I just want to show the row number.

    The next code is to activate an event to press the button inside the cell.

     

    Private Sub DATAG_CUSTOM_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DATAG_CUSTOM.CellClick
    
                      Dim dgv As DataGridView = CType(sender, DataGridView)
                If dgv.Columns(e.ColumnIndex).Name = "ACTION" Then
    'CODE
     End If
        End Sub

    The other code to add the button to the Datagrid View cells

      Dim column As New DataGridViewButtonColumn()
                column.Tag = False
                column.Name = "ACTION"
                column.UseColumnTextForButtonValue = True
                column.Text = "ACTION"
                DATAG_CUSTOM.Columns.Insert((0), column)
                DATAG_CUSTOM.Columns(0).SortMode = False
    There is a problem problem that allows clicking on the column head and does the button event as well



    Saturday, December 22, 2018 10:43 AM
  • See if this is what you are looking for.

    Public Class Form1
        ''' <summary>
        ''' 1. Columns are created at design time
        ''' 2. The row header works regardless of bound or unbound
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DataGridView1.AutoGenerateColumns = False
    
            AddHandler DataGridView1.CellFormatting, AddressOf DataGridView1_CellFormatting
    
            DataGridView1.RowHeadersWidthSizeMode =
                DataGridViewRowHeadersWidthSizeMode.AutoSizeToFirstHeader
    
            DataGridView1.EnableHeadersVisualStyles = False
    
            Dim dt As DataTable = New DataTable With {.TableName = "MyTable"}
    
            dt.Columns.AddRange(New DataColumn() {
                                                     New DataColumn("ColumnA", GetType(String)),
                                                     New DataColumn("ColumnB", GetType(String)),
                                                     New DataColumn("ColumnC", GetType(String))
                                                 })
    
            dt.Rows.Add(New Object() {"A", "1", "1"})
            dt.Rows.Add(New Object() {"B", "2", "2"})
            dt.Rows.Add(New Object() {"C", "3", "3"})
            dt.Rows.Add(New Object() {"D", "4", "4"})
            dt.Rows.Add(New Object() {"E", "5", "5"})
            dt.Rows.Add(New Object() {"F", "6", "6"})
    
            DataGridView1.DataSource = dt
    
    
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
            If Not (DataGridView1.Rows(e.RowIndex).IsNewRow) Then
                DataGridView1.Rows(e.RowIndex).HeaderCell.Value = Convert.ToString(e.RowIndex + 1)
            End If
        End Sub
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) _
            Handles DataGridView1.CellContentClick
    
            If e.ColumnIndex < 0 Then
                Exit Sub
            End If
    
            Dim senderGrid = DirectCast(sender, DataGridView)
    
            If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.RowIndex >= 0 Then
                MessageBox.Show($"Row: {e.RowIndex + 1} -- Data: {String.Join(",", CType(DataGridView1.DataSource, DataTable).Rows(e.RowIndex).ItemArray)}")
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Saturday, December 22, 2018 7:43 PM
    Saturday, December 22, 2018 11:23 AM
    Moderator