Live Chat Icon For mobile
Live Chat Icon

How to edit data in the DataGrid using a TemplateColumn

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid id='DataGrid1' AutoGenerateColumns=False OnCancelCommand ='CancelCmd' OnEditCommand ='EditCmd' 
	OnUpdateCommand ='UpdateCmd' DataKeyField='Employeeid'  runat='server'>
<Columns>
	<asp:TemplateColumn HeaderText ='Employee Information'>
	<ItemTemplate >
		<asp:label ID='lblLastName' Runat=server text=<%#DataBinder.Eval(Container.DataItem, 'Lastname')%>></asp:Label> 
,		<asp:label ID='lblFirstName' Runat=server text=<%#DataBinder.Eval(Container.DataItem, 'Firstname')%>></asp:Label> 
		<br>
		<asp:label ID='lblTitle' Runat=server text=<%#DataBinder.Eval(Container.DataItem, 'Title')%>> 
	</ItemTemplate>
	<EditItemTemplate >
		<asp:TextBox runat=server ID='txtLastName' Text=<%#DataBinder.Eval(Container.DataItem, 'lastname')%>></asp:TextBox>
		<asp:RequiredFieldValidator  id='rqdfldLastName' Display ='Static' Runat =server ErrorMessage ='*' 
			ControlToValidate ='txtLastName'>
		<asp:TextBox runat=server ID='txtFirstName' text=<%#DataBinder.Eval(Container.DataItem, 'Firstname')%>></asp:TextBox>
		<asp:RequiredFieldValidator  id='rqdfldFirstName' Display ='Static' Runat =server ErrorMessage ='*' 
			ControlToValidate ='txtFirstName'>
		<asp:TextBox runat=server ID='txtTitle' Text=<%#DataBinder.Eval(Container.DataItem, 'title')%>></asp:TextBox>
	</EditItemTemplate>
	</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType='PushButton' CancelText='Cancel' EditText='Edit' UpdateText='Update'></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>

VB.NET


Dim sqlStmt As String
Dim conString As String
Dim cn As SqlConnection
Dim cmd As SqlCommand
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        	’ Put user code to initialize the page here
        	If Not Page.IsPostBack Then
            		BindGrid()
        	End If
End Sub ’Page_Load

Sub BindGrid()
        	’Bind the Datagrid with dataSet
End Sub ’BindGrid


Protected Sub EditCmd(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        	DataGrid1.EditItemIndex = CInt(e.Item.ItemIndex)
        	BindGrid()
End Sub ’EditCmd

Public Sub CancelCmd(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        	DataGrid1.EditItemIndex = -1
        	BindGrid()
End Sub ’CancelCmd

Protected Sub UpdateCmd(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        	If Page.IsValid Then
            		sqlStmt = 'UPDATE Employees SET LastName = @LastName, FirstName=@FirstName,Title=@Title  where EmployeeId = @EmployeeId'
            		conString = 'server=localhost;database=Northwind;uid=sa;pwd=;'
            		cn = New SqlConnection(conString)
            		cmd = New SqlCommand(sqlStmt, cn)
            		Dim LastName, FirstName, Title As String
            		LastName = CType(e.Item.FindControl('txtLastName'), TextBox).Text
            		FirstName = CType(e.Item.FindControl('txtFirstName'), TextBox).Text
            		Title = CType(e.Item.FindControl('txtTitle'), TextBox).Text
            		Dim EmployeeId As Integer = CInt(DataGrid1.DataKeys(CInt(e.Item.ItemIndex)))

            		cmd.Parameters.Add(New SqlParameter('@LastName', LastName))
            		cmd.Parameters.Add(New SqlParameter('@FirstName', FirstName))
            		cmd.Parameters.Add(New SqlParameter('@Title', Title))
            		cmd.Parameters.Add(New SqlParameter('@EmployeeId', EmployeeId))
            		Try
		               cn.Open()
		               cmd.ExecuteNonQuery()
		               DataGrid1.EditItemIndex = -1
	                Catch ex As Exception
		               Response.Write(ex.Message.ToString())
		End Try
		BindGrid()
	End If
End Sub ’UpdateCmd

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if (!Page.IsPostBack )
	{
		BindGrid();
	}
}

void BindGrid()
{
	//Bind the DataGrid with dataset
}

protected void EditCmd(object sender, DataGridCommandEventArgs e)
{
	DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
	BindGrid();
}

public void  CancelCmd(object sender, DataGridCommandEventArgs e)
{
	 DataGrid1.EditItemIndex = -1;
	BindGrid();
}

string sqlStmt;
string conString;
SqlConnection cn;
SqlCommand cmd;
protected void UpdateCmd(object sender, DataGridCommandEventArgs e)
{
	if (Page.IsValid)
	{
	sqlStmt = 'UPDATE Employees SET LastName = @LastName, FirstName=@FirstName,Title=@Title  where EmployeeId = @EmployeeId';
	conString = 'server=localhost;database=Northwind;uid=sa;pwd=;';
	cn = new SqlConnection(conString);
	cmd = new SqlCommand(sqlStmt, cn);
	string LastName , FirstName , Title;
	LastName = ((TextBox )e.Item.FindControl ('txtLastName')).Text ;
	FirstName = ((TextBox )e.Item.FindControl ('txtFirstName')).Text ;
	Title  = ((TextBox )e.Item.FindControl ('txtTitle')).Text ;
	int EmployeeId =(int) DataGrid1.DataKeys[(int)e.Item.ItemIndex];

	cmd.Parameters.Add(new SqlParameter('@LastName', LastName));
	cmd.Parameters.Add(new SqlParameter('@FirstName',FirstName ));
	cmd.Parameters.Add(new SqlParameter('@Title', Title ));
	cmd.Parameters.Add(new SqlParameter('@EmployeeId',EmployeeId ));
	try
	{
		cn.Open();
		cmd.ExecuteNonQuery();
		DataGrid1.EditItemIndex = -1;
	}
	catch(Exception ex)
	{
		Response.Write (ex.Message.ToString () );
	}
	BindGrid();
	}
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.