See no Items after adding them - c#

My Listview show nothing after adding Items from a List into it. Why?
My List is not empty And my program goes into this loop.
So Is the code wrong for adding items into an listview? Because I saw many chatrooms and also from Microsoft that I can add them like this.
I tried it also with this code:listView1.Items.Add(new ListViewItem { ImageKey = "Person", Text = database1[counter1].name });
Here a picture of my imglist, and the list is chosen at thelistview:
enter image description here

You can't add items if there are no columns, or you can but they wont show up.
Have you added some columns to your listView?
//Adds needed columns
this.listView1.Columns.Add("<column_name>", 50);
If you have one column, you can simply add items by:
ListViewItem itm = new ListViewItem("my_item");
listView1.Items.Add(itm);
If you have multiple columns instead of a string, you can do the same but with a string array where the array size equals to the number of columns.
string[] items = new string[listView1.Columns.Count];
Try this in your code too, in my code only works with this:
this.listView1.View = View.Details;

Related

Fastest way to add to a list elements not in second list

I have 2 listBoxes with BindingLists as their data sources. The idea is to make a List builder (as MSDN names it), where first listBox shows currently added columns and second listBox shows the rest of available columns. First list contains ViewColumn objects, while the other list contains strings.
I load chosen columns into first listBox from database and then I want to load the rest of available columns into the second listBox (the list itself comes from another place in database). Considering there are no limits on the number of columns, I want to do that in the fastest way possible - what would that be?
Here's some code to visualize that:
ViewTable _view;
BindingList<ViewColumn> _viewColumns = new BindingList<ViewColumn>();
BindingList<string> _detailsColumns = new BindingList<string>();
void CustomInitialize()
{
_view = //get view and its columns
_viewColumns = new BindingList<ViewColumn>(_view.Columns);
listBox_CurrentColumns.DataSource = _viewColumns;
listBox_CurrentColumns.DisplayMember = "Name";
var detailsTable = //get the list of available columns
foreach (var row in detailsTable)
{
//TODO: if _viewColumns does not contain this value, add it to _detailsColumns
_detailsColumns.Add(row.ColumnName);
}
listBox_AvailableColumns.DataSource = _detailsColumns;
}
I think you want to do something like:
_detailsColumns = _allColumns.Except(_viewColumns.Select(c => c.Name))
This should get you all entries in the _allColumns collection excluding the entries in the _viewColumns collection.
I assume here that _allColumns contains the overall collection of possible columns.

How to retain listbox selected items values on multiple clicks?

for (int i = 0; i < lboxavilableInsName.Items.Count; i++)
{
if (lboxavilableInsName.Items[i].Selected)
{
if (!arraylist1.Contains(lboxavilableInsName.Items[i]))
{
arraylist1.Add(lboxavilableInsName.Items[i]);
arrUpdatedInsValues.Add(lboxavilableInsName.Items[i].Value);
arrUpdatedInsNames.Add(lboxavilableInsName.Items[i].Text);
}
ViewState["UpdatedInsValues"] = arrUpdatedInsValues;
arrUpdatedInsValuestotal = (ArrayList)ViewState["UpdatedInsValues"];
ViewState["UpdatedInsValues2"] = `enter code here`arrUpdatedInsValuestotal;
ViewState["UpdatedInsNames"] = arrUpdatedInsNames;
}
}
Actually I have given selsectionmode="Multiple" in the listbox. That will select me multiple items when I select first time or subsequent time after page gets loaded, but I want that in a code behind saying ex: if I select 2 items 1st time that 2 items will added in the second listbox and I will get the values of those selected items.
If again I select any items after adding previous selection items in 2nd listbox, I want the item value selected at 2nd time along with first two item values. So totally 3 values I want. and I need to send that values to the stored procedure to insert.
Its because you are assigning new values each time to the 2nd listbox, you don't need to assign them, you need to add values within your 2nd listbox, in this way your 2nd listbox will retain all the previous and the new values as well.
Hope it helps.

Add more items to same Row and same column in listview

I know how to add/delete items but I don't know how to add more items to the same field (same row and same column). I want whenever I click a button, an item is added to same selected row but not to new row in the listView.
I uploaded a photo you can check to see what I exactly mean.
Consider looking at ObjectListView or DataGridView instead of what you are currently. It may be more flexible to your needs.
Your question is somewhat unclear. Clearly you are using listView and you have columns and rows resulting in a cell / box / grid location. I gather that, after its initial creation, you wish to append or alter the data at that location.
To get to the point: Multi-line text within a given 'cell' is not supported (as best I can tell). The picture you have shown is likely a custom object or something similar to a listView, but different (such as a ObjectListView). Or perhaps a picture.
listView2.Items[0].SubItems[4].Text = "123\nabc"; //Doesn't add a proper newline like a normal string
listView2.Items[0].SubItems[4].Text = "123\rabc"; //Doesn't add a proper return carriage like a normal string
listView2.Items[0].SubItems[4].Text = "123\r\nabc"; //Doesn't add a proper newline like a normal string
I am assuming you are using the details view
listView1.View = View.Details;
First adding your headers, listView1.Columns.Add(text, width);
listView1.Columns.Add(First Name", 50);
listView1.Columns.Add("Middle Name", 100);
listView1.Columns.Add("Last Name", 100);
You then add data to the listView. However, this is not done directly. You build a listViewITEM then add that item to the list view.
string[] row = new string[3];
row[0] = "john";
row[1] = "someone";
row[2] = "doe";
ListViewItem lvi = new ListViewItem(row);
listView1.Items.Add(item);
listView1.SelectedItems[#].SubItems[#].Text = "string" + "\n" + "string2";
CrazyPaste suggested adding a row, which could be practical and is something you often see with listViews.
However, If you choose to add or "redo" the rows, be sure to remove any old information before inputting new information to avoid duplicates.
Taken from the popup within visual studio 2013 pro
listView1.Items.RemoveAt(int index)
listView1.Items.Insert(int index, string key, string text, int imageIndex)
OR
listView1.Items.Clear(); //Clears all items
then
//Add populate logic here
Two arrays or a multidimensional array in a loop would be effective if you wish to populate the listview in that manner.
To achieve this programmatically, you could...
listView2 = new ListView();
listView2.View = View.Details;
listView2.Location = new Point(50, 50);
listView2.Size = new Size(400, 100);
this.Controls.Add(listView2);
listView2.Columns.Add("AAA");
listView2.Columns.Add("BBB");
listView2.Columns.Add("CCC");
listView2.Columns.Add("DDD");
listView2.Columns.Add("EEE");
ListViewItem item1 = new ListViewItem();
item1.Text = "0"; //The way to properly set the first piece of a data in a row is with .Text
item1.SubItems.Add("1"); //all other row items are then done with .SubItems
item1.SubItems.Add("2");
item1.SubItems.Add("3");
item1.SubItems.Add("");
item1.SubItems.Add("");
ListViewItem item2 = new ListViewItem();
item2.Text = "00";
item2.SubItems.Add("11");
item2.SubItems.Add("22");
item2.SubItems.Add("33");
item2.SubItems.Add("");
item2.SubItems.Add("");
ListViewItem item3 = new ListViewItem();
item3.Text = "000";
item3.SubItems.Add("111");
item3.SubItems.Add("222");
item3.SubItems.Add("333");
item3.SubItems.Add("");
item3.SubItems.Add("");
//item1.SubItems.Clear();
//item1.SubItems.RemoveAt(1);
listView2.Items.Add(item1);
listView2.Items.Add(item2);
listView2.Items.Add(item3);
//listView2.Items.Insert(2, item1); //0 here is the row. Increasing the number, changes which row you are writing data across
listView2.Items[0].SubItems[4].Text = "123\rabc";
To 'update' the information:
listView1.Items.Clear();
listView1.Items.Add(item1);
listView1.Items.Add(item2);
...etc
NOTES:
I was not able to get .Insert to work with subitems.
If you already inserted a listViewItem, You cannot insert an item
without first removing it
SubItems are not automatically created to fill empty space. Commands like 'listView2.Items[0].SubItems[4].Text' will not work with null/non-existent SubItems
I don't have much to go on. But this adds a new row:
string[] row = { "1", "snack", "2.50" };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
Here's a post discussing how to update an existing listitem:
C#: How do you edit items and subitems in a listview?
Ok. after I searched the internet for ages, it turned out that listView does not support text wrap. so instead I used DataGridView. thank you for your help

C# Combobox move item to bottom of the list

I need to add "Select more..." to the bottom of the combobox items, like it done on SQL 2008 servers selector. Trying like this:
List<string> srvList = new List<string>();
srvList.Add("ff");
srvList.Add("jj");
srvList.Add("pp");
srvList.Add("<Select more...>");
ComboBoxServs.Items.AddRange(srvList.ToArray<String>());
But "Select more..." appears at the top of items.
As MSDN says:
If the Sorted property of the ComboBox is set to true, the items are
inserted into the list alphabetically. Otherwise, the items are
inserted in the order they occur within the array.
Try to set Sorted property to false:
ComboBoxServs.Sorted = false;
List<string> srvList = new List<string>();
srvList.Add("ff");
srvList.Add("jj");
srvList.Add("pp");
srvList.Add("<Select more...>");
ComboBoxServs.Items.AddRange(srvList.ToArray<String>());
You have to use the index of Insert method of Combobox control
myComboBox.Items.Insert(0, "Select more");
hope that help.
you may Refer Here also

How to implement multiple column headers on ListView?

Like on the pic, there is second column header and two subheaders under it ?
For a complete example look at this link this should give you a full working example of what you can do
Add ListView Column and Insert Listview Items
try something like this
The order in which you add values to the array dictates the column they appear under so think of your sub item headings as [0],1 etc.
Here's a code sample:
//In this example an array of three items is added to a three column listview
string[] saLvwItem = new string[2];
foreach (string wholeitem in listofitems)
{
saLvwItem[0] = "Status Message";
saLvwItem[1] = wholeitem;
ListViewItem lvi = new ListViewItem(saLvwItem);
lvwMyListView.Items.Add(lvi);
}
To add SubItems you could also do something like this
lvwMyListView.Items[0].SubItems.Add("John Smith");

Categories

Resources