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

EveryOne

I have a DropDownList ,On a Delete button click the numbers from the dropdown get deleted and and on Add Button click numbers are added to the dropdown.

Now if have 1, 2, 3 in my dropdown and if I delete 1, I want the number 2 and 3 to be replaced by 1 and 2. I am not sure how to do this. Can someone please help me?
Posted
Updated 6-Aug-12 1:27am
v2
Comments
OriginalGriff 6-Aug-12 7:32am    
The question is: Why?
Think about it from the POV of the user: he just pressed the delete key to get rid of number 1 and 3 vanished instead! I want 3 back, and 1 to go! Should I delete 1 again? Does it matter, What happened to 3 anyway?

It's visually confusing - what are you trying to achieve that you think this is a good idea?
[no name] 6-Aug-12 7:38am    
My dropdown values are like this 1,2,3,4..
if i delete 2 ,then my dropdown values are like this 1,3,4.....Once observe this numbering formate is not in proper way ,so i want to rearrange the num's like this 1,2,3...How to do this plz give any suggestion.
OriginalGriff 6-Aug-12 7:42am    
Yes - exactly.
Think about it.
Your drop down list has 1, 2 and 3.
You select 2, and press delete. 2 is still there. What are you going to do now? Answer: press delete again, and get annoyed.
Either don't renumber them, so the user can see that his input has had an effect, or don't use numbers at all. Use something that means something to the user, and reflects what he is doing as a task rather than arbitrary numbers.
[no name] 6-Aug-12 8:01am    
yes ur correct,

but my requirement is like that only that's way i do that format..
could u expalin it in example plz
Vani Kulkarni 6-Aug-12 7:39am    
True. No user would like such a confusing solution.

Hi,
First get the datasouce of your dropdownlist, sort it and set the datasouce again to the dropdownlist.
Try this:
C#
DataTable dt = (DataTable)DropDownList1.DataSource;
//Sort your datatable here.
dt.DefaultView.Sort = "Parameter_Name ASC";
dt = dt.DefaultView.ToTable();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Description";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind();




--Amit
 
Share this answer
 
v2
Comments
[no name] 6-Aug-12 7:55am    
I do like this in my DataBound event


DataTable dt = ViewState["x"] as DataTable;


DataView dv = dt.DefaultView;

dv.RowFilter = "Type=2";

DropDownList ddlSort = (DropDownList)e.Row.Cells[2].FindControl("ddlSort");



ddlSort.DataSource = dv;

ddlSort.DataTextField = "Sort";
ddlSort.DataValueField = "Sort";
_Amy 6-Aug-12 7:59am    
Try my answer. Update the column first.
In this answer DataTextField and DataValueField both are same... Little confusing...
 
Share this answer
 
Comments
[no name] 6-Aug-12 7:56am    
In that i'm not getting my requirement exactly could u plz expalin it in any example
Unareshraju 6-Aug-12 8:39am    
first u have to add some items.
after that in delete button write like this
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
[no name] 6-Aug-12 23:40pm    
for Deleting purpose i wrote SP
Unareshraju 7-Aug-12 0:33am    
No need ,because of dropdown list items did not come from database right?

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