• This workspace has been inactive for over 11 months, and is scheduled to be reclaimed. Make an edit or click here to mark it as active.
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Dokkio Sidebar (from the makers of PBworks) is a Chrome extension that eliminates the need for endless browser tabs. You can search all your online stuff without any extra effort. And Sidebar was #1 on Product Hunt! Check out what people are saying by clicking here.


Lecture - Form Objects - GridView

Page history last edited by Dr. Ron Eaglin 10 years, 4 months ago

 The ASP.NET GridView Control





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.




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.







GridView Part 1 -


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 




Part 2 Code





















<%@ 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>
    <form id="form1" runat="server">
    <h1 style="text-align:center"></h1>
    <td>Select Category</td>
        <asp:DropDownList ID="ddlSelectCategory" runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="DisplayText" 
            DataValueField="id" AutoPostBack="True" 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TraumaFlowConnectionString %>" 
            SelectCommand="SELECT * FROM [Categories]"></asp:SqlDataSource>
    <td>Enter Code</td><td>
        <asp:TextBox ID="tbCode" runat="server"></asp:TextBox></td>
        <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">
                <asp:ControlParameter ControlID="ddlSelectCategory" Name="CategoryID" 
                    PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="tbCode" Name="DisplayText" PropertyName="Text" 
                    Type="String" />
        <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TraumaFlowConnectionString %>" 
            SelectCommand="sp_UpdateCode" SelectCommandType="StoredProcedure">
                <asp:SessionParameter Name="CodeID" SessionField="CodeID" Type="Int32" />
                <asp:ControlParameter ControlID="tbCode" Name="DisplayText" PropertyName="Text" 
                    Type="String" />
        <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" />
                <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:LinkButton runat="server" ID="lbEdit" Text="Edit This Code" CommandName="editinform" 
                CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"></asp:LinkButton>
            <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: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.CategoryID
WHERE Categories.id = @CategoryID">
                <asp:ControlParameter ControlID="ddlSelectCategory" Name="CategoryID" 
                    PropertyName="SelectedValue" />



















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"]);
            { return 0; }
        protected void Page_Load(object sender, EventArgs e)
        protected void btnSubmit_Click(object sender, EventArgs e)
            tbCode.Text = string.Empty;
        protected void ddlSelectCategory_SelectedIndexChanged(object sender, EventArgs e)
            tbCode.Text = string.Empty;
        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)
            tbCode.Text = string.Empty;
            btnSubmit.Visible = true;
            btnUpdate.Visible = false;




Additional Information


Adding Item Templates to Datagrid - Video covering more details of Item Templates and the DataGrid Control.



COP 4834 Lectures Page

Comments (0)

You don't have permission to comment on this page.