The ASP.NET GridView Control
Prerequisites
You should have a decent mastery of all the basic form objects at COP 4834 Lectures Page – these will give you a foundation for the more advanced objects here.
Summary
GridView Part 1 –
GridView Part 2 – Part 2 demonstrates how to use the ItemTemplate and Template fields to add custom form objects into a GridView. This video also demonstrates how to customize the RowCommand behavior and event handling.
Video
GridView Part 1 –http://roneaglin.online/COP4834/lectures/video-not-found/
GridView Part 2 – http://online1.daytonastate.edu/player2.php?id=b06f50d1f89bd8b2a0fb771c1a69c2b0
Reference Materials
If you really like the GridView – here is an entire site devoted to this object http://www.gridview.net/
I demonstrate some use of the Session State variable in this video – more information here – http://msdn.microsoft.com/en-us/library/ms178581.aspx
Code
Part 2 Code
InsertCode.aspx | <%@ Page Language=”C#” Title=”Insert/Update Codes” AutoEventWireup=”true” CodeBehind=”InsertCode.aspx.cs” Inherits=”TraumaFlow.CategoryManagement.InsertCode” %><!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>Insert Code</title></head><body> <form id=”form1″ runat=”server”> <div> <h1 style=”text-align:center”></h1> <table><tr> <td>Select Category</td> <td> <asp:DropDownList ID=”ddlSelectCategory” runat=”server” DataSourceID=”SqlDataSource1″ DataTextField=”DisplayText” DataValueField=”id” AutoPostBack=”True” onselectedindexchanged=”ddlSelectCategory_SelectedIndexChanged”> </asp:DropDownList> <asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:TraumaFlowConnectionString %>” SelectCommand=”SELECT * FROM [Categories]”></asp:SqlDataSource> </td> </tr> <tr> <td>Enter Code</td><td> <asp:TextBox ID=”tbCode” runat=”server”></asp:TextBox></td> </tr> </table> <asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” onclick=”btnSubmit_Click” /> <asp:Button ID=”btnUpdate” runat=”server” Text=”Update” onclick=”btnUpdate_Click” Visible=”False” /> <asp:SqlDataSource ID=”SqlDataSource2″ runat=”server” ConnectionString=”<%$ ConnectionStrings:TraumaFlowConnectionString %>” SelectCommand=”sp_InsertCode” SelectCommandType=”StoredProcedure”> <SelectParameters> <asp:ControlParameter ControlID=”ddlSelectCategory” Name=”CategoryID” PropertyName=”SelectedValue” Type=”Int32″ /> <asp:ControlParameter ControlID=”tbCode” Name=”DisplayText” PropertyName=”Text” Type=”String” /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID=”SqlDataSource4″ runat=”server” ConnectionString=”<%$ ConnectionStrings:TraumaFlowConnectionString %>” SelectCommand=”sp_UpdateCode” SelectCommandType=”StoredProcedure”> <SelectParameters> <asp:SessionParameter Name=”CodeID” SessionField=”CodeID” Type=”Int32″ /> <asp:ControlParameter ControlID=”tbCode” Name=”DisplayText” PropertyName=”Text” Type=”String” /> </SelectParameters> </asp:SqlDataSource> <hr /> <asp:GridView ID=”gvCodes” runat=”server” AutoGenerateColumns=”False” CellPadding=”4″ DataKeyNames=”CategoryID,CodeID” DataSourceID=”SqlDataSource3″ EnableModelValidation=”True” ForeColor=”#333333″ GridLines=”None” OnRowCommand=”gvCodes_RowCommand”> <AlternatingRowStyle BackColor=”White” /> <Columns> <asp:BoundField DataField=”CategoryID” HeaderText=”CategoryID” InsertVisible=”False” ReadOnly=”True” SortExpression=”CategoryID” /> <asp:BoundField DataField=”CodeID” HeaderText=”CodeID” InsertVisible=”False” ReadOnly=”True” SortExpression=”CodeID” /> <asp:BoundField DataField=”Category” HeaderText=”Category” SortExpression=”Category” /> <asp:BoundField DataField=”Code” HeaderText=”Code” SortExpression=”Code” /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton runat=”server” ID=”lbEdit” Text=”Edit This Code” CommandName=”editinform” CommandArgument=”<%# ((GridViewRow) Container).RowIndex %>”></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <EditRowStyle BackColor=”#2461BF” /> <FooterStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” /> <HeaderStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” /> <PagerStyle BackColor=”#2461BF” ForeColor=”White” HorizontalAlign=”Center” /> <RowStyle BackColor=”#EFF3FB” /> <SelectedRowStyle BackColor=”#D1DDF1″ Font-Bold=”True” ForeColor=”#333333″ /> </asp:GridView> <asp:SqlDataSource ID=”SqlDataSource3″ runat=”server” ConnectionString=”<%$ ConnectionStrings:TraumaFlowConnectionString %>” SelectCommand=”SELECT Categories.id AS ‘CategoryID’, Codes.id AS ‘CodeID’, Categories.DisplayText AS ‘Category’, Codes.DisplayText AS ‘Code’ FROM Categories INNER JOIN Codes ON Categories.id = Codes.CategoryIDWHERE Categories.id = @CategoryID”> <SelectParameters> <asp:ControlParameter ControlID=”ddlSelectCategory” Name=”CategoryID” PropertyName=”SelectedValue” /> </SelectParameters> </asp:SqlDataSource> </div> </form></body></html> |
InsertCode.apsx.cs | using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace TraumaFlow.CategoryManagement{ public partial class InsertCode : System.Web.UI.Page { public int CodeID() { if (Session[“CodeID”] != null) { return Convert.ToInt32(Session[“CodeID”]); } else { return 0; } } protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { SqlDataSource2.Select(DataSourceSelectArguments.Empty); tbCode.Text = string.Empty; gvCodes.DataBind(); } protected void ddlSelectCategory_SelectedIndexChanged(object sender, EventArgs e) { tbCode.Text = string.Empty; gvCodes.DataBind(); } protected void gvCodes_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); if (e.CommandName == “editinform”) { GridViewRow row = gvCodes.Rows[index]; tbCode.Text = row.Cells[3].Text; Session.Add(“CodeID”, row.Cells[1].Text); btnSubmit.Visible = false; btnUpdate.Visible = true; } } protected void btnUpdate_Click(object sender, EventArgs e) { SqlDataSource4.Select(DataSourceSelectArguments.Empty); tbCode.Text = string.Empty; gvCodes.DataBind(); btnSubmit.Visible = true; btnUpdate.Visible = false; Session.Remove(“CodeID”); } }} |