PROWAREtech

articles » archived » asp-net » updateable-gridview

ASP.NET: Updateable GridView Example

An updateable Web Form GridView server control example (.NET Framework).

The GridView was introduced in .NET 2.0. Like the DataGrid, the GridView allows creating a record-editable table of data but without writing a single line of scripting code.

The page language can be "VB" and it will make no difference.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
	<title>GridView Example</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:SqlDataSource ID="employeeDataSource" Runat="server"
			SelectCommand="SELECT emp_id, fname, lname FROM employee"
			UpdateCommand="UPDATE employee SET fname = @fname, lname = @lname WHERE emp_id = @emp_id"
			ConnectionString="<%$ ConnectionStrings:ConnectPubsSql %>">
			<UpdateParameters>
				<asp:Parameter Type="String" Name="fname"></asp:Parameter>
				<asp:Parameter Type="String" Name="lname"></asp:Parameter>
				<asp:Parameter Type="String" Name="emp_id"></asp:Parameter>
			</UpdateParameters>
		</asp:SqlDataSource>
		<asp:GridView ID="GridView1" Runat="server" DataSourceID="employeeDataSource" DataKeyNames="emp_id"
			AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px" BackColor="#FFFFCC" CellPadding="2"
			BorderStyle="Solid" BorderColor="Black" AllowSorting="True" Font-Names="Arial" ForeColor="#000099" ShowHeaderWhenEmpty="True">
			<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
			<PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC"></PagerStyle>
			<HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#3399FF"></HeaderStyle>
			<AlternatingRowStyle BackColor="#CCFFCC" Font-Names="Arial" />
			<Columns>
				<asp:CommandField ShowEditButton="True"></asp:CommandField>
				<asp:BoundField ReadOnly="True" HeaderText="Employee ID" InsertVisible="False" DataField="emp_id" SortExpression="emp_id"><ItemStyle HorizontalAlign="Right"></ItemStyle></asp:BoundField>
				<asp:BoundField HeaderText="First Name" DataField="fname" SortExpression="fname"></asp:BoundField>
				<asp:BoundField HeaderText="Last Name" DataField="lname" SortExpression="lname"></asp:BoundField>
			</Columns>
			<SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True" BackColor="Silver"></SelectedRowStyle>
			<RowStyle ForeColor="Black" BackColor="White"></RowStyle>
		</asp:GridView>
	
	</div>
	</form>
</body>
</html>
GridView1
GridView2

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE