Prevent DataGridView selecting a row - is it possible?
-
Hi all, I have a sorted, non-editable DataGridView which contains some cells in each row that are traffic-lighted (ie: they have a different b/g colour depending on value) but when I populate the control the row that was the first one (before sorting) is always highlighted thus wiping out my custom coloured cells. I have tried every combo of colours and highlighting options to try and stop the DGV doing this but can't find a way. Ideally I just want to suppress altogether the DGV control selecting a row when it populates - does anyone know how I can go about this? This is driving me mad so I hope someone can help! TIA... Mike
-
Hi all, I have a sorted, non-editable DataGridView which contains some cells in each row that are traffic-lighted (ie: they have a different b/g colour depending on value) but when I populate the control the row that was the first one (before sorting) is always highlighted thus wiping out my custom coloured cells. I have tried every combo of colours and highlighting options to try and stop the DGV doing this but can't find a way. Ideally I just want to suppress altogether the DGV control selecting a row when it populates - does anyone know how I can go about this? This is driving me mad so I hope someone can help! TIA... Mike
There should be an option where readonly means "for use as display only no selection please". :) I got this effect in a recent project by deselecting everything just after the DataGridView was populated. The Deslection:
Private Sub DeselectGridRows(ByVal dgv As DataGridView) Dim dgr As DataGridViewRow For Each dgr In dgv.SelectedRows dgr.Selected = False Next End Sub
Deselecting called form within the forms load event:
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Windows.Forms.Cursor.Current = Cursors.WaitCursor Me.SuspendLayout() PopulateDataGridView(dgv) DeselectGridRows(dgv) Me.ResumeLayout() Windows.Forms.Cursor.Current = Cursors.Default End If End Sub
-
There should be an option where readonly means "for use as display only no selection please". :) I got this effect in a recent project by deselecting everything just after the DataGridView was populated. The Deslection:
Private Sub DeselectGridRows(ByVal dgv As DataGridView) Dim dgr As DataGridViewRow For Each dgr In dgv.SelectedRows dgr.Selected = False Next End Sub
Deselecting called form within the forms load event:
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Windows.Forms.Cursor.Current = Cursors.WaitCursor Me.SuspendLayout() PopulateDataGridView(dgv) DeselectGridRows(dgv) Me.ResumeLayout() Windows.Forms.Cursor.Current = Cursors.Default End If End Sub
Thanks Leah.... actually I'm not sure why I even posted the question as I realised soon after all you need to do is say datagridview1.SelectedRows(0).Selected = false and that takes care of it. What I want to do now is only highlight a row when the user clicks on one but rather than using a background colour (which obscures my cell traffic-lighting) I want to leave the B/G and F/G colours as they are and instead draw a 2-pixel red border around the entire row - and I'm thinking the only way to do that is to override one of the Paint methods... anyone know of a better/simpler way?
-
Thanks Leah.... actually I'm not sure why I even posted the question as I realised soon after all you need to do is say datagridview1.SelectedRows(0).Selected = false and that takes care of it. What I want to do now is only highlight a row when the user clicks on one but rather than using a background colour (which obscures my cell traffic-lighting) I want to leave the B/G and F/G colours as they are and instead draw a 2-pixel red border around the entire row - and I'm thinking the only way to do that is to override one of the Paint methods... anyone know of a better/simpler way?
This is proably an old thread now and past it's sell by date, but I did put an article in about drawing cell colours that may or may not help. Search the articles for 'Couluring and centering cells in datagridview' - Yes it contains a typo that got uploaded to the web!
David Loring !! Keep Music Live !!