How to update and delete records in datagridview?
Hello, i want to update and delete all the records dynamically in datagridview on keyboard's enter button click. Without using any textboxs or buttons on window form. I'm using ms access database. So how can I do this... Help me.
James AxsomPosted Jun 25, 2018, 12:56 AM
The following is an opinion:
Data Grid Views. They are such a pain. Why? Because as programmers we place high expectations on these grid controls and they don’t deliver. But actually, the data grid controls do deliver! We programmers are abusing them. We programmers are forcing the data grid view into unsuitable functionality and contexts which Microsoft didn’t implement. As a result, we programmers code-hack the control in order to force it do what we think it should be doing.
In my opinion, this hack is the closest you may get to achieving what you are after.
Other programmers may have discovered other hacks like creating a custom data grid view by inheriting DataGridView class and overriding sacred protected virtual methods. Others come up with hacks and manipulate the KeyPress event. All in all, in the end in my opinion nobody really gets what they want because we want something that Data Grid View just can’t deliver.
Here is what you can expect by suppressing the KeyDown event:
You put the focus on a data row. (Remember a data row not inside a cell editing it.) You press the enter button. The KeyDown event is suppressed and whatever code you want is executed.
If you put the focus inside the cell and enter a new value or edit an existing value, then you press the enter key, the KeyDown event will not fire. Your code is ignored and the focus moves to the next row.
The closest you can get, is only press enter when you get to the last cell and the focus is back on the row. For example, the focus is on the first cell of a selected row. You type a value and the cell accepts edit change. You tab to the next cell and change it. You tab and change to the last cell. Once you typed the last character in the last cell, the focus has to leave the cell and come back to row. Because if you press enter while the cell has focus, your code is ignored and you navigate to the next row (which how the data grid view is designed.) You may have to click the row which will put the focus on the row, then you press enter, and then your code executes, or changes are saved.
I wish I could give you a silver bullet solution. But really the data grid view excels at one thing, making data look nice on a form. You can edit data and add rows using the data grid view, but you do so per Microsoft’s design. Microsoft created the enter key to navigate rows as opposed to saving data. No matter how much we want it do something different it just not going to want we want.
I hope you find someone who may prove me wrong or find a suitable alternative.
Bharathi RajaPosted Jun 21, 2018, 12:51 AM