Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid editing
 »  Home  »  Web Development  »  ASP.NET  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid editing
Using ASP.NET DataGrid Web Server Control
by Misha Zhutov | Published  11/03/2005 | Web Development ASP.NET | Rating:
DataGrid editing

As we already know, a TemplateColumn has two control sets for viewing and editing mode. First of all, we need to add an Edit button to DataGrid.
A column with Edit link can be added either as the first column or as last column in the DataGrid.
I prefer to add Edit link as the  DataGrid’s first column. This is because, sometimes, DataTable can have many columns and a user needs to scroll horizontally.
Let’s take a look at the HTML code in the ASPX page that creates the DataGrid column with links:

<asp:TemplateColumn>
  <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
  <ItemTemplate>
    <asp:LinkButton id="LinkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
  </ItemTemplate>
  <EditItemTemplate>
    <asp:LinkButton id="LinkUpdate" runat="server"
CommandName="Update" Text="Update"></asp:LinkButton>
    <asp:LinkButton id="LinkCancel" runat="server"
CausesValidation=False CommandName="Cancel" Text="Cancel"></asp:LinkButton>
  </EditItemTemplate>
</asp:TemplateColumn>

So, we have Edit link visible in view mode and Update, Cancel links visible in edit mode. User can edit any fields by typing in TextBoxes, changing selected value in DropDownLists or changing value in CheckBoxes.
After that, user can hit Update link to accept changes to database or Cancel link to return to view mode without changing anything.
Developer must also add code to DataGrid.EditCommand Event handler such way:

Private Sub ProductsDataGrid_EditCommand(ByVal source As Object_
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles ProductsDataGrid.EditCommand
  ProductsDataGrid.EditItemIndex = e.Item.ItemIndex
  BindDataGrid()
End Sub
Generated using PrettyCode.Encoder

Validating user inputs

In some cases the user can enter incorrect values during editing. Validators prevent the user from doing that.
You can use as many validators as you want in different columns. But how to output errors in one single place? The answer is a ValidationSummary contol which displays messages from all validators in a single place. I used RequiredFieldValidator for ProductName and QuantityPerUnit fields and CompareValidator for UnitPrice, UnitsInStock, ReorderLevel field which checked that user input a correct type of a value.
Here is a screenshot of the “validation” on web form:

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 12/11/2005)
Rating
realy usefull,
 
Comment #2  (Posted by Johana on 12/28/2005)
Rating
El articulo es interante pero muy engorroso, mi pregunta es como podria, hacer que el segundo combo carge con una funcion que reciba 2 valores, el primero q sea resultado del codigo del primer combo y el segundo valor de un campo del grid, eso es lo q me gustaria hacer pero sin tantas clases no se si puedas resolver mi duda.
Gracias
 
Comment #3  (Posted by Misha Zhutov on 12/29/2005)
Rating
My apologies - I can't read Spanish
 
Comment #4  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #5  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #6  (Posted by David Wilburn on 01/08/2006)
Rating
Very good article! For a person such as myself that has very little ASP.Net experience, it was very informative.

Much appreciated.
 
Comment #7  (Posted by an unknown user on 03/14/2006)
Rating
hi,
this is very good article.
 
Comment #8  (Posted by AFSUR HUSSAIN on 03/25/2006)
Rating
ya really its fine friends..,


 
Comment #9  (Posted by an unknown user on 04/18/2006)
Rating
good
 
Comment #10  (Posted by an unknown user on 05/19/2006)
Rating
I want to know data update in gtagrid.I can lern lot of thing in your article. Thanks lot for your help.
 
Comment #11  (Posted by an unknown user on 08/10/2006)
Rating
Great but:"The source code (both VB.NET and C#) is attached."...i cannot see it
 
Comment #12  (Posted by Misha Zhutov on 08/11/2006)
Rating
The attachment is on the last article's page, here is direct link on it: http://www.devcity.net/Download.aspx?FileType=Attachment&FileName=WebDataGridExamples.zip&File=3532cd2b-353d-4277-a05e-e6f20d4a268c.zip
 
Comment #13  (Posted by an unknown user on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #14  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #15  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #16  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #17  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Sponsored Links