How to Bind Gridview using Dictionary DataSource in ASP.NET

Introduction about Dictionary

If you know about array , array can store only single value or single type value  in the memory . It can't store double value in single memory location with different types. Suppose you want to store name and age of the person in single memory location then you must use collection . Here in this example we will use Dictionary collection.
The simple syntax of dictionary object are

Dictionary<type, type> dict =  new Dictionary<type, type>(); 
The above mention syntax is says to you that if you want to store person age with person name then you must use Dictionary
for example 



//create instance of the the Dictionary 
Dictionary<string,int> dict =new Dictionary<string,int>();

//Add item to the dictionary
dict.Add("my name is jacob", 26);
dict.Add("my name is lefore",27);

Now if you want to show of the data , You can use any data control such as GridView , FormView , ListView etc.

Lets take an simple example to bind GridView using Dictionary collection
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %>

<!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 ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
 
    </div>
    </form>
</body>
</html>

CodeBehind

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

public partial class Default11 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict.Add("My name is jacob", 26);
        dict.Add("My name is lefore", 27);
        dict.Add("My name is michel", 24);
        dict.Add("My name is lisa", 19);
        GridView1.DataSource = dict;
        GridView1.DataBind();


    }
}

Output
How to Bind Gridview using Dictionary DataSource in ASP.NET

Download Source Code

How to Bind Gridview using Dictionary DataSource in ASP.NET How to Bind Gridview using Dictionary DataSource in ASP.NET Reviewed by Jacob Lefore on September 08, 2013 Rating: 5

No comments