<p>I want to disable the selection of certain rows in a datagridview.</p>

<p>It must be possible to remove the select property for one or more datagridview rows in a datagridview shown in a winform. The goal is that the user can't select certain rows. (depending on a condition)</p>

<p>Thankx,</p>

[{'answer_id': 71665, 'author': 'szevvy', 'author_id': 11437, 'author_profile': 'https://Stackoverflow.com/users/11437', 'pm_score': 5, 'selected': True, 'text': "<p>If SelectionMode is FullRowSelect, then you'll need to override SetSelectedRowCore for that DataGridView, and not call the base SetSelectedRowCore for rows you don't want selected.</p>\n\n<p>If SelectionMode is not FullRowSelect, you'll want to additionally override SetSelectedCellCore (and not call the base SetSelectedCellCore for rows you don't want selected), as SetSelectedRowCore will only kick in if you click the row header and not an individual cell.</p>\n\n<p>Here's an example:</p>\n\n<pre><code>public class MyDataGridView : DataGridView\n{\n    protected override void SetSelectedRowCore(int rowIndex, bool selected)\n    {\n        if (selected &amp;&amp; WantRowSelection(rowIndex))\n        {\n            base.SetSelectedRowCore(rowIndex, selected);\n        }\n     }\n\n     protected virtual void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)\n     {\n         if (selected &amp;&amp; WantRowSelection(rowIndex))\n         {\n            base.SetSelectedRowCore(rowIndex, selected);\n          }\n     }\n\n     bool WantRowSelection(int rowIndex)\n     {\n        //return true if you want the row to be selectable, false otherwise\n     }\n}\n</code></pre>\n\n<p>If you're using WinForms, crack open your designer.cs for the relevant form, and change the declaration of your DataGridView instance to use this new class instead of DataGridView, and also replace the this.blahblahblah = new System.Windows.Forms.DataGridView() to point to the new class.</p>\n"}, {'answer_id': 15474300, 'author': 'Asad Naeem', 'author_id': 390163, 'author_profile': 'https://Stackoverflow.com/users/390163', 'pm_score': -1, 'selected': False, 'text': '<pre><code>Private Sub dgvSomeDataGridView_SelectionChanged(sender As Object, e As System.EventArgs) Handles dgvSomeDataGridView.SelectionChanged\n        dgvSomeDataGridView.ClearSelection()\nEnd Sub\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/71423', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4392/']