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();
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.
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).
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.
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.
Ref:
DataGridView read only cells
this.dgridvwMain.Rows[index].Cells["colName"].ReadOnly = true;
add this to your code:
dataGridView1.ReadOnly = true;
or you can change Read Only property in designer mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With