Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling editing in DataGridView

I'm using Visual Studio 2012. I want to disable the editing on the DataGridView, it seems to work when I used this code:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.ReadOnly = true;
}

But when I get back on the menu form then go back to the form where the DataGridView is, it can now be edit. I only define

dataGridView1.ReadOnly = true;

to this form. And I don't know whats the problem. Can someone help? Thanks.

Here's my code on the button going to the menu

Menu menu = new Menu();
this.Hide();
menu.ShowDialog();

and my button going back to the DataGrid:

FrmList frmlist = new FrmList();
frmlist.Show();
this.Hide();
like image 311
Jonald Samilo Avatar asked Aug 09 '16 06:08

Jonald Samilo


People also ask

How to make DataGridView not editable?

DataGridView control has a property named ReadOnly that determins if the data in the cell can be changed. The ReadOnly property can be set not only for particular cells but also for entire rows and column. You can achieve this behavior by setting the DataGridViewRow. ReadOnly or DataGridViewColumn.

How to disable editing in DataGridView vb net?

You should set the readonly property of your DataGridView to true, then it will not be editable while users can copy the cells. Sorry, I meant to write that I have it set to readonly property = true (edited the original post).

How do you make an editable grid in WPF?

You can set the properties CanUserAddRows to true to allow user to add rows. DataGrid is editable by default, where each column has an edit control which lets you edit its value. By default the DataGrid automatically generates columns for every property in your Model, so you don't even have to define it's columns.


3 Answers

Why don't you try setting the ReadOnly property to True in the Properties window of the DataGridView?

Edit:

Double click on the form and in the design window, select the DataGridView and open the Properties tab. Scroll down through the properties and you will see the ReadOnly option. Change it's value to True.

You were setting the ReadOnly property in the CellContentClick event which will be executed only when user clicks on the grid cells. So, when you create a new object of the form like this,

FrmList frmlist = new FrmList();

it will just create a new instance of the form with the Properties set in the designer. Since the ReadOnly property is set to false by default and the code you wrote to set it to true has not executed, the DataGridView will be editable.

like image 174
Romy Mathews Avatar answered Oct 23 '22 18:10

Romy Mathews


Ref:

DataGridView read only cells

this.dgridvwMain.Rows[index].Cells["colName"].ReadOnly = true;
like image 1
kattav mauk Avatar answered Oct 23 '22 19:10

kattav mauk


add this to your code:

dataGridView1.ReadOnly = true;

or you can change Read Only property in designer mode.

like image 1
Daly Hachicha Avatar answered Oct 23 '22 17:10

Daly Hachicha