473,394 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Move items in ComboBox

BRawn
28
Hi guys,

I have a data-bound ComboBox and I populate a DataSet with the values returned from a SQL query.

Expand|Select|Wrap|Line Numbers
  1.  
  2.         public DataSet CompanyArray(DataSet dataSet)
  3.         {
  4.             SqlConnection connection = InstancesTest.IDataInterface.DefineConnection();
  5.  
  6.             SqlCommand command = new SqlCommand();
  7.             command.CommandText = "usp_CompanyArray";
  8.             command.CommandTimeout = 20000;
  9.             command.CommandType = CommandType.StoredProcedure;
  10.             command.Connection = connection;
  11.  
  12.             try
  13.             {
  14.                 if (connection.State != ConnectionState.Open)
  15.                     connection.Open();
  16.  
  17.                 SqlDataReader reader = command.ExecuteReader();
  18.                 DataTable dt = new DataTable();
  19.                 dt.Load(reader);
  20.                 DataRow row = dt.NewRow();
  21.                 row["COMPANY_NAME"] = "Please Select";
  22.                 dt.Rows.InsertAt(row, 0);
  23.                 dataSet.Tables.Add(dt);
  24.                 reader.Close();
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                 MessageBox.Show(ex.Message);
  29.             }
  30.             finally
  31.             {
  32.                 if (connection.State != ConnectionState.Closed)
  33.                     connection.Close();
  34.             }
  35.  
  36.             return dataSet;
  37.         }
  38.  
When I call this method, I take the values in the DataSet, build an array and populate the ComboBox.

Expand|Select|Wrap|Line Numbers
  1.  
  2.         void CompanyArray()
  3.         {
  4.             DataSet ds = new DataSet();
  5.             ds = logic.CompanyArray(ds);
  6.             string[] array = new string[ds.Tables[0].Rows.Count];
  7.  
  8.             int counter = 0;
  9.             foreach (DataRow dr in ds.Tables[0].Rows)
  10.             {
  11.                 array[counter] = dr["COMPANY_NAME"].ToString().Trim();
  12.                 counter++;
  13.             }
  14.  
  15.             Array.Sort(array);
  16.  
  17.             foreach (string companyName in array)
  18.             {
  19.                 cmbCompanySelect.Items.Add(companyName.ToString().Trim());
  20.             }
  21.  
  22.             //cmbCompanySelect.SelectedItem = "Please Select";
  23.             int i = cmbCompanySelect.Items.IndexOf("Please Select");
  24.         }
  25.  
All this works fine. The reason I populated it in an array was so that I could sort the items alphabetically. I know I could just do an "order by" in SQL but I chose this route.

What I'm after is putting a "Please Select" at index 0 in the ComboBox. How can I move this value to the top of the list, without reordering the sorted list?
And I don't really want to insert a "Please Select" as a first row in a database table. Any ideas?

Thanks
May 17 '12 #1

✓ answered by firexfighterx

After doing your sort of the elements of the combo box insert the Please Select element at the 0 position.

2 3772
PsychoCoder
465 Expert Mod 256MB
Try this:

Expand|Select|Wrap|Line Numbers
  1. cmbCompanySelect.Items.Insert(0,"Please Select");
May 19 '12 #2
After doing your sort of the elements of the combo box insert the Please Select element at the 0 position.
May 21 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Job Lot | last post by:
I have worked out how to move items in ListBox when it is populated using Items collection, but it doesn't seems to work when the ListBox is bound to a DataSource. How can i move items when...
3
by: Progalex | last post by:
Hi! I have this question: I have a listview which gets populated with some file names. At the moment I have implemented d&d, but I'd like also to be able to move up and down items inside my...
2
by: vijay | last post by:
Hi, Can we do something like we select nodes from one tree and when we press a button like "Add" It will move that node with its parent node to the tree to the right? Is this possible in...
5
lwwhite
by: lwwhite | last post by:
I'm trying to reproduce functionality I've seen in some other (non-Access) applications. On one of my forms, the master record is a document. The subform consists of a datasheet list of the topics...
3
by: Brian Simmons | last post by:
Hi All, I search on codeproject and google but didn't find what I was looking for. Anybody know of a good implementation where you have 2 listboxes and you want to move items between the 2. ...
4
by: hhak | last post by:
Hi all, Im pretty new to C#, im trying to assign a combobox selected value to a int variable. int number; combobox has items added (1,2,3,4,...9) when i select a number from the combobox i...
1
by: JAnn81 | last post by:
I have a combo box created using xaml. I bind the item source to a xml document. <Items> <Item> <Name>David</Name> <Age>20</Age> </Item> <Item> <Name>Marcus</Name> <Age>25</Age>
4
by: bogdan | last post by:
Hi, I have two listboxes on a page and need to move items between them - using buttons (e.g. "<<" ">>"). Can this be done on a client side in asp.net? I'd like to avoid hitting the server on...
5
by: amandab116 | last post by:
I'm having a hard time figuring out how to move items between a left and right listbox server-side. I have a populated listbox from the database on the left and I'm trying to add items to the box...
1
by: sanndeb | last post by:
I have a combobox like <ComboBox Height="23" HorizontalAlignment="Left" Margin="88,13,0,0" Name="cmbYears" VerticalAlignment="Top" Width="112" SelectionChanged="cmbYears_SelectionChanged"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.