ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
ASP Dot Net Tutorial - Index
ASP Dot Net Tutorial - Microsofts ASP.NET
ASP Dot Net Tutorial - Begining Web Forms
ASP Dot Net Tutorial - User Controls
ASP Dot Net Tutorial - ADO .NET
ASP Dot Net Tutorial - DataBinding to WebControls
ASP Dot Net Tutorial - DataList Controls
ASP Dot Net Tutorial - DataGrid Control
ASP Dot Net Tutorial - Advance Practical Examples on DataGrid
ASP Dot Net Tutorial - Working with DataSets
ASP Dot Net Tutorial - Working with XML
ASP Dot Net Tutorial - Forms Based Authentication
ASP Dot Net Tutorial - Windows Based Authentication
     

 
Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > Advance Practical Examples on DataGrid
 

ASP.NET Tutorial - Advance Practical Examples on DataGrid

                                                                                                                 Page 5 of 5
 

Edit column in a Datagrid Control

In Example 46 ExpertDataGridEditCommandColumn.aspx there is one problem that there is no Update command execution. This example is an answer to that problem. Therefore the values here are updating using update command in the subroutine.

 

Example 50 ExpertDataGridEditEmployees.aspx

<%@ Import Namespace="System.Data.SqlClient" %>

 <Script Runat="Server">

 Dim conNorthwind As SqlConnection

Dim cmdSql As SqlCommand

Dim strSql As String

 Sub Page_Load

conNorthwind=New SqlConnection( "Server=localhost; UID=sa; PWD=secret;Database=Northwind" )

If Not IsPostBack Then

BindDataGrid

End If

End Sub

 Sub BindDataGrid

cmdSql = New SqlCommand( "Select * From Employees", conNorthwind )

conNorthwind.Open()

dgrdEmployees.DataSource = cmdSql.ExecuteReader() dgrdEmployees.DataBind()

conNorthwind.Close()

End Sub

 Sub dgrdEmployees_EditCommand( s As Object, e As DataGridCommandEventArgs )

dgrdEmployees.EditItemIndex = e.Item.ItemIndex

BindDataGrid

End Sub

 Sub dgrdEmployees_UpdateCommand( s As Object, e As DataGridCommandEventArgs )

Dim intEmployeeID As Integer

Dim txtFirstName As TextBox

Dim strFirstName As String

intEmployeeID = dgrdEmployees.DataKeys( e.Item.ItemIndex )

txtFirstName = e.Item.Cells( 1 ).Controls( 0 )

strFirstName = txtFirstName.Text

strSql = "Update Employees Set FirstName=@FirstName " _

& " Where EmployeeID=@EmployeeID"

cmdSql = New SqlCommand( strSql, conNorthwind )

cmdSql.Parameters.Add( "@FirstName", strFirstName )

cmdSql.Parameters.Add( "@EmployeeID", intEmployeeID )

conNorthwind.Open()

cmdSql.ExecuteNonQuery()

conNorthwind.Close()

dgrdEmployees.EditItemIndex = -1

BindDataGrid

End Sub

 Sub dgrdEmployees_CancelCommand( s As Object, e As DataGridCommandEventArgs )

dgrdEmployees.EditItemIndex = -1

BindDataGrid

End Sub

 </Script>

 <html>

<head><title>ExpertDataGridEditEmployees.aspx</title></head>

<body>

<form Runat="Server">

 <asp:DataGrid

ID="dgrdEmployees"

OnEditCommand="dgrdEmployees_EditCommand"

OnUpdateCommand="dgrdEmployees_UpdateCommand"

OnCancelCommand="dgrdEmployees_CancelCommand"

DataKeyField="EmployeeID"

AutoGenerateColumns="False"

CellPadding="10"

HeaderStyle-BackColor="LightGrey"

Runat="Server">

<Columns>

<asp:BoundColumn

HeaderText="Employee ID"

DataField="EmployeeID"

ReadOnly="True" />

<asp:BoundColumn

HeaderText="First Name" DataField="FirstName" />

<asp:EditCommandColumn

EditText="Edit!"

UpdateText="Update!"

CancelText="Cancel!" />

</Columns>

</asp:DataGrid>

 </form>

</body>

</html>

 

The output of this example is shown below :

It can be seen that updation is possible.

When an item is selected for editing, the dgrdEmployees_EditCommand subroutine is executed. This subroutine sets the value od EditItemIndex property of the DataGrid. When the Update link is clicked then dgrdEmployees_UpdateCommand subroutine executes and FirstName column of employees table is updated. Each row of a DataGrid consists of cell and each cell contains controls.

After a column is updated in the database, the DataGrid control’s EditItemIndex property is assigned the value -1. Any item can be unselected by this value for editing in the DataGrid.

       


 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2015. All Rights Reserved.