Adding text on specific line on listbox/listview - c#

Is it possible to insert a new item to a listbox at a specific line?
I already tried litsbox1.Items.Add() but this method did not accept any index.

you can try
listBox1.Items.Insert(desiredIndex, yourObject);
Hope this is what you want..

Related

How to add data from one listbox to another listbox?

The following data are displayed in listbox1. I want to pass items from listbox2 to listbox1. The result should be look as shown below (lb1).How can this be achieved?
Many thanks for your help!
var itm = listBox2.Items[0].ToString();
listBox1.Items.Add(itm);
enter image description here
Use AddRange Method. Try like:
listBox1.Items.AddRange(listBox2.Items);
You can use AddRange
listBox1.Items.AddRange(listBox2.Items);
Or if you want an exact copy of what is in the other listbox without your old items.
listBox1.Items = listBox2.Items;

DropDownList Showing First Item Empty

Every time this Dropdown is showing first Item Blank
ComboBox cb;
List<string> namesCollection=new List<string>();
namesCollection.Add("---- Select ----");
namesCollection.Add("ABC1");
namesCollection.Add("ABC2");
namesCollection.Add("ABC3");
namesCollection.Add("ABC4");
foreach(string pname in namesCollection)
cb.Items.Add(pname);
Does anyone have solution for this ?
You appear to be defining the ComboBox right there in your code, so I'll assume it's actually displayed somewhere in your form / window.
It's normal for the ComboBox to display a blank line initially.
Specify the item you want to display, immediately after populating the ComboBox with data:
cb.SelectedIndex = 0;
You probably have a line feed in your items collection. Hard to see. Adding a cb.Items.Clear() before populating is probably a good idea anyway and will get rid of the problem, if you can't locate it.

How to add a Datagridview into a Scatterviewitem?

I am trying to display the contents of different tables dynamically using scatterview, is there an easy way to do that. Right now I have the results of a query in DatagridView and I want to add it to a ScatterViewItem. I tried directly assigning, however I must be doing something wrong here. Do I need to bind it to the xaml code?
dgv = QueryResult();
svi.Content = dgv.DataSource;
The answer for this question was already there, my mistake I did not search that well.
The last post in :
Can I programmatically add a row to a WPF datagrid?
Moderators please flag appropriately.
Thanks

How to delete item from listbox at particular location in C#

How can I delete an item from listbox at particular location and not the SELECTED one?
Let's say if I have 4 items in listbox and I want to delete the one at index 2, how can I do that without selecting it.
ListBox.Items should be a List. More precise a ListBox.ObjectCollection
Have you tried :
myListBox.Items.RemoveAt(2);
RemoveAt refference in MSDN
listBox1.Items.RemoveAt(position);
ListBox1.Items.RemoveAt(anyindex);

Adding a new value to the drop down list box

I have a drop down list box which has one of the values to be others. I want to move these value to the last. Please help me with this. The code i am using is as follows
ddlAssetsCountryOthersone.DataSource = lstIMAssetsByCountryOthers;
ddlAssetsCountryOthersone.DataTextField = "AssetDescription";
ddlAssetsCountryOthersone.DataValueField = "AssetDescription";
ddlAssetsCountryOthersone.DataBind();
ddlAssetsCountryOthersone.Items.Remove(
ddlAssetsCountryOthersone.Items.FindByValue(
Constants.PhilosophyAndEmphasis.Other.ToString()));
How can i add the others back to the drop down list in the last
try this after your databind :
ddlAssetsCountryOthersone.Items.Add(new ListItem("Others", "Others"));
By the way if you use Insert method, you can insert the item you want to the position you want. For example the code below adds the Other option to the 4th order :
ddlAssetsCountryOthersone.Items.Insert(4, new ListItem("Others", "Others"));
you can try like
ListItem li = new ListItem("text", "value");
yourDropdown.Items.Insert(yourDropdown.Items.Count, li);
If you have 5 items in dropdown, it will return 5, since the insert index start from 0
If you the dropdownlist is databound you will not be able to add items to your control after the DataBind() call, and if you add them before they will be cleared anyway whe you call DataBind().
You can use Insert or Add after the data binding takes place, and you can specify the index of the item you're inserting. Insert is used to insert at the specific location, Add will append to the bottom, as in your case:
//after databind
//example with insert - (-1 as list is zero based)
ddlMyList.Items.Insert(noOfItems-1, "Other");
OR
//add
ddlMyList.Items.Add("Other");
Or:
ListItem li = ddlAssetsCountryOthersone.FindByValue(Constants.PhilosophyAndEmphasis.Other.ToString()));
ddlAssetsCountryOthersone.Remove(li);
ddlAssetsCountryOthersone.Add(li);
That should work, please test - and replace Add with Insert as per JohnIdol's suggestion...
When databinding, it is far easier to add/remove items from the list that is being databound than it is to add/remove items after the data binding takes place.
I would create a wrapper around lstIMAssetsByCountryOthers that makes the necessary changes into a new IEnumberable and returns that new object to be databound.

Categories

Resources