dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersMore...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » ASP.NET »

Adding dropdown list in grids header and bind data to dropdown


Posted Date: 24 Apr 2012      Posted By:: S.N.V.Rama Krishna     Member Level: Silver    Member Rank: 1499     Points: 5   Responses: 2



i want to bind the data to dropdownlist using Linq to sql which is present in the head part of the grid view. i want to add the data from the data base. i have done like

protected void GridView1_RowCreated1(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.Header)
{

DropDownList ddl = new DropDownList();
ddl.AutoPostBack = true;
Label lb = new Label();
lb.Text = "Location Id";
e.Row.Cells[0].Controls.Add(lb);
e.Row.Cells[0].Controls.Add(ddl);
SchoolDataDataContext dc = new SchoolDataDataContext();
var c=from m in dc.DepartmentDatas
select m.Location_Id ;
ddl.items.add("Select the Location ID");
ddl.items.Add(C);
ddl.DataBind();
}

But it is now showing the gridview while page is loading.
}




Responses

#667630    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 24/Apr/2012   Rating: 2 out of 52 out of 5     Points: 4

You can add the Drop down while design template in header template of grid as


<asp:templatefield>
<headertemplate>
<asp:dropdownlist id="ddlYears" runat="server">

And then can bind it as

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlCellType.Header)
{

DropDownList mydropDown = (DropDownList)e.Row.FindControl("ddlYears");

mydropDown.DataSource = YourDataset;
mydropDown.DataValueField = "ValueField";
mydropDown.DataTextField = "DisplayFieldName";
mydropDown.DataBind();

}
}




Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#667713    Author: Pawan Awasthi      Member Level: Diamond      Member Rank: 4     Date: 24/Apr/2012   Rating: 2 out of 52 out of 5     Points: 2

Hai Rama,
You can add the dropdown list and label at the Design time of the GridView under the Header template.
Now after adding these controls in the GridView,
you can write the RowDataBound event handler to get the Data From Database and bind to these controls.
As you are writing in the RowCreated event, it should also work but there might be the issue that your controls are getting added to the container control and due to this, it snot able to bind your data and not displaying in the GridView..

Hope it will be helpful to you.

Regards,
Pawan Awasthi(DNS MVM)
+91 8143683708 (pawansoftit@gmail.com)
Outstanding Contribution Award..NTT Data Inc



 
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 : How do i change my password using 3 tier architecture
Previous : How can i prepare website setup
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.