0

Hi I was wondering if there was anyway to select and highlight new data automatically in listview. Currently I can load data into the listview from an ERP using their API acces token. Select manually and Save to my database.

What I want to do is following:

  1. When the form loads, list everything from the EPR. This is accomplished.

  2. Select a row in the listview then press a button to save into database. This is accomplished.

  3. Instead of selecting a single row manually, select the new users added. This is where I have issues.

I do not know if it is even possible what i am trying to do. I have tried to do is select all and save everything into the database. This is possible but the Database will keep getting duplicates every time the form is loaded.

foreach (ListViewItem item in listView1.Items)
        {
            item.Selected = true;
            //This method inserts each row(item) in the database
            updateAll(object sender, EventArgs e);

        }

I have selected all items to true and called updateAll(object sender, EventArgs e). The updateAll(object sender, EventArgs e) will select the first row, update it and put it in the database. How do I ensure that updateAll(object sender, EventArgs e) goes to the next item in the row and invokes updateAll(object sender, EventArgs e) on that?

My method works for single selected item and does what I want though just can't get it to work for all the items in the listview.

SoA
  • 15
  • 1
  • 5
  • 2
    yes its possible, but what have you tried so far? can you show us what you have tried in code – Simon Price Nov 01 '16 at 09:53
  • Well your for statement seems to be inside the loop, isn't it easier to keep it outside of the loop. I can imagine now that your `updateAll()` method checks the selected items, selects the first, saves the first, then selects the second, saves the first and the second, and so on. If you would keep your updateall outside of the loop, then you could select everything at once and then it would save everything only once :) – Icepickle Nov 01 '16 at 09:57
  • @SimonPrice hi, im not sure what you what you want me to show exactly please be specific. Il show you my LoadAccounts when i'm done editing if that is what you are asking for. – SoA Nov 01 '16 at 09:57
  • @Icepickle Hi thanks for help. If I keep it outside the loop true it saves it one time true. The issue is there is no way to distinct new user with the ones in the listview already so I have to run updateAll() on all of them which will give me duplicates in the Database. I'm looking for some constraint that checks if its a new user if its even possible. – SoA Nov 01 '16 at 10:09
  • Where is the code that saves to your database? Do you mean to compare the items loaded from the ERP site to the items already in your database and then only add the ones that do not exist? If so, you can use LINQ to get the items in ERPSiteApi.GetAllAccounts() returned list (I assume it's a list of some sort) that are not in the list of items already in the database: http://stackoverflow.com/questions/3944803/use-linq-to-get-items-in-one-list-that-are-not-in-another-list – o_weisman Nov 01 '16 at 10:17
  • @o_weisman Honestly that is not what i'm asking. The question is if there is a function or way to select and highlight new items loaded with LoadAccount(). The rest with database or saving is done. To make it clear for everyone: I can select a row manually and save them. I can select all rows and save. I want to select the latest user added and save them. I could not find a way to distinct them and need help with that – SoA Nov 01 '16 at 10:28
  • What is LoadAccount() ? Is it your own function? My suggestion still stands. Take a snapshot of the list before calling LoadAccount(), compare that snapshot to the list after LoadAccount() using LINQ (or manually) to find out which items are in the current list and not in the snapshot and then set their Selected property to true. – o_weisman Nov 02 '16 at 08:00
  • @Yes LoadAccounts() is my own function to load accounts fetched from ERP API into the listview. Currently I can select a row and save the account from listview into the database. What I want is to select all and save all at once. Well I have selected all and when I invoke my save function saveIntoDatabase(sender, e) it only saves the first row. – SoA Nov 02 '16 at 08:06
  • That's not the issue you were asking help with earlier. How can we know why your saveIntoDatabase function doesn't work properly without seeing it? Have you tried debugging it at all? – o_weisman Nov 02 '16 at 08:51
  • @o_weisman I specifically said numerous times that I can save to the database for single row. The function works for one row and the possible solution I came up with after this post to is select all rows in a loop and apply SaveIntoDatabase(sender, e) button to all of them, I do not know how to make the button save the next row and next and so on, it only saves the first row. If there is no way to keep clicking from first row, to second row and so on in a loop then I can just stop and try something completely different. Is this possible and can you possibly supply a small general example? – SoA Nov 02 '16 at 09:05
  • @o_weisman Do not get me wrong with my comment about the LINQ approach I really appreciate your time, thought and participation. – SoA Nov 02 '16 at 09:07
  • I'm sorry, I feel like I'm wasting my time here, as you don't seem to want to cooperate with people who are trying to help you. `updateAll(object sender, EventArgs e)` is most definitely not part of a code that works. You have not shown SaveIntoDatabase or explained if that is your own code that you can change or not. How do you invoke saveIntoDatabase (which is clearly an event handler)? By pressing a button? Unless you answer all of these question, I don't see how anyone can help you. P.S it wouldn't be a bad idea to be more polite when you're asking for help. – o_weisman Nov 03 '16 at 12:33
  • It was not my intention to be rude the LINQ, it just did not have much to do with my problem. The core of my question was very general. I have solved a base case of the problem with is Select a row, Save. What I wanted was Select All, Save All. A loop is needed for that, Select All working but Save All does not look past the first row. It does not increment with the loop, it keeps saving the first row. How do you increment buttonclick as the loop iterates? – SoA Nov 03 '16 at 13:00
  • You don't "increment" the button click. You change the event handler or write another one if you can't change it. – o_weisman Nov 04 '16 at 09:54
  • @o_weisman Much appreciated exactly what I was looking for, wish you would submit answer so I can upvote it. – SoA Nov 04 '16 at 10:20

0 Answers0