Web Applications in Action

with Telerik, Ext.NET (Sencha), jQuery
using ASP.NET WebForms / MVC (C#)

Contact webapps.codeplex.com

How to use a simple GridView with ASP.NET

this little example was requested by Salwa Khan. It's plain ASP.NET WebForms. Hope this helps. Basically this is okay for small data because of binding always the whole data set (unless you page/select only few records).
It's about to toggle Button State along with the DataItem of current row. I suggest also to use a Enum for Approved/Status. But this depends on your data source.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SimpleGridView.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView runat="server" ID="gridView" AutoGenerateColumns="false" OnRowCommand="gridView_RowCommand" OnRowDataBound="gridView_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="ProposalID" HeaderText="ProposalID" InsertVisible="False" ReadOnly="True" SortExpression="ProposalID" />
                    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                    <asp:BoundField DataField="GrantAmount" HeaderText="GrantAmount" SortExpression="GrantAmount" />
                    <asp:TemplateField HeaderText="Assess">
                        <ItemTemplate>
                            <asp:Button ID="btnAprv" runat="server" CommandName="Approve" Text="Approve" CommandArgument='<%#Eval("ProposalID") %>' Enabled="false" />
                            <asp:Button ID="btnRej" runat="server" CommandName="Reject" Text="Reject" CommandArgument='<%#Eval("ProposalID") %>' Enabled="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Approved">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Approved")%>' ReadOnly="true"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

            </asp:GridView>
        </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace SimpleGridView
{
    public partial class Default : System.Web.UI.Page
    {
        // Author: Otto Neff | webapps-in-action.com
        [Serializable]
        public class RowData
        {
            public System.Guid ProposalID { get; set; }
            public string Title { get; set; }
            public decimal GrantAmount { get; set; }
            public string Approved { get; set; }
        }
        protected List<RowData> data
        {
            get
            {
                if (ViewState["data"] == null)
                {
                    ViewState["data"] = new List<RowData>
                {
                    new RowData{ Approved = "Yes", GrantAmount = (decimal)10.4, ProposalID = Guid.NewGuid(), Title = "Title 1" },
                    new RowData{ Approved = "No", GrantAmount = (decimal)5.4, ProposalID = Guid.NewGuid(), Title = "Title 2" },
                    new RowData{ Approved = "", GrantAmount = (decimal)6.2, ProposalID = Guid.NewGuid(), Title = "Title 3" },
                    new RowData{ Approved = "", GrantAmount = (decimal)1.7, ProposalID = Guid.NewGuid(), Title = "Title 4" },
                };
                }
                return (ViewState["data"] as List<RowData>);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gridView.DataSource = data;
                gridView.DataBind();
            }
        }
        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Guid id = Guid.Parse(e.CommandArgument.ToString());
            switch (e.CommandName.ToString().ToLower())
            {
                case "approve":
                    var rApprove = data.Where(x => x.ProposalID == id).FirstOrDefault();
                    rApprove.Approved = "Yes";
                    break;
                default:
                    var rDecline = data.Where(x => x.ProposalID == id).FirstOrDefault();
                    rDecline.Approved = "No";
                    break;
            }
            gridView.DataSource = data;
            gridView.DataBind();
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                RowData r = (e.Row.DataItem as RowData);
                if (r.Approved == "")
                {
                    (e.Row.FindControl("btnAprv") as Button).Enabled = true;
                    (e.Row.FindControl("btnRej") as Button).Enabled = true;
                }
            }
        }
    }
}

 

blog comments powered by Disqus