Saturday, March 24, 2012

loop through dropdownlist items

Hallo,

I already post a similar request but I couldn't not find a solution. Here the problem: I have a dropdownlist bound to a stored procedure. I would like to apply some changes to the dropdownlist items (in the code below I apply a strreverse function) before they are displayed in the control. I tried with the code below: binding the dropdown to a oledbdatareader and looping through its items with a function. But of course it doesn't work. Is there a sintax mistake or it is the general idea that is wrong? thanks

with objCmd_archive
.Connection = objConnect
.CommandType = CommandType.StoredProcedure
.CommandText = "Query1"
.Connection.Open()
objDataReader = .ExecuteReader()
while objDataReader.read()
ConvertDate(objDataReader("mes_monthyear"))
end while
cbo_archive.Datasource = objDataReader
cbo_archive.DataBind()
.Connection.Close()

End with

End Sub

Private function ConvertDate(mes_monthyear as string) as string
mes_monthyear = StrReverse(mes_monthyear)
'mes_monthyear = InStr(1, mes_monthyear, " ", CompareMethod.Text)
return mes_monthyear
End function

Hi,

not sure how much help this will be, so I hope someone can help more; but -

I am surprised you are not getting an error; you are calling a function and returning a value without assigning it to anything (may be allowed under .net though: perhaps it assumes that it should use an open object)

You then bind the original dataset (objDataReader) to the list; so I would expect that the original list is written.

I'm not sure it is possible to alter values in an active dataset: ie
objDataReader("mes_monthyear") = ConvertDate(objDataReader("mes_monthyear"))
but give it a try.

The only other, way I expect this could work (but being new to .net; .net may be cleverer than I give it credit for) would be to read objDataReader in to a custom (offline?) dataset converting on the way and then bind that dataset to the list.

cheers

Kevin


i use c# script:

sqlCommand1.Connection.Open();
my_reader = sqlCommand1.ExecuteReader();

while(my_reader.Read()) drop1.additem(i++);


First bind the dropdown from Dataset, then loop through the entire items of dropdown like this

sqlDataAdapter1.Fill(dataSet11);

DropDownList1.DataBind();

foreach(ListItem ltItemin DropDownList1.Items)

{

//Here u can make changes to the individual item in the dropdown by changing the ltItem value and

//text property and assigning to the same property like this:

ltItem.Text=/Applyconvertion/Convert.ToString(ltItem.Text);
ltItem.Value=/Applyconvertion/Convert.ToString(ltItem.Value);
}

and then call try to call the function that access dropdown list items.I think this will work. Sorry abt the C# code as i dont know the correct syntax of VB but i think u will get to my point. On more thing dont forget to use

using System.Collections;

As foreach loop requires this.


You're still binding the datareader to the dropdownlist and I got the feeling, didn't check it though, that the returned value of your custom function is discarded.

You must loop over the datareader but add the items to the dropdownlist yourself like this:

ddl.Items.Add(ConvertDate(objDataReader("mes_monthyear")))

and then comment out the code right behind it (the .datasource= and .databind)

Grz, Kris.


jupi! that was the trick. Thanks a lot Kris


I wonder if it would be better for performance if you did *not* loop through the datareader, i.e. bind to the DDL, get rid of the DR, and then loop through the items collection changing what you want to change.

Just a thought...

0 comments:

Post a Comment