.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to fill the dropdownlist

Posted By: Suresh     Posted Date: November 26, 2008    Points:2   Category :ASP.Net
Hi
How to fill the dropdownlist inside the gridview. i want to fill the dropdownlist in edittemplate and also in footer template. can u please give me sample code for this.

Responses
Author: Amit Mehra             
Posted Date: November 26, 2008     Points: 5   

Hi,
Try like this

This is your DatGrid View with template column in the edittemplate

<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataSource = "<%# LoadData()%>" runat="server">
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" DataSource = "<%# LoadData()%>" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


I bound Array List to grid view


public ArrayList LoadData()
{
ArrayList arrList = new ArrayList();
arrList.Add("First");
arrList.Add("Second");
arrList.Add("Third");
return arrList;
}



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
GridView1.DataSource = LoadData();
GridView1.DataBind();
}
}



Hope this will help you

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Akhil Raj       [Moderator]      
Posted Date: November 26, 2008     Points: 5   

Amit Mehra give a nice method. If you want to fill the dropdownlist and show the already selected please look the following article..that will help you..

http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx


Author: Suresh             
Posted Date: November 26, 2008     Points: 5   

HI amit mehra.

i want load the data from database.how? but im using gridview. can u plz explain that .

Author: Amit Mehra             
Posted Date: November 26, 2008     Points: 5   

HI Suresh,
just change the DataSource to DataSet or DataReader it will help you.

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Suresh             
Posted Date: November 26, 2008     Points: 5   

hi amit mehra

<asp:TemplateField HeaderText="City" SortExpression="Cus_City">
<EditItemTemplate>
<asp:Dropdownlist ID="drpCity" runat="server" Text='<%# Bind("Cus_City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Cus_City") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Dropdownlist ID="drpCity1" runat="server" Text='<%# Bind("Cus_City") </FooterTemplate>
</asp:TemplateField>

this is my code how can i do for this.

Author: Suresh             
Posted Date: November 26, 2008     Points: 5   

hi amit mehra

<asp:TemplateField HeaderText="City" SortExpression="Cus_City">
<EditItemTemplate>
<asp:Dropdownlist ID="drpCity" runat="server" Text='<%# Bind("Cus_City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Cus_City") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Dropdownlist ID="drpCity1" runat="server" Text='<%# Bind("Cus_City") </FooterTemplate>
</asp:TemplateField>

this is my code how can i do for this.

Author: Akhil Raj       [Moderator]      
Posted Date: November 27, 2008     Points: 5   

hi suresh,
I think you read the article i gave. You can find the nice way to fill the drop down list in the edititemtemplate there and also maintain the old value if you want..

Author: Syed Shakeer Hussain             
Posted Date: March 15, 2009     Points: 5   


Retriving data from database and Add Items to DropDownList uisng C#

Suppose I created a table name as ddltable,and I have two columns in that table.column names are id and name.I inserted some values in a ddltable.Now I want to retrive a id column values and display in dropdownlist.

We can do this one in two ways.
1)by using DataReader
2)by using DataSet;

Method I :
---
SqlConnection conn = new SqlConnection(" your connection string ");
conn.Open();
SqlCommand cmd = new SqlCommand("select id from ddltable ", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList2.Items.Add(dr["id"].ToString ());
}



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend