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 asp.net using c#.net.




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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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" AutoGenerateColumns="False" ShowHeader="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <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">
                        </asp:CheckBoxList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    <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>
    </form>
</body>
</html>

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

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

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)
            {
                BindGrid();
            }
        }
        /// <summary>
        /// Bind the grid view as per the XML file data
        /// </summary>
        public void BindGrid()
        {
            DataSet objds = new DataSet();
            objds.ReadXml(Server.MapPath("~\\App_Data\\XMLFile1.xml"));
            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;
                GridView1.DataBind();
                //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";
                    chkSubItemList.DataBind();
                }
            }
        }
        /// <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

6 comments:

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

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

      Delete
    2. Hi please check the same article in asp.net MVC

      http://www.aspdotnet-pools.com/2015/06/bind-xml-file-data-to-gridview-by_24.html

      Delete
    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

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

    ReplyDelete

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

GMAIL ID: dotnetpools@gmail.com

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 contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top