Question

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

Était-ce utile?

La solution

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.

Autres conseils

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.

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)!

Best solution for me that disables everything except scrolling and resizing cells (which imho is best for "read only tables"):

// disable editing
this.dgTabOptions.ReadOnly = true;

// disable selection        
private void myGrid_SelectionChanged(object sender, EventArgs e)
{
    dgTabOptions.ClearSelection();
}

//disable sorting
private void dgTabOptions_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
    e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top