3

Here is the code I am using.

foreach (DataRow row in data.Rows)
{
    ListViewItem lst = default(ListViewItem);
    lst = lvw.Items.Add(row(0));  // My error is on row

    for (int i = 1; i <= data.Columns.Count - 1; i++)
    {
        lst.SubItems.Add(row(i));
    }
}

Now, what I'm doing or trying to do is actually add database items onto my listview. However, I can't seem to get my rows onto my list view. I get a syntax error:

'Rows' is a 'variable' but is used like a 'method'.

I have been struggling with this winform for a couple days and my coding has stopped dead at this point. I have tried changing the parenthesis. Here is the whole function. I got it from the internet in vb.net and ran the code it worked. I then configured that code for my application in vb.net that worked too, now after converting to c# and it doesn't work which is my problem. I went through all the code(I am new to c# like relatively) and fixed most of it to work for my application except the row error

private void ShowDataInLvw(DataTable data, ListView lvw)
{       
    lvw.View = View.Details;
    lvw.GridLines = true;
    lvw.Columns.Clear();
    lvw.Items.Clear();
    foreach (DataColumn col in data.Columns)
    {
       lvw.Columns.Add(col.ToString());
    }
    foreach (DataRow row in data.Rows)
    {
        ListViewItem lst = default(ListViewItem);
        lst = lvw.Items.Add(row(0));

        for (int i = 1; i <= data.Columns.Count - 1; i++)
        {
            lst.SubItems.Add(row(i));
        }
    }  
}
Alex R.
  • 4,664
  • 4
  • 30
  • 40
Broken_Code
  • 306
  • 5
  • 19

2 Answers2

2

In C#, to get to an index of a collection, you need to use [ ] (whereas VB uses ( )). Refer to this specifically for DataRows.

Thus, your code should look like:

lst = lvw.Items.Add(row[0]);

And

lst.SubItems.Add(row[i])

On a side note, you may want to read up on the use of the default keyword.

EDIT:

Here's a complete listing (without spoon-feeding much), after noticing that your code won't actually work and assuming you really intend to do as described:

foreach (DataRow row in data.Rows)
{
    ListViewItem lst = default(ListViewItem);

    for (int i = 0; i <= data.Columns.Count - 1; i++)
    {
        if (i == 0)
           lst = new ListViewItem(row[0].ToString());
        else
           lst.SubItems.Add(row[i].ToString());
    }
    lvw.Items.Add(lst);
}

Notice a few things:

  • The instantiation of lst has been changed.
  • The lvw will add the lst along with its subitems.
  • The addition will occur only after the subitems have been created.

Disclaimer: I haven't tested the code, but the idea is there.

Community
  • 1
  • 1
Alex R.
  • 4,664
  • 4
  • 30
  • 40
  • Good description. Always in a hurry, so I answer and dash. :) – Anthony Horne Apr 10 '14 at 08:12
  • I had tried using "[]" and also tried changing the "default" to this code "ListViewItem lst;" which hadn't helped – Broken_Code Apr 10 '14 at 09:38
  • The code uses lvw to configure the listview properties have tried changing it to your code while that has removed my errors how will i be able to config the properties without it and will using lst not cause problems if i do so before its actually declared? – Broken_Code Apr 10 '14 at 10:37
  • 1
    If you really don't get the above working you can always manipulate the incoming datatable, which is most flexible, and then just databind. Good luck. – Anthony Horne Apr 10 '14 at 11:01
  • @Broken_Code What do you mean by "able to config the properties without it"? Anyway, the edited code above should work discretely. If your requirement is something else, please update your question accordingly. – Alex R. Apr 11 '14 at 02:23
  • 1
    The code does work, I just have some weird error with my database connection. So thank you for the help – Broken_Code Apr 11 '14 at 06:39
1

Have you tried:?

lst = lvw.Items.Add(row[0]);

lst.SubItems.Add(row[I]);
Anthony Horne
  • 2,522
  • 2
  • 29
  • 51
  • And yes i have tried both and it hasn't worked with the change. I actually have a new error message after trying that. Can not changed object to string then an invalid argument error for both. – Broken_Code Apr 10 '14 at 09:47
  • What about just binding it: DataTable dt = new DataTable(); ListView ls = new ListView(); ls.DataSource = dt; ls.DataBind(); – Anthony Horne Apr 10 '14 at 09:53
  • lvw.DataSource = data; lvw.DataBind(); – Anthony Horne Apr 10 '14 at 09:55
  • I have not thought of doing that on account to the fact i would like to manipulate my data after i get the whole thing connected and working properly – Broken_Code Apr 10 '14 at 10:31