Skip Navigation LinksHome > Categories > Code from a Category

DataBind a filtered Dataset to a Dropdownlist control



User Name: jamest85
Name: James Tang
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
Google Apps for Work Partner, Certified Zoho CRM Consultant. 7 years experience on Zoho consulting, implementation, and programming using C#, VB, Php..., 15 years of total IT experience including prog... [More]
Viewed Times: 2732
Add Date: 02/17/2010
Quick way to filter a Dataset and then bind it to a Dropdownlist control.
A dropdownlist might only shows a limited values from a Dataset.
DataSet dst = new DataSet(); 
MembershipUser uUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);

//will get 3 columns: bookName, bookId, userWhoPosted
//could use Cache to save it in memeroy.
dst = DataWebCoder.BookAuthor.GetBookNamesAll();

//if uUser'r role is not an administration:
if ((uUser != null) && (Roles.IsUserInRole(uUser.UserName,
                      YourNameSpace.Config.RoleAsAdministration) == false))
{
    dst.Tables[0].DefaultView.RowFilter = "userWhoPosted = '" + uUser.UserName + "'";
    ddlBookNames.DataSource = dst.Tables[0].DefaultView;
}
else
    ddlBookNames.DataSource = dst.Tables[0];

//bind filtered data to dropdownlist
ddlBookNames.DataTextField = "bookName";
ddlBookNames.DataValueField = "bookId";
ddlBookNames.DataBind();
ddlBookNames.Items.Insert(0, new ListItem("--SELECT--", "0"));

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.