How to Populate a DropDownList insides a DataGrid through an XML File


In this article, we will show how to fill a DropDownList inside a DataGrid through an XML file. We will also show how the contents of one DropDownList changes dynamically as we change the index of second DropDownList (both inside the grid).

Brief description:

Here we have a datagrid with two template columns; we have two dropdowns inside these columns. In this example one dropdown is populated with the states and second is populated with the cities of state of the corresponding (same row) dropdown. If we change the index of dropdown populated with states second dropdown is populated with the cities of corresponding state.

Main events:

  1. IsPostBack to bind events
  2. SelectedIndexChanged event of Dropdown.
  3. XML file code access to populate dropdown list in datagrid.

Source Code:

using
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Xml;
namespace Helper_Project
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid sampleGrid;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
this.FillGrid();//this function populates the grid
this.FillState();//for every dataitem in grid this function each state dropdown
}
// note how SelectedIndexChanged event of dropdown is declared and it is for very dropdown in the grid.
foreach(DataGridItem dItem in sampleGrid.Items)
{
DropDownList stateList = (DropDownList)dItem.Cells[1].FindControl("stateList");
stateList.SelectedIndexChanged +=
new System.EventHandler
( this.stateList_SelectedIndexChanged);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//this function populates the grid
private void FillGrid()
{
DataTable dTable =
new DataTable();
DataColumn dColumn =
new DataColumn("Id",System.Type.GetType("System.String"));
dTable.Columns.Add(dColumn);
for(int i = 0; i < 5 ; i++)
{
DataRow row = dTable.NewRow();
row[0] = i;
dTable.Rows.Add(row);
}
sampleGrid.DataSource = dTable;
sampleGrid.DataBind();
}
//for every dataitem in grid this function each state dropdown
private void FillState()
{
XmlTextReader xRead =
new XmlTextReader(Server.MapPath("XMLFile1.xml"));
XmlDocument doc =
new XmlDocument();
doc.Load(Server.MapPath("XMLFile1.xml"));
XmlNodeList xList = doc.GetElementsByTagName("state");
XmlNode node =
null;
XmlNode node0 = xList.Item(0);
for(int num = 0; num < 5 ; num ++)
{
for(int i= 0; i < xList.Count; i++)
{
node = xList.Item(i);
ListItem lItem =
new ListItem(xList.Item(i).Attributes.GetNamedItem
("key").Value,xList.Item(i).Attributes.GetNamedItem("key").Value);
((DropDownList)sampleGrid.Items[num].Cells[1].FindControl("stateList")).Items.Add
(lItem);
}
this.FillCity(node0);
}
}
//for every state cities are filled accordingly
private void FillCity(XmlNode xNode)
{
string city = "";
if(!object.Equals(xNode,null))
{
XmlNodeList xCity = xNode.ChildNodes;
foreach(DataGridItem dt in sampleGrid.Items)
{
((DropDownList)dt.Cells[0].FindControl("cityList")).Items.Clear();
for(int j = 0; j < xCity.Count; j ++)
{
city = xCity.Item(j).InnerText;
ListItem lItem =
new ListItem(city,city);
((DropDownList)dt.Cells[0].FindControl("cityList")).Items.Add(lItem);
}
}
}
}
//In this function note that how the datagriditem of the current row is accessed
//so that cities of that particular row only should be changed.
private void stateList_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList dlist = (DropDownList)sender;
TableCell s = (TableCell)dlist.Parent;
DataGridItem dItem = (DataGridItem)s.Parent;
int index = dItem.ItemIndex;
XmlTextReader xRead =
new XmlTextReader(Server.MapPath("XMLFile1.xml"));
XmlDocument doc =
new XmlDocument();
doc.Load(Server.MapPath("XMLFile1.xml"));
XmlNodeList xList = doc.GetElementsByTagName("state");
XmlNode node =
null;
for(int i = 0; i < xList.Count; i++)
{
if(xList.Item(i).Attributes.GetNamedItem("key").Value == dlist.SelectedValue)
{
node = xList.Item(i);
break;
}
}
((DropDownList)dItem.Cells[0].FindControl("cityList")).Items.Clear();
foreach(XmlNode nod in node.ChildNodes)
((DropDownList)dItem.Cells[0].FindControl("cityList")).Items.Add(nod.InnerText);
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all