Net-informations.com
SiteMap  | About    

ASP.NET GridView Editing

The GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control.

gridview-edit

The GridView allows editing on a row-by-row basis. An editable GridView contains an additional column with an Edit button in each row. When the end user clicks on an Edit button that row becomes editable, causing the Edit button to change to Update and Cancel buttons and the other columns to become TextBoxes. The end user can then update one or more column values and click Update to save their changes. To enable editing, set the AutoGenerateEditButton property to true and specify the DataKeyNames as the primary key of your table.

Download Database

In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.

Download

Before you start to create Edit functionality on GridView in your asp file, you should create a ConnectionString in your web.Config File. Double click the web.config file on the right hand side of the Visual Studio and add the following connectionstring code in that file.

web.config file

Web.Config File

  

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
	<add name="SQLDbConnection"
	 connectionString="Server=Your-Server-Name; Database=pubs; User Id=sa; password= your-passoword"
	 providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

The following program shows how to update gridview values from an ASP.NET application

Default.aspx

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AutoGenerateEditButton="true"
	AllowSorting="True" DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="stor_id">
		<Columns>
		<asp:BoundField ReadOnly="True" HeaderText="stor_id"
		DataField="stor_id" SortExpression="stor_id"></asp:BoundField>
		<asp:BoundField HeaderText="stor_name" DataField="stor_name"
		SortExpression="stor_name"></asp:BoundField>
		<asp:BoundField HeaderText="stor_address" DataField="stor_address"
		SortExpression="stor_address"></asp:BoundField>
		<asp:BoundField HeaderText="city" DataField="city"
		SortExpression="city"></asp:BoundField>
		<asp:BoundField HeaderText="state" DataField="state"
		SortExpression="state"></asp:BoundField>
		<asp:BoundField HeaderText="zip" DataField="zip"
		SortExpression="zip"></asp:BoundField>
		</Columns>
	</asp:GridView>
	<asp:SqlDataSource ID="SqlDataSource1" runat="server"
	ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
	SelectCommand="select * from stores"
	UpdateCommand="UPDATE [stores] SET [stor_name] = @stor_name , [stor_address] =  @stor_address , [city] = @city , [state]=@state , [zip]=@zip
	WHERE [stor_id] = @stor_id" >
	<UpdateParameters>
	<asp:Parameter Type="String" Name="stor_name"></asp:Parameter>
	<asp:Parameter Type="String" Name="stor_address"></asp:Parameter>
	<asp:Parameter Type="String" Name="city"></asp:Parameter>
	<asp:Parameter Type="String" Name="state"></asp:Parameter>
	<asp:Parameter Type="String" Name="zip"></asp:Parameter>
	</UpdateParameters>
	</asp:SqlDataSource>
	</div>
	</form>
</body>
</html>






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.