How to Update, Delete and Cancel a row in gridview asp.net? - c#

I have one GridView and one "Add" Button. If I click the "Add" button, one new row will be added to the GridView at the sametime I want to show "Update and Cancel" buttons in CommandField Column.
When I click the Update Button after the Insertion of the New Row, I want to Show the Delete Button in the Command Field Column.
Once again When I click the Delete Button, the Corresponding row will have to be deleted.
So far I have done all the four things(update, cancel, edit and delete) at the same. But now I dont want Edit.
The problem is when I include "ShowDeleteButton = True" in the command field, the "update and cancel" buttons are not displaying when I click the "Add" button.
The "update cancel" buttons are displaying only when I include "ShowEditButton = true". But I dont want to Edit the Column. Any one please help me.
Here I want only to do Update, Cancel and Delete. How to do? I dont want coding, I have written all the coding parts in Gridview_Rowupdating and RowDeleting Event.
Here is my GridView's Command Field Column.

have is good article to explain step by step
http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx
http://www.codeproject.com/KB/aspnet/InsertingWithGridView.aspx
http://www.aspsnippets.com/Articles/ASP.Net-GridView--Insert-Edit-Update-and-Delete-the-ADO.NET-way.aspx

Related

Is it possible to disable first row from clicking on it?

I got event CellMouseClick that is editing the selected Row, but I have filter also so , when I click on the first row new dialog is showing with the edit datagrid.Is it possible to disable only first row click in DataGridView,tried that but not working:
advancedDataGridView.Rows[0].ReadOnly=true;

How to update a single column of a selected row in gridview using a button?

I have a Select button at the end of each row in gridview. I also have a separate button that I want to use to update one of the column of the selected row. This column contains a checkbox, I want to put a check in it when I click the button.
I do not want to use an Update button inside the gridview because I do not want the user to be able to change any other column.
Can anyone help? I have been researching this for a while now.
You've got a lot of option regarding your problem..
You could use Gridview's RowCreated or RowDataBound events or even on the buttons onclick event if that button is on the same row of the gridview(just get the sender etc//based on your logic).
here are some code snippet
If
e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender,
"Select$" & e.Row.RowIndex.ToString))
End If

You must finish editing the current row gridview validation?

I am new to the wonderful world of asp.net gridviews. As of now, my gridview would have several rows each with a edit and update button. When I click edit, the editable cells turn into textboxes and can be successfully updated. However, if I am in edit mode in one row, and I click an update button in another row, an error will fire.
So my question is, how could I validate that the current row finished editing? Ive been searching google and I understand how to make field templates, assign a control, and can validate a certain cell. But I am not sure of a way to force someone to finish editing before causing that error. Thanks in advance!
From comments:
Why do you provide an update button when the row is not in edit mode?
You could set AutoGenerateEditButton to true. Then update + cancel buttons are shown when edit was clicked.

How to validate only few controls ? Anyway to group it on click of a button?

I have a table design, used by recruiter to upload profiles. Each row contains a txtname,txtContactNo,txtEmailID and a submit link button at the end of each row(contained in last column of the table). There are 5 such rows. I want all the txt boxes in row 1 to be filled before clicking on btnSubmit1 (and same with all other row on click of their corresponding submit button). I have placed requiredFieldValidator to all the controls of all the rows. If i click on submit button of first row, it giving error on all the controls of all the rows. I want that my first submit button should check only those controls that are in first row, instead of all the controls.
Please Help !
The Textcontrols and the Button causing the Submit have a Property called ValidationGroup.
So set the same ValidationGroup for the Textboxes in the first row and button 1
Set the ValidationGroup property on the validators and submit button, by row.
E.g. all the validators and btnSubmit in row 1, ValidationGroup="valgroup1",
all the validators and btnSubmit in row 2, ValidationGroup="valgroup2", etc.

DataGridView UserDeletingRow Not Firing

Consider 2 DataGridViews both with SelectionMode = RowHeaderSelect. In grid1 I have 2 columns; a TextBox and a CheckBox. In grid2 I have 2 columns; a ComboBox and a TextBox.
When I click on the RowHeader, the focus goes to the first cell, not the RowHeaderCell. When I press the DEL button, the contents of the cell is deleted (TextBox content is cleared and ComboBox is set to 1st item).
This way the UserDeletingRow event never fires as the DataGridView FAQ says "Users can delete rows only when the current cell is not in edit mode, the AllowUserToDeleteRows property is set to true".
As a workaround I implemented this code in RowHeaderMouseClick
grid.CurrentCell = grid.Rows[rowIndex].Cells[columnName];
grid.Rows[rowIndex].Selected = true;
In case of grid1 I pass the name of the CheckBox column and the UserDeletingRow event fires when I press DEL. However for grid2 passing the name of the ComboBox column doesn't fire the event.
I have tried the following:
1- Select RowHeaderCell -> Exception because it cannot be selected.
2- grid.EndEdit() -> No effect.
3- Change the SelectionMode to FullRowSelect then back to RowHeaderSelect -> Cell is set to edit mode.
My Question:
I need the UserDeletingRow to fire regardless of the column of the grid? Or a workaround to select the HeaderCell if possible?
Edit (Short form of the question):
Clicking the RowHeader selects the row and puts the first cell in edit mode, if applicable, this way clicking the DEL button deletes the cell contents and doesn't fire the UserDeletingRow event. As a workaround I select a cell, if available, that cannot be put in edit mode, e.g. CheckBoxColumn.
Is this the default behavior of clicking the RowHeader? If yes, can you show me a workaround? If no can you point me to the cause of this behavior?
Edit 2:
I have found that setting the EditMode to EditOnEnter makes clicking RowHeader put the first cell in edit mode. I will try to overcome this but any help is appreciated.
It's a shame that I can't leave a comment yet (because of the rep stuff and so on!)
Also you weren't clear on what exactly you want to do.
Clicking the RowHeader of the row results in the full row getting selected and pressing the Del button the row gets deleted and the event is fired!
If you want to be able to delete a row without selecting row header, consider using "Full Row Select" instead of "Row Header Select".
I have found the answer in http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/cab3c9eb-4c86-475e-8cbd-dee6b235765a. It is a workaround to change the EditMode to a mode other than EditOnEnter when the RowHeader is clicked then changing it back to EditOnEnter.
I want to thank #M2X because his comment was helpful.

Categories