Disable DataGridView except scrolling

How can I customize the datagridview so that the user can only navigate the rows and use the scroll, and nothing more ... If I turn off the grid, do not allow scrolling

+4
source share
3 answers

Install datagridview read-only, this will disable any changes.

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.

+4
source

As discussed in the comments on the OP:

 dataGridView.ReadOnly = true; 

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

I considered another possibility of repeating rows and columns and disabling each of them, but Enabled is not a property of a row or column object. In any case, iterating through a large number of objects will be slow.

+1
source

T. Answer Fabre did not help me. In my case, I have buttons and editable flags for each row of my datagrid, so they will not be deactivated even if the DataGrid is in ReadOnly. However, what worked for me (without disabling scrolling) disables each line, as 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 this helps (sorry if only posted a XAML solution)!

0
source

Source: https://habr.com/ru/post/1438047/


All Articles