On a DataGridView control, is it possible to ...

 

  1. Completely hide/disable the leftmost fixed column
  2. Disable cell/row/column selection mode – A mouse click on any cell/row/column does no selection

      If so, how

      Thanks,



Answer this question

On a DataGridView control, is it possible to ...

  • Sudhir

     

    I have solved (a bit of a kludge) my last remaining issue with hiding selectionMode on the DataGridView control ...

    Set the SelectionMode property to DataGridViewSelectionMode.CellSelect

    In the DataGridView_CellMouseDown event, I coded : If e.RowIndex >= 0 Then DataGridView.CurrentCell = DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)

    In the DataGridView_CellStateChanged event, I coded : DataGridView.CurrentCell.Selected = False

    So far it creates the desired appearance,

    Thanks


  • Pooneh

     

    Thanks for the suggestions, they're most helpful.  The setting of .RowHeaderIsVisible to false does the trick.  I'll have to experiment with the DefaultCellStyle.SelectionBackColor/SelectionForeColor settings and see how they work with my setup - which uses AlternatingRowsDefaultCellStyle (alternating rows set to different back colors).

    Seeing as I'm on a winning streak, I have two more DataGridView questions:

    a) How can I set the TopRow property (as was used in the old MSFlexGrid)   I'd like to be able to (from the application) scroll to the row of interest in the grid.

    b) How can I (from the application) change the height of all rows   I have tried DataGridView.RowTemplate.Height = DataGridView.ColumnHeadersHeight, but this does not change the row height.

    Thanks,

     

     

     


  • Martin Fierz

     

    Again, thanks for the helpful answers.  I solved the TopRow issue by using a combilation of .FirstDisplayedScrollingRowIndex and .DisplayedRowCount(False).  It is working nicely.

    As yet, I haven't tried setting rowHeight when adding rows on the fly, but for now, I start with 100 rows, and this code works just fine as the Column header height is dynamic based on the font size being used.

    DataGridView.RowTemplate.Height = DataGridView.ColumnHeadersHeight

    DataGridView.RowCount = 100

    For now, I have only one remaining problem (with the DataGridView control), and it relates to an earlier question - How to disable Cell/Row/Column selection (or at least fake it out so it looks like no selection takes place).  Is there a way to read a cell Back/Fore color   If I can do this, I can (hopefully) read the cell Back/Fore color and set the cell selection Back/Fore Color to match the current Back/Fore color before selection code executes.

    Thanks,

     

     


  • Andreas Jaeger

    A) Set the RowHeadersVisible property of the DataGridView control to false.

    B) What is it that you want to accomplish If you don't like the colors of the selected cells, you can set the foreground/background color of the selected cells to be the same as for the unselected cells:

    Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Me.DataGridView1.DefaultCellStyle.BackColor

    Me.DataGridView1.DefaultCellStyle.SelectionForeColor = Me.DataGridView1.DefaultCellStyle.ForeColor

    This will hide the selection from the user (the cells will still be selected, though).

    Best regards,
    Johan Stenberg



  • Gary Cawley

    A) You can use the DataGridView's FirstDisplayedScrollingRowIndex to set the index of the top row.

    B) Do you want to change the row height for rows that are already created, or do you want to change the row height for new rows

     The row/cell templates only affect the new rows that are created. I believe that there may be a problem in the order that the windows forms designer spits the property set:ers for the RowTemplate.Height and when the new row is actually created - if I set the rowtemplate's height, it will affect all new rows except the first row (it was already created by the time that the rowtemplate.height is set) I would encourage you to open an issue on http://lab.msdn.microsoft.com/productfeedback/Default.aspx. The issue look similar to http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx feedbackid=70a7066a-e235-4bd2-8e76-30366de59b1c, but not quite the same.

    Best regards,
    Johan Stenberg



  • On a DataGridView control, is it possible to ...