dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersRaj.Trivedi
Abul Bashar Sardar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » .NET »

How to bind gridview dropdownlist


Posted Date: 12 Jul 2012      Posted By:: Lawrence     Member Level: Gold    Member Rank: 319     Points: 2   Responses: 7



Hi Everyone,
I have one dropdownlist in gridview.... How to bind database values to dropdownlist.....
Eg:
Database:
Names[Column Name]
Lawrence
Raja
Grace
Rhenius...
I bound this values in gridview dropdownlist....

Thanks In Advanced




Responses

#679999    Author: Nathaniel Nellas Sumaya      Member Level: Silver      Member Rank: 1260     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 2

there many ways on how to achieve you goal. One of my method is to use object datasource where in I bound the object to the object data source object. And in gridview you can change the template to drop down list.


 
#680001    Author: Lawrence      Member Level: Gold      Member Rank: 319     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 0

thanks for ur reply....How to write the code....


 
#680005    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 3

The best option is to bind the Drop down list in the Row Data Bound event, FInd the Drop down list and you can call the bind method there \
here is a sample code for the same.


void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{
label lbl = (label) e.Row.FindControl("lblHidden");
DropDownList ddl = (label) e.Row.FindControl("ddlID");

if(lbl.Text =="A")
{
ddl.Items.Clear();
ddl.Items.Add("B");
ddl.Items.Add("C")
}
Else if(lbl.Text =="B")
{
ddl.Items.Clear();
ddl.Items.Add("C")
}
}

}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM






 
#680010    Author: Lawrence      Member Level: Gold      Member Rank: 319     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 0

thanks for ur reply.... I inserted names in one page and if i see all the names then i bound the gridview but that names in shown in dropdownlist.....


 
#680025    Author: saravanakumar      Member Level: Gold      Member Rank: 195     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

hi friend this code is gridview itemtemplate textbox textchange event. i think it is help u.

protected void txt_pireceived_qty_TextChanged(object sender, EventArgs e)
{
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent;

DropDownList vehicle_piname = (DropDownList)currentRow.FindControl("ddl_pivehicle_name");

vehicle_piname.Items.Clear();
vehicle_piname.Items.Insert(0, "Select");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select value from grn_ddl_control where type='vehi'", con);
DataSet ds = new DataSet();
da.Fill(ds, "table");
con.Close();

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
vehicle_piname.Items.Add(ds.Tables[0].Rows[i][0].ToString().Trim());
}

}



 
#680050    Author: Paritosh Mohapatra      Member Level: Diamond      Member Rank: 6     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Please check the following code:


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="LblSId" runat="server" Text='<%#Eval("sid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:SqlDataSource SelectCommand="Select * from student" ConnectionString="<%$ ConnectionStrings:sqlcon %>" ID="SqlDSMaker" runat="server"></asp:SqlDataSource>
<asp:DropDownList ID="ddlMaker" DataValueField="sid" DataSourceID="SqlDSMaker" DataTextField="sname" AutoPostBack="True" runat="server" Text='<%# Bind("sid") %>'></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



Thanks & Regards
Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM



 
#680315    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 20     Date: 13/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

See the below example:


<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<%# Eval("ProductID") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:DropDownList ID="ddlProductName" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView();
}
}

private void BindGridView()
{
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter sda;
DataSet ds;

conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["NWConnectionString"].ConnectionString;

cmd = new SqlCommand();
cmd.CommandText = "Select top 5 ProductID, ProductName from Products";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;

sda = new SqlDataAdapter();
sda.SelectCommand = cmd;

ds = new DataSet();
sda.Fill(ds);

gvProducts.DataSource = ds;
gvProducts.DataBind();

foreach (GridViewRow gvRow in gvProducts.Rows)
{
DropDownList ddList = (DropDownList)gvProducts.Rows[gvRow.RowIndex].Cells[1].FindControl("ddlProductName");
ddList.DataSource = ds;
ddList.DataValueField = "ProductID";
ddList.DataTextField = " ProductName";
ddList.DataBind();
}
}


Hope it'll help you.
Regards
Ajatshatru



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Read&Write Large Text File Simultaneously.
Previous : Explain me about azure
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.