By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,679 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,679 IT Pros & Developers. It's quick & easy.

populating dropdown in datagrid

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Dec 1 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
The code that you posted shows that you return an object named dsUsers but it
does not show when the object was instantiated. If the scope of that
variable "dsUsers" is outside the scope of the function GetUsersList then
your table would keep appending records for each row of the datagrid
processed. You should move the steps of populating the table to the
Page_Load if they are not dependent on any value in the datagrid item
otherwise you code has to change to return only a DataTable that is local in
scope to the "GetUsersList" function.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Mike P" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***

Dec 1 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.