Keep row in edit mode after inserting in GridView - c#

I have a Gridview and I have defined the respective itemtemplates and edittemplates for the rows and controls. I have different row events to bind, insert, update, delete the data from the grid. As per the requirement I am struggling to find a way to keep the new Row in edit state once it has been inserted in the grid. Presently there is an overhead of clicking the edit button after the row is inserted. How do I accomplish this?

I found a resolution. I could simply call the RowEdit Event explicitly by : gridView_RowEditing(sender, new GridViewEditEventArgs(gridView.Rows.Count-1));

Related

Adding a checkbox to a databound DataGridView

I'm working with DataGridView in C#. I fill it with a table from database using BindingSource. After filling the DataGridView I need to add a row of checkboxes that should be first in the table, to enable data filtering in future.
I mean: for example after clicking "Load" button I get DataGridView filled with 4 columns of data and the first row should consists of checkboxes.
I confused. I use
DataGridViewCheckBoxCell checkCell = new DataGridViewCheckBoxCell();
SourceDGV.Rows.Add(checkCell);
I mean here:
But after clicking load button it says You can't add rows to databounded DataGridView
Sorry if ecxeption translated wrong, I translated it from Ukrainian.
Any help appreciated.
Am afraid, there is no concept of whole rows to be of some type(checkbox in your case) in DataGridView. It is other way around. You can have whole column to be of checkbox type. You'd use DataGridViewCheckBoxColumn for that.
Showing checkboxes in whole row even makes no sense for me: Assume you have data bounded Person object to the DataGridView. So there will be columns like Name, Age, Address. Now, on what basis you show checkbox in the row? How can a Name be converted to a bool(which is what checkbox needs as a value)?
One workaround I can think of would be, handle custom painting, If First row paint checkboxes over there and manually handle their click events. But am pretty sure it's not gonna be easy.

DataGrid problems. Using DataSet.Merge with "Edit Template"

I am trying to debug an issue with a WinForm application.
The application uses a DataGridView bound to a DataSet.
The user can select a row and click "Edit" and an Edit Form appears so the user can edit the row and save his changes.
If the DataGridView is sorted by one or more columns and the user edits a row more than once and changes that rows position in the DataGrid (by changing one of the sorted columns TWICE), the DataGridView gets out of sync and I get Exception errors.
Here is how the code is written:
When the user clicks a row and hits edit (the user may only select one row), that row is passed to the EditFom. The edit Form creates an empty DataSet and loads that Row. When the user is done, the calling form uses DataSet.Merge to merge those changes back into its DataSet.
Here are a couple of scenarios that tell me it is womething to do with the "merge" and sorting:
(Note, I never change the actual "sorting", I am talking about changing the value of one of the sorted by columns in one of the rows.)
If the user edits a row directly in the Grid and changes the value of one of the columns being used to sort, then changes it again, etc. The row moves around the Grid, like it should and when the user clicks save, everything saves fine.
If the user edits a row (directly in the Grid OR via the "template") and doesnt change any of the "sort" columns, just makes an edit (therby setting the "State" of the row to Modified, right...), THEN the user edits the row using the Template (which goes through the seperate DataSet and merges the data back when done), and changes one of the columns being sorted (when done, the grid should resort and the row should "move"), the row doesn't get REPAINTED, it stays in the same position in the grid that it was in before the template edit. (Although, the data saves fine and you can refresh the grid and it sorts properly.) Evidently, the DataGrid isn't being notified that the RowChanged in this scenario (or it is, but it isn't doing anything about it).
If the user edits a row directly in the Grid, change a sort column (the row moves), then edits the row using the template and changes that column again, we get the same results as 2, above. The data saves fine, but when the user leaves the edit template, the grid doesn't resort.
(This is the one that causes the Exception error.)
If the user edits a row using the template and changes a sort column and leaves the template, the datagrid resorts and the row moves properly. Then, if the user edits the SAME row using the Template Edit and changes the sort column AGAIN. When the user leaves the edit template, the datagrid doesn't resort and when the user saves the data and the grid refreshes, we get an error:
System.InvalidOperationException: DataTable internal index is corrupted: '5'.
And sometimes, we get an error complaining the primary key is missing.
Any advice on how to "debug" this? I feel like it has something to do with the "internediate" dataset the edit template uses and the "Merge". It is like the DataGrid datasource is getting out of sync with the DataGrid Rows.
I should have done some Google'ing before posting this. It seems to be a common error and I found a HUGE post about this on the MS site. Unfortunately, I haev tried most of their suggestions, but most likely I will find my answer there. I just wanted to make a note of this, in case anyone is interested:
http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/18544cd3-1083-45fe-b9e7-bb34482b68dd

How can I add one row to a DataGridView?

I work with a WinForms app.
I want to add one row to a DataGridView that this row must enter to database.
What event handler must I use to do this?
Is your DataGridView bound to that database? If so, you should be adding and deleting rows in that data source, not in the grid control itself. Try calling the AddNew method on the binding source.
If you're using a standalone DataGridView that isn't bound to a database, then you can call one of the many overloads of the Add method of the grid control's Rows collection.
This might help you out to add a row to the DataGridView:
http://msdn.microsoft.com/de-de/library/system.windows.forms.datagridview%28v=vs.80%29.aspx
If the DataGridView isn't bound to the database, try to bound it or write the data to the database programmatically.

Inserting new row in gridview using an external Add button and a list as data source

I have a grid with three columns, two of which contain drop-downs, all of them getting filled from a web service result set. Now I want to allow the functionality of adding a new record in the grid by clicking an Add button present outside the gridview.
Whenever the user clicks the Add button, a new record should be created in the grid with value list filled in the drop-downs with the available options.
What is the best possible way to achieve this considering extensibility in mind.
Thanks a lot!
P.S. The data source set for the grid is a list.
Add a blank item to the list, and rebind with this new list and dummy item. That's typically one way to do it, or store the insert form in the footer of the columns. I've used that approach.

update data grid view automatically

I have two windows forms. The first form shows list of records from sql. When you click some cell of a record in DataGridView it shows the second form. In the second form you can edit and update the record. I want DataGridView to be
updated automatically when user close second form. What should I do?
Are you using data bindings, or do you manually fill the grid ? With data bindings its very easy... if the data is contained in a DataTable and you change it somewhere, the change will be reflected automatically in the DataGridView. It also works with objects that implement INotifyPropertyChanged, and lists that implement IBindingList.
If you're not using bindings, you can :
locate the cell that contains the edited value, and update it manually, OR
refill the entire grid
Fill the grid with data from the database again?

Categories