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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add a value to datagrid drop down list

Status
Not open for further replies.

iis6newbie

Technical User
Nov 22, 2003
54
0
0
US
Hello all,

How can I add values to a dropdownlist in datagrid using c# code? I have values from file. I use a loop to add those value to the dropdownlist.


Thanks for any help
 
Code:
// this will appear at the end of the list
myDropDownList.Items.Add("Here is another item");
// using insert YOU supply the index
// this will insert the item at the beginning
myDropDownList.Items.Insert(0,"Here is another item");
 
That does not work with datagrid.
I put the dropdownlist in the <EditItemTemplate>

Here is my code:

<asp:TemplateColumn SortExpression="auditedby" HeaderText="Audited By">
<HeaderStyle Font-Overline="True" Font-Bold="True"></HeaderStyle>
<ItemTemplate>
<asp:label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.auditedby") %>'>
</asp:label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlAuditors" runat="server" Width="195px"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

 
Sorry I totally misread.

One was is to create a method that loads your dropDownList and returns a DataReader.
Code:
private SqlDataReader LoadDropDown()
{
   //Your connection stuff,command object etc.
   return yourCommand.ExecuteReader(CommandBehavior.CloseConnection)
   
}
Then back in your EditItemTemplate:
Code:
<EditItemTemplate>
<asp:DropDownList 
id="ddlAuditors" 
runat="server" 
Width="195px"
DataSource='<%# LoadDropDown() %>'
DataTextField = "YourTextField"
DataValueField = "YourIDField" />
EditItemTemplate>
 
Veep,
Thanks for you help.

I have another question.
Is there any way that I can add values to the dropdownlist without make any connnection? My data is in the text.txt file

the file has some things like:
option1
option2
option3
..

After I used the following code to read, I got the data in the string, how can I put in "private SqlDataReader LoadDropDown()"

Thanks


string fileName = @"" + Server.MapPath("files/auditor.txt");
FileStream fs = new FileStream(fileName,FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string eachLine = "";
while(fs.CanRead)
{
eachLine=sr.ReadLine();
if (eachLine != null)
//ddlAuditors.Items.Add(eachLine.Trim());
else
break;
}
 
Instead of a DataReader you could pump the values into an ArrayList by slightly modifying your existing code and return the ArrayList as the DataSource of the DropDownList.
 
Veep,

How can I make the current item to be selected?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top