0

I need to sort a data table and load that data table values to a combo box.Then I need to add a new row called 'All' and it should appear at the top of the combo box items.Here is my work so far,

DataTable dt = bLStatus.GetStatusDetail();
if (dt != null && dt.Rows.Count > 0)
 {
    dt.DefaultView.Sort= "Description ASC";

    cmbCurrentStatus.DataSource = dt;
    cmbCurrentStatus.ValueMember = "Description";
    cmbCurrentStatus.DisplayMember = "Description";

    DataRow row = dt.NewRow();
    row["Description"] = "All";
    dt.Rows.InsertAt(row, 0);                 

  }             

Problem is Row 'All' gets sorted as well,I need to display the row at top How to solve this? Any help will be appreciated.

USERP
  • 1
  • 3
  • 1
    This isn't Linq. Also the solution is to sort all the rows but with a non-standard sorter (that makes your ALL row special). – Aron Dec 29 '15 at 05:01
  • you can add the row directly to the `cmbCurrentStatus` instead of adding it to the `DataTable`. `dbmCurrentStatus.Items.Insert(0, "All");` – Kien Chu Dec 29 '15 at 05:34

2 Answers2

0

You could try to insert "All" into the combobox at 0th index. The syntax would be like:

Insert(int insertIndex, object insertItem);

An example would be:

cmbCurrentStatus.Items.Insert(0, (new myClass { Description = ""}));

Reference: https://social.msdn.microsoft.com/Forums/en-US/762a58f5-667d-4e97-a107-86312942f966/can-i-add-items-to-a-combobox-at-a-specific-index?forum=wpf

Similar post in SO: add an item to combobox before bind data from data base

Let me know if it doesn't work.

Community
  • 1
  • 1
Mahe
  • 1,372
  • 2
  • 12
  • 23
  • I added added the row 'All' to the combobox like showed here but it didn't work either. – USERP Dec 31 '15 at 03:40
  • @userp can u please post your updated code? Is it sorting the new entry as well or is it not working at all ?? – Mahe Dec 31 '15 at 04:08
0

I answered my question I just needed to copy my Sorted defaultView to another new datatable and add row 'All' to that datatable and bind it to the combo box. Like below,

   DataTable dt = bLStatus.GetStatusDetail();
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.DefaultView.Sort = "Description ASC";
                DataTable dt2 = dt.DefaultView.ToTable();

                DataRow row1 = dt2.NewRow();
                row1["Description"] = "All";
                dt2.Rows.InsertAt(row1, 0);

                cmbCurrentStatus.DataSource = dt2;
                cmbCurrentStatus.ValueMember = "Description";
                cmbCurrentStatus.DisplayMember = "Description";

            }
USERP
  • 1
  • 3