How to append selected items in WPF c# listbox? - c#

I want to append all the selected items in listbox. When i am trying the following code i got "System.Data.DataRowView\r\nSystem.Data.DataRowView\r\nSystem.Data.DataRowView\r\nSystem.Data.DataRowView\r\n" How to get the actual data?
StringBuilder sb = new StringBuilder();
foreach (object o in lstBoxRoles.SelectedItems)
{
sb.AppendLine(o.ToString());
}
string s = sb.ToString();

According to comments: You use DataTable and DataRow to present data in listbox (I think it is a DataGrid or ListView btw).
As you see, Selected item contains the whole View of Row.
If your stuff - DataRow - has ToString() implementation, modify your code:
sb.AppendLine(((DataRowView)o).Row.ToString());
If you need a specific column to append:
sb.AppendLine(((DataRowView)o).Row["SpecColumnName"].ToString());

Related

C# WPF get listview value from multiple selected row

im using WPF in c#.
How to get all the value of the row selected from column M.
Image of listview
From ListView.SelectedItems Property,
foreach (var item in myListView.SelectedItems)
{
//do something with the values, maybe add to a list?
DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();
}
Basically replace "NameAndScore" by your column name.
Reference: Why I get “System.Data.DataRowView” instead of real values in my Listbox?

How to delete multiple rows in a table that are selected in a listbox?

I have a table which's content is displayed at listbox. I want to delete rows that user has selected. How to know which rows to delete and how delete?
This is how I display listbox items:
DataSet AllPairs = new DataSet();
AllPairs.ReadXml(PathToPairsXML);
listBox1.DataSource = AllPairs.Tables[0];
listBox1.ValueMember = "PAIR_text";
listBox1.DisplayMember = "PAIR_text";
Listbox selection property is MultiExtended.
When you use DataTable as DataSource of your ListBox, items are of type DataRowView, so you can use Delete method of DataRowView or using Row property of DataRowView access the row behind that data row view and remove that row from your DataTable.
You can use this code:
this.listBox1.SelectedItems.Cast<DataRowView>()
.ToList()
.ForEach(item =>
{
//item.Delete();
//or
this.AllPairs.Tables[0].Rows.Remove(item.Row);
});
string selectedtext= "";
foreach (int i in listBox1 .SelectedIndices )
{
selectedtext= listBox1.Items[i].ToString ();
DataRow[] drCollection=AllPairs.Tables[0].Select("PAIR_text='"+selectedtext+"'");
if (drCollection.Length>0)
AllPairs.Tables[0].Rows.Remove(drCollection[0]);
}
This is so far the simplest way:
var selectedRows = listBox1.SelectedItems.Cast<DataRowView>().ToList();
foreach (var dr in selectedRows)
dr.Delete();
A small explanation. I'll be using the concrete types instead of var keyword for better understanding. The following line
listBox1.DataSource = AllPairs.Tables[0];
in equivalent to something like this
DataView dataView = AllPairs.Tables[0].DefaultView;
for (int i = 0; i < dataView.Count; i++)
{
DataRowView dataRowView = dataView[i];
listBox1.Items.Add(dataRowView);
}
Now when we know the list box contains DataRowView objects, although the SelectedItems returns items of type object, we can safely cast them back to a DataRowView and call Delete method.
But why we need ToList call? Because when we delete a row, the data source will send a notification to the list box and it will remove that item from the Items and SelectedItems. While it's not required, depending of the internal implementation this could destroy the selection. So in order to be absolutely sure that this will not happen, we buffer the initial selection in a list before proceeding with delete.

Table content to list view

Hello i have got an problem i need to show the contents of an database table the name is Employees.
it needs to get shown in an list view or something simular.
i cant get the listview box to show the table contents.
I think following code should work for you:
private void BindTable(DataTable table)
{
lvItemList.Clear();
foreach (DataRow row in table.Rows)
{
ListViewItem lvItem = new ListViewItem();
lvItem.Text = row["ColumnName"].ToString();
lvItemList.Items.Add(lvItem);
// Or in a one-liner
//lvItemList.Items.Add(row["ColumnName"].ToString();
}
}
You need to override the toString() method and provide the string data you want to display.

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");

Select the specific column of ListView and print it in a new messagebox in C#.net

I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}

Categories

Resources