Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable DataGridView except the scroll

How I can configure a datagridview so that the user can only move through the rows and use the scroll, and nothing else... If I disable the grid not allow me to use the scroll

like image 498
mggSoft Avatar asked Oct 05 '12 16:10

mggSoft


3 Answers

Set your datagridview to read-only, this will disable any edits.

dataGridView1.ReadOnly = true;

And inside your handlers, do :

void dataGridView1_DoubleClick(object sender, EventArgs e)
{
     if (dataGridView1.ReadOnly == true)
          return;

     // .. whatever code you have in your handler...
}

Even if the user double-clicks on the grid, nothing will happen.

like image 136
T. Fabre Avatar answered Oct 03 '22 04:10

T. Fabre


As discussed in OP comments:

dataGridView.ReadOnly = true;

Inside any DataGridView events you are handling, check the ReadOnly property and do not do anything inside the event if true.

I looked at another option of iterating through rows and columns and disabling each of them, but Enabled is not a property of the row or column object. Iterating through a large number of items would be slow, anyway.

like image 28
Michael Sallmen Avatar answered Oct 03 '22 06:10

Michael Sallmen


T. Fabre answer didn't work for me. In my case, I have buttons and editable checkboxes for each row of my datagrid, so they won't be deactivated even if the DataGrid is in ReadOnly. However, what did work for me (without disabling the scroll) is disabling each row like in this example:

<Style TargetType="{x:Type DataGridRow}" x:Key="MyDataGridRowStyle">
    <Style.Setters>
        <Setter Property="IsEnabled" Value="False"/>
    </Style.Setters>
</Style>

And then in the DataGrid:

<DataGrid ... RowStyle="{StaticResource MyDataGridRowStyle}">

Hope that helps (sorry if only posted the XAML solution)!

like image 38
François Boivin Avatar answered Oct 03 '22 06:10

François Boivin