populating dropdown in datagrid

M

Mike P

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I guess is cause you are adding rows to the Datatable, what if you clear
the table?

cheers,
 
B

bfking

you want to make the call to GetUserList() in Page_Load, then set the
DataSource of the ddlUserName like DataSource='<%# dsUsers %>'> b/c u
don't want to make a call to the db every iteration of the datagrid
items. This will also fix your issue of adding the new rows to the
table.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top