Sunday, 21 June 2015

Bind XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net

6/21/2015 - By Pranav Singh 6

This will show you how you can bind  the XML file data to gridview as per Category and SubCategory in using

Your Page code.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="False">
                        <asp:Label ID="lblMainCategory" runat="server" Text='<%# Eval("MainCategory") %>'
                            Font-Bold="false" Style="font-weight: 700"></asp:Label>
                        <asp:Label ID="lblMainCatID" runat="server" Text='<%# Eval("Category_Id") %>' Visible="false"></asp:Label>
                        <br />
                        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show selected Value" />
    <br />
    Text :
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    Id :
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

Now we will create a XML file to populate the data.

<?xml version="1.0" encoding="utf-8" ?>
    <Category Name="Green" Id="1"></Category>
    <Category Name="Blue" Id="2"></Category>
    <Category Name="Red" Id="3"></Category>
    <Category Name="English" Id="4"></Category>
    <Category Name="Hindi" Id="5"></Category>

After creating the XML file add the ML file into the App_Data folder.

Now add the below mention code inside the .cs page.

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

namespace WebApplication2
    public partial class WebForm7 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
        /// <summary>
        /// Bind the grid view as per the XML file data
        /// </summary>
        public void BindGrid()
            DataSet objds = new DataSet();
            if (objds.Tables[0].Rows.Count > 0)
                DataTable objdtmaincat = new DataTable();
                objdtmaincat = objds.Tables[0].AsEnumerable().Where(m => m.Field<string>("MainCategory") != null).CopyToDataTable();
                GridView1.DataSource = objdtmaincat;
                //Bind the sub category (CheckBox List)

                for (int i = 0; i < GridView1.Rows.Count; i++)
                    CheckBoxList chkSubItemList = (CheckBoxList)GridView1.Rows[i].FindControl("CheckBoxList1");
                    Label lblMainCatID = (Label)GridView1.Rows[i].FindControl("lblMainCatID");

                    DataTable objdtSubCat = new DataTable();
                    objdtSubCat = objds.Tables[0].AsEnumerable().Where(m => m.Field<string>("MainCategory") == null && m.Field<int>("Category_Id_0") == Convert.ToInt32(lblMainCatID.Text)).CopyToDataTable();
                    chkSubItemList.DataSource = objdtSubCat;
                    chkSubItemList.DataTextField = "Name";
                    chkSubItemList.DataValueField = "Id";
        /// <summary>
        /// Code to detect the selected value inside the grid view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
            string selectedItemValue = "";
            string selectedItemText = "";
            for (int j = 0; j < GridView1.Rows.Count; j++)
                CheckBoxList CheckBoxList1 = (CheckBoxList)GridView1.Rows[j].FindControl("CheckBoxList1");
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    if (CheckBoxList1.Items[i].Selected == true)
                        selectedItemValue = selectedItemValue + CheckBoxList1.Items[i].Value + ",";
                        selectedItemText = selectedItemText + CheckBoxList1.Items[i].Text + ",";
            Label1.Text = selectedItemText;
            Label2.Text = selectedItemValue;

In above code I have created a method to bind the grid view. In this I have first read the XML file. And in this I have get xml data as shown below format.

Now filter of data for main category and detection of the control. After getting control I have filter the data as per the main category. As per category I have get filter data shown below. I have bind the filter data to the CheckBoxList.

After binding the detail output will look as shown below.

Now on button click selected check box value have been detected and displayed.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post


  1. it is such as a great article!!!
    can we do the same using mvc?

    1. Thanks for your valuable comment. Thanks for suggesting me a new article. i will do the same. keep visiting.

    2. Hi please check the same article in MVC

    3. thank you for your good article!
      i have a question
      is there any possibility to retrive the data from database into checkboxlist
      such as
      each row in the database contain question and answer
      so for each row in the table required to create checkboxlist

      i will appreciate if you could answer my question

  2. Hi,
    i want to know how to bind databse(table) into checkbox as multiple choice question and determine the one or true answer


Please let me know your view

Free Ebooks

About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your


Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at


Global Classified :
© 2014 Designed by Bloggertheme9.
back to top