Home » Disable selection of rows in a datagridview

Disable selection of rows in a datagridview

Solutons:


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.

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.

Here’s an example:

public class MyDataGridView : DataGridView
{
    protected override void SetSelectedRowCore(int rowIndex, bool selected)
    {
        if (selected && WantRowSelection(rowIndex))
        {
            base.SetSelectedRowCore(rowIndex, selected);
        }
     }

     protected virtual void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
     {
         if (selected && WantRowSelection(rowIndex))
         {
            base.SetSelectedRowCore(rowIndex, selected);
          }
     }

     bool WantRowSelection(int rowIndex)
     {
        //return true if you want the row to be selectable, false otherwise
     }
}

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.

Private Sub dgvSomeDataGridView_SelectionChanged(sender As Object, e As System.EventArgs) Handles dgvSomeDataGridView.SelectionChanged
        dgvSomeDataGridView.ClearSelection()
End Sub

Related Solutions

How to “force” users to upgrade their browser?

Updated Answer - March 2013 Since this answer was posted on November 2012, Google has discontinued this plugin. While it might still work as of today (March, 2014) there is no guarantee of it working in the future. As of January 2014, support for Chrome Frame...

Splitting credit card number fields into four different inputs

I would generally always opt for the simplest solution. In this case, one single field for the user to type into. With split fields, such as the 4-box one you propose it adds in an extra cognitive load to the user. "Do I need to manually jump to each field?"...

Is coffee a good excuse for a slow application start-up time?

To me, the basic logic is this: It's better to have a fast app than a slow app. While there are many studies that show that faster applications provide better UX, it seems pretty axiomatic to me. I mean, generally in life if we want something done, then we...

Why are terminal consoles still used?

From a UX perspective, terminal consoles have a few key advantages over GUI's. These advantages are rarely relevant to end-users, which is why CLI's are used almost exclusively by technical users and "power users" these days. Things done in a terminal are...

Should you always minimize cognitive load?

No. It is not always appropriate to minimize cognitive load. Minimizing cognitive load is not the goal of usability, human factors, UX, or the user centered design process in general. It is about "good design", and good design is not always the simple design....

How do you give your user options without overloading them?

While Charles's Answer shows some great UIs, I wanted to add the UX that Directory Opus uses for the same action, which I find very good as well. Simple on the surface with advanced options tucked away after a click. . Clicking on the Dropdowns results in these...

Why is it important to focus on user needs as opposed to requests?

Users are bad at asking for what they need and great at asking for what they want. Anecdotal evidence from my own recent experience: We have a department that asked for a button that would generate a PDF report about some data. A few months later they asked for...

Is Markdown Friendly Enough for Non-Technical Users?

I know I'm coming to this thread rather late, but I actually have run usability tests comparing a WYSIWYG editor (iWeb) to a non-WYSIWYG editor based mostly on markdown. Here's what I've found that users struggle with when using markdown: Tags that require...

Why do numpads on keyboards and phones have reversed layouts?

There's this humongous article called Keyboard Trivia that has collected many of the theories and stories. The summary of facts: Touch-tone key pad was designed to mimic the rotary dial with the "1" on top and the 7-8-9 on the bottom, and AT&T conducted...

Why are radio buttons circles?

Square was easy The earliest appearance of circular radio buttons that I can find is in Apple Macintosh System 4 (1987). Prior to that Mac OS used squares with beveled corners, which was probably just computationally easier to draw and better-looking on...

Why might right aligned field labels be better?

Luke Wroblewski wrote about this in Top, Right or Left Aligned Form Labels (April, 2007). In it, he references eyetracking data from an article by Matteo Penzo called Label Placement in Forms (July, 2006). Matteo drew several conclusions from this study,...

Which way should be “on” for a switch?

It appears to be dependent on country or region, as Wikpedia states in the article Light Switch: Up or down The direction which represents "on" also varies by country. In the USA and Canada and Mexico and the rest of North America, it is usual for the "on"...

Who needs an external on/off Wi-Fi button?

Security A hardware radio switch has security benefits to it in that when it is off, you know for a fact that nothing is connected to your laptop wirelessly. The radio switch usually turns off all wireless communication, including Bluetooth and 3G. I have heard...

How big a deal is blocking copy-pasting on a website?

Ask your client what he's actually trying to accomplish. Copy-paste restrictions are about as effective as a toddler gate preventing access to an unmonitored garden in a remote area. Perhaps you could ask the client where he saw copy-paste restrictions being...

What’s the point of a delayed popup on a webpage?

It is about increasing the chance of the user having a look at the popup. Task-completion mode Users in task-completion mode are eager to fulfil their goal (eg, "Is the product/information/answer I'm after on this page?"). As such, they simply ignore anything...

Is the folder structure dying?

Rumors of its death are greatly exaggerated... Classic hierarchical folder views aren't dying. But they are being complemented by other ways of viewing and interacting with files. The key trend here is the decoupling of views from the underlying file system....