Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following code to sort data before binding a dropdownlist. What happens is that 'SELECT' is also getting sorted. How to avoid this and retain the position of select in the first place.
C#
adds1.Add("SELECT");
        for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
        {
            adds1.Add(ds1.Tables[0].Rows[i].ItemArray[0].ToString().ToLower());
            name1 = ds1.Tables[0].Rows[i]["block_name"].ToString().ToLower();
        }
        adds1.Sort((x, y) => string.Compare(x, y));
        DropDownList7.DataSource = adds1;
        DropDownList7.DataBind();
Posted

I assume adds1 is a List<t> of some sort, so you can use adds1.Insert(0, "SELECT") after you sort.
This will add the "SELECT" at location 0 of the list (top of the list)
 
Share this answer
 
Comments
Zafar Sultan 28-Dec-12 4:35am    
Shahare, you beaten me by few seconds :)
Shahare 28-Dec-12 14:59pm    
:)
S.Rajendran from Coimbatore 28-Dec-12 4:48am    
Thank you,Zafar. You gave me a good clue. I had to add 'adds1.Remove("SELECT")' before 'adds1.Insert(0,"SELECT")'. It works.
Rajendran.
Zafar Sultan 28-Dec-12 4:53am    
Welcome.
Change your code. Write:

C#
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
adds1.Add(ds1.Tables[0].Rows[i].ItemArray[0].ToString().ToLower());
name1 = ds1.Tables[0].Rows[i]["block_name"].ToString().ToLower();
}
adds1.Sort((x, y) => string.Compare(x, y));
DropDownList7.DataSource = adds1;
DropDownList7.DataBind();

ListItem li = new ListItem();
li.Text = "Select";
li.Value = "0";
DropDownList7.Items.Insert(0,li);
DropDownList7.SelectedIndex = 0;


This way you are adding all the sorted items to your dropdownlist and then inserting a new item "Select" at first position.
 
Share this answer
 
Try:
C#
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
    adds1.Add(ds1.Tables[0].Rows[i].ItemArray[0].ToString().ToLower());
    name1 = ds1.Tables[0].Rows[i]["block_name"].ToString().ToLower();
}
adds1.Sort((x, y) => string.Compare(x, y));
adds1.Insert(0, "SELECT");
DropDownList7.DataSource = adds1;
DropDownList7.DataBind();


Refer: MSDN: List<t>.Insert Method[^]
 
Share this answer
 
v2
You need to add the select after you sort.Assuming adds1 is a List of some kind:


C#
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
    adds1.Add(ds1.Tables[0].Rows[i].ItemArray[0].ToString().ToLower());
    name1 = ds1.Tables[0].Rows[i]["block_name"].ToString().ToLower();
}
adds1.Sort((x, y) => string.Compare(x, y));
adds1.Insert(0, "SELECT");
DropDownList7.DataSource = adds1;
DropDownList7.DataBind();


Note that you need to use Insert rather than Add to specify where you want "Select" to go. If add1 is an array or collection that doesn't support Insert, the problem is very slightly harder. In this case you should copy the items from the dataset into a new variable (e.g. tempAdds), sort tempAdds
add "Select" to adds1, then copy the order values from tempAdds.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900