0

Here is part of code:

private void load_lv() {
    lv_ss.Items.Clear();
    lv_ss.Items.Add(supp.lv_standstill((optionsloaded) ? cbx_options.SelectedIndex : 0));
}

lv_ss is a Listview.

I want to add items to lv_ss using another method:

Here is another part of the code:

public ListViewItem lv_standstill(int option) {
    string[] row = new string[6];
    ListViewItem lv_item = new ListViewItem();

    //not important part of code
    //loading data from sql etc to row[]

    if (first) {
        lv_item = new ListViewItem(row);
        first = false;
    } else {
        lv_item.SubItems.AddRange(row);
    }

}

return lv_item;
} 

Every new item of lv_item contains a row. The problem is to move all ROWS to lv_ss listview. When I use lv_ss.Items.Add it puts only first row.

I've tried with ListViewItem.ListViewSubItemCollection .. and then foreach subitem. But it reads a single string from a row, not a whole row.

Not sure how to do that. I mean, of course, I can put whole code in -> private void load_lv(){} But I'm stubborn :D Any help or ideas?

Long Luong
  • 764
  • 2
  • 14
  • 28
rsj
  • 41
  • 5
  • Not sure what the issue is. I would remove the `first`flag and replace it by: `lv_item = new ListViewItem(row.First()); lv_item.SubItems.AddRange(row.Skip(1).ToArray());`. To get all the other rows you obviously need to loop over either a DataTable or until a Reader is done.. - Don't forget to add the item: `listView1.Items.Add(lv_item);` and to add your columns to the LV. – TaW Aug 08 '20 at 14:16
  • `public ListViewItem lv_standstill(int option){}` is ok. Reader get through all lines and every rows are inserted into `lv_item`. The problem is when i return `lv_item` to `load_lv()`. `lv_ss.Items.Add` insert only first row of `lv_items`. – rsj Aug 08 '20 at 16:19
  • `lv_ss.Items.Add(supp.lv_standstill(` adds __one__ Item with several fields/columns in the subitems/columns. If you want one item per row it would belong in a loop, no? – TaW Aug 08 '20 at 19:25
  • Loop. Yes.But what would it look like ? – rsj Aug 08 '20 at 22:56
  • As the SQL stuff prepares reading all rows it must happen outside the loop: `someSqlDataReader myReader; myReader = myCommand.ExecuteReader(); while (myReader.Read()) { processRowAndAddToLV(myReader,lv_ss );}` – TaW Aug 09 '20 at 04:23
  • I think we don't understand each other. Maybe I can't pinpoint where the problem is. `public ListViewItem lv_standstill(int option) {...}` is a method, that using sql and reader returns ListViewItem to `lv_ss`. When i use `lv_ss.Items.Add(thismentionedabovemethod)` it adds only first row of items to ListView. How can i add other elements (rows) to `lv_ss` ? :) – rsj Aug 09 '20 at 07:56
  • I think I understood that but I told you that this is the issue. You need to set up the reader outside of a loop. Look at the signature I proposed: I pass the reader into the method that creates the lvi. You really shouldn't do sepearate sql accesses in the method! - The priciple is called: Separation of responsibilities. Your method should only turn data into a lvi. reading the data is some other method's job. If sql were any good at doing indexed reads one could pass in the rowindex to read and slap both jobs together; but that is neither possible nor a really good idea.. – TaW Aug 09 '20 at 08:52
  • Ok, i moved content of `ListViewItem lv_standstill` into `load_lv()` method. I have all datasources of listboxes or comboboxes in separate methods. I thought with the ListView data, I could do it as well. Unfortunately, I must give up. Thx for help – rsj Aug 10 '20 at 05:02
  • Ah, DataSources, ie DataBinding: LV's Items are __jagged arrays__ and hence __don't [bind](https://stackoverflow.com/questions/2799017/is-it-possible-to-bind-a-list-to-a-listview-in-winforms).__ So you need a little code for each row. – TaW Aug 10 '20 at 06:50

0 Answers0