Friday, May 6, 2011

Add Xml Datasource to gridview DropDown list

HI friends ,In this article i would like to explain "how to bind XML Datasource to Dropdown list" just follow the steps clearly

First we need to create a XML file to store the data.suppose you would like to store contries data in xml file and bind that data to Dropdown list
Step 1:Add new Xml file to your existing Aspnet project

Step 2:copy the following code in to XML file
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
  
  <Country name="India" value="1"></Country>
  <Country name="America" value="2"></Country>
  <Country name="France" value="3"></Country>
  <Country name="Saudi" value="4"></Country>
  <Country name="Srilanka" value="5"></Country>
</Countries> 
 
Step 3:save the file with countries.xml

Step 4:now open the page which contains Gridview

Step 5: Now add the following code for your gridview.In the below code , i am added One Xml datasource which is available in our Toolbox.just Drag and Drop the  Xml datasource.then just add the Datasource ID of the drop downlist

<asp:TemplateField HeaderText="DOB">
  
<ItemTemplate>

<asp:Label ID="lbl_ct"  runat="server" Text='<%# Bind("country")%>'>>

</asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:DropDownList ID="ddl_ct" AutoPostBack="True" 
DataTextField="name" DataValueField="value" runat="server" 
AppendDataBoundItems="True" DataSourceID="countries" 
SelectedValue='<%# Bind("country", "{0}") %>' >
</asp:DropDownList>

  <asp:XmlDataSource ID="countries" runat="server" DataFile="~/XmlDataSource/countries.xml">

</asp:XmlDataSource>

</EditItemTemplate>

</asp:TemplateField>


Success....Now the data will displayed in the Drop down list

Thats it...


developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , , , , ,

2 Comments:

At May 6, 2011 at 12:28 PM , Anonymous Anonymous said...

Good Work...

 
At August 20, 2011 at 4:23 PM , Anonymous Anonymous said...

Thanks For Sharing

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home