DataGridView Edit Mode
Currently I have a .NET DataGridView
di开发者_开发问答splaying data from my source DataTable
dt
. I'd like it to be able to be edited so that when the user clicks into a cell and makes changes the changes will eventually get stored back into the DataTable
source. So far I've tried:
dataGridView1.DataSource = dt;
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
But this won't allow me to edit anything in the table. Any suggestions?
Thanks for the help.
I tried this out in a project and the following works like a charm:
public Form1()
{
InitializeComponent();
var dt = new DataTable();
dt.Columns.Add("Int", typeof(int));
dt.Columns.Add("Double", typeof(double));
var random = new Random();
for(var i = 0; i < 50; i++)
{
dt.Rows.Add(random.Next(), random.NextDouble());
}
dataGridView1.DataSource = dt;
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
}
Nothing special done to the DGV. I just dropped it on the form from the toolbox. I did not pre-create DGV columns.
The best I can suggest for your situation is to try to reproduce the problem outside of your main solution. Try to replicate what you're doing elsewhere and hopefully you'll find the one thing that's tripping you up. If you do create your own DGV columns before databinding, make sure they aren't set ReadOnly
either.
Do you want it to save to the DataTable (in memory on the machine the app is running on) , or the server?
If you mean the DataTable, and the DataGridView is bound to the table, it is happening automatically, you're just not seeing it. You can see this by calling GetChanges() on the datatable in the DataGridView's RowLeave event.
However, I'm guessing you REALLY mean you want to save it back tothe source database. if I'm guessing right, see this post: http://social.msdn.microsoft.com/Forums/en/vbide/thread/5f8741dc-5874-46e7-8665-6bda3674bafd
Edit - added based on comments
There are a few things to check and try.
First, just for fun, instead of EditOnEnter, set the EditMode to EditOnKeystroke.
Also check to ensure that the .Enabled property is set to true, and that it's not inside another control (such as a Panel) that has .Enabled set to false.
Also, ensure the ReadOnly property is set to false .
Also, CanFocus needs to be set to true.
Most of the things I listed are the default settings, so look for settings in the Properties pane that are bolded, which indicates that they are not the default property value.
In case you were using the designer there are three checkboxes which appear while configuring the Columns. Allow Add Allow Editing Allow Delete I presume you have checked none of them due to which the Grid is shown readonly to you.
Also do check if you have marked your columns as readonly.
精彩评论