Class collection to listview items - c#

What I'm trying to do is make so it selects the whole transaction when a listview item is selected so I don't have to rebuild it from each of it's string components.
I can do
List<Transaction> Transations = getTransations();
foreach(Transaction T in Transactions ){
string[] row = {T.DatabaseIndex.ToString(), T.TimeRan.ToShortTimeString(), T.MerchantID, T.OperatorID, T.TerminalID, T.AccountNumber, T.ExpDate, T.InvoiceNumber, T.PurchaseAmount, T.AuthorizeAmount, T.AcqRefData, T.RecordNo, T.CardType, T.AuthCode, T.CaptureStatus, T.RefNo, T.ResponseOrigin, T.DSIXReturnCode, T.CmdStatus, T.TextResponse, T.UserTraceData, T.Processor};
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
}
But that doesn't save me any work when I try to retrieve the data when the user picks it.

To be able to use the ListViewItem constructor with a string array for subitem data and actually view your subitems you need to set a details view and define list view columns beforehand.
Here is a running mockup.

Related

Can you take an item from one list, add to another list, then delete item from original list in C#

As the title says, I'm trying to take an item from one list of objects add it to another list and delete the item from the original list without it being deleted from my new list.
This is what I've tried so far:
_ToUpdateQnA.Add(_qnaPair[rowIndex]);
int delIndex = Int32.Parse((e.CommandArgument).ToString());
ListViewDataItem item = (e.CommandSource) as ListViewDataItem;
TextBox deletedQtn = (TextBox)item.FindControl("QuestionTb");
string delQtn = deletedQtn.Text;
_qnaPair[rowIndex].Questions[delIndex].delta_question_description = delQtn;
_qnaPair[rowIndex].DelQuestions.Add(delQtn);
_qnaPair[rowIndex].Questions[delIndex].status = "toDelete";
_qnaPair[rowIndex].Questions.RemoveAt(delIndex);
After this code runs, _ToUpdateQnA keeps the _qnaPair object however, deletes the '_qnaPair[rowIndex].Questions[delIndex]' which has now had it's status updated to "toDelete" which I'd like to remain in the _ToUpdateQnA List.
Questions is a list of objects within a QnAPair object.
Thanks I'm quite new at this.

C# delete tagged Object

I have a question regarding use of "Tag" :
I have a ListBox, or ListView, in which I have the name of my objects, I addes a "Tag" property to find its corresponding object :
foreach(Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
listBoxAss1.Items.Add(op_ass.Nom);
}
Now what I would like, is when I select an item in my list(or several), make an action on corresponding objects. But how can I find them back?
For example I want to remove selected objects from a List, or get the list of Operation ID (without displaying ID in my list).
Looks like you are adding the property, op_ass.Nom into the listbox instead of the ListViewItem, item. Modify your code as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
// Add the list view item instead of op_ass.Nom
listBoxAss1.Items.Add(item);
}
Now you should be able to retrieve the tag from selected item/items as follows:
var operation = ((listBox1.SelectedItem as ListViewItem).Tag) as Operation;
Alternatively, you could think of using data binding as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
}
listBoxAss1.DataSource = ListOpAss1;
listBoxAss1.DisplayMember = "Nom";
And access the data bound object as follows:
var operation = listBox1.SelectedItem as Operation;
using foreach is kind of deprecated you can look into implemented functions in list of objects
ListOpAss1.ForEach(x=>
{
x.getNom(Properties.Settings.Default.Langue);
var item = new ListViewItem(x.Nom);
item.Tag = x;
listBoxAss1.Items.Add(x.Nom);
});
in order to select an item in a list you can use SingleOrDefalt() or Skip(count) take (count) for multiple files or you can run native querys with conditions to search the list like this
var items = collection.Where(x=> x.City == "Burgas").ToList(); //You can use select if you want only certain properties of the object to be selected
///then you can use that new item list to remove the objects from the collection list like this
items.ForEach(x=>
{
collection.Remove(x);
});

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.

UWP - Update listview source dynamic from code behind

I have a group item. Then, each group in group item, i put it into a listview
var Groups = query.GroupBy(query => query.Name);
foreach (var group in Groups)
{
if (group.records.Count() > 2)
{
ListView listview = new ListView();
var itemsource = new ObservableCollection<FileProperties>();
var header = "";
foreach (var item in group.records)
{
header = item.Name;
itemsource.Add(new FileProperties(item.Name, item.Size, item.DateModified, item.Hash, item.Path, item.IsOrigin));
}
listview.ItemsSource = itemsource;
var itemsTemplate = (DataTemplate)this.Resources["Show"];
listview.ItemTemplate = itemsTemplate;
//test is mother listview
test.Items.Add(listview);
}
}
Now, i have a question, how can i update listview UI if i change value in group items without reset mother listview
The default ListView can be grouped by using CollectionViewSource. There is no need to create a child ListView for each group of the parent ListView.
My answer here shows how to create a grouped ListView, you may take a look. Or there are a bunch of demos on internet, you can googling them.
But the most important point here is that by using the default grouped ListView, we can simply create one data collection for the whole ListView, modify the items source collection to update the grouped children automatically, we don't need to create ObservableCollections for each group any more.

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