Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating dropdown in datagrid 2

Status
Not open for further replies.

monkeymeister

Programmer
Mar 17, 2005
58
GB
I am populating a drop down column in a datagrid on page load. Here is my code :

<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList ID="ddlUserName" Font-Name="Verdana" Font-Size="8pt" Runat=server
DataValueField="UserName"
DataTextField="UserName"
DataSource='<%# GetUserList() %>'>
</asp:DropDownList>

</ItemTemplate>
</asp:TemplateColumn>


public DataSet GetUserList()
{
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["strAtradius"]);

string strUserList = "select username from userlogin where jobrolekey = 2 ";
strUserList += "order by username";

SqlDataAdapter objDataAdapter = new SqlDataAdapter(strUserList, objConnection);

objDataAdapter.Fill(dsUsers, "Users");

return dsUsers;
}

dsUsers is global to the page. The problem I have is that the first row of the datagrid populates correctly, but the following lines append the contents of the dropdown in the row above. How do I clear the contents of the dropdown before populating each row?



Any help would be really appreciated.



Cheers,

Mike



 
The problem I have is that the first row of the datagrid populates correctly, but the following lines append the contents of the dropdown in the row above. How do I clear the contents of the dropdown before populating each row?
I'm not sure what you mean by the first part of that but to answer your question about clearing a DDL, you can just use FindControl to get a handle on the DDL and then use it's Items.Clear method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Another thing to point out, is that you are calling GetUserList() to get the values for your ddl. This is fine, however, it will run for each row of the grid. So imagine if you had 500 rows. You would be calling this function 500 times. So, you would be accessing the DB 500 times. You should just call that function once, in the Page_Load(Check if it is a PostBack). This way the dataset is filled only once, no matter how many rows are in the grid.

Jim
 
Well spotted Jim - I didn't pick up on that!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
No probs, I'd hate to send the user away from this post with them running a SQL Statement 500 times!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top