Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can I sort Listbox(1 and 2) Items then Listbox(2) sorted to be header text of columns in gridview after a button is click.

ListBox1 ListBox2
1 aaa
2 bbb
5 ccc
4 eee
6 ddd
3 fff

GridView (column headertext)

aaa bbb fff eee ccc ddd

hope its clear. thanks to provide code.
Posted
Updated 20-Aug-14 19:07pm
v2
Comments
MONU MITTAL 21-Aug-14 0:33am    
sort() mathod is called on listbox to sort
Member 10943942 21-Aug-14 0:46am    
can you provide what you saying?. I know the sort() thing. but the problem is how?

Firstly short listbox1 and after that short listbox2 and after that combine them.
 
Share this answer
 
Comments
Member 10943942 21-Aug-14 0:46am    
If I will combine them the header would be 1aaa 2bbb . . . please provide code behind
XML
<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            List<ListItem> list = new List<ListItem>(ListBox1.Items.Cast<ListItem>());

            //sort list item alphabetixcally
            listlist = list.OrderBy(x=> x.Text).ToList<ListItem>();

            ListBox1.Items.Clear();
            ListBox1.Items.AddRange(list.ToArray<ListItem>());
        }
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "you selected....<br />";
        Label1.Text += "item: " + ListBox1.SelectedItem.Text;
        Label1.Text += "<br />value: " + ListBox1.SelectedItem.Value;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net listbox sort alphabetically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            asp.net example - listbox sort alphabetically
        </h2>
        <hr width="550" align="left" color="Gainsboro" />
        <asp:Label
            ID="Label1"
            runat="server"
            Text="select an item from ListBox."
            Font-Size="X-Large"
            Width="350"
            >
        </asp:Label>
        <br /><br />
        <asp:ListBox
            ID="ListBox1"
            runat="server"
            AutoPostBack="true"
            Width="350"
            Font-Size="X-Large"
            SelectionMode="Single"
            Height="200"
            OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
            >
            <asp:ListItem Text="Rufous-tailed Plantcutter" Value="1"></asp:ListItem>
            <asp:ListItem Text="Andean C**k-of-the-rock" Value="2"></asp:ListItem>
            <asp:ListItem Text="Yellow-bellied Elaenia" Value="3"></asp:ListItem>
            <asp:ListItem Text="Grey-hooded Flycatcher" Value="4"></asp:ListItem>
            <asp:ListItem Text="Cliff Flycatcher" Value="5"></asp:ListItem>
        </asp:ListBox>
    </div>
    </form>
</body>
</html>





this example may help you
 
Share this answer
 
C#
private void SortListBox()

   {

       ArrayList ListBoxArray = new ArrayList();

       int i = 0;



       while (i < CarNameListBox.Items.Count)

       {

           ListBoxArray.Add(CarNameListBox.Items[i].Value);

           ++i;

       }

       CarNameListBox.Items.Clear();

       ListBoxArray.Sort();

       i = 0;

       while(ListBoxArray.Count > i)

       {

           CarNameListBox.Items.Add(ListBoxArray[i].ToString());

           ++i;

       }

   }



you can also apply this
 
Share this answer
 
Comments
Member 10943942 21-Aug-14 21:00pm    
Thanks. but the question is how to make a sorted list to become headertext of a gridview column

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