简体   繁体   中英

System.Windows.Forms.ListViewItem.ListViewSubItem C# Error

for (indx = 0; (indx <= (Dt.Rows.Count - 1)); indx++)

  { ListViewItem lv = new ListViewItem(); lv.Text = Dt.Rows[indx]["ID"]; lv.SubItems.Add(Dt.Rows[indx]["GuestFName"]); lvGuest.Items.Add(lv); } 

So this is my problem. I got squiggly lines under

lv.SubItems.Add(Dt.Rows[indx]["GuestFName"]);

The error says

The best overloaded method match for 'System.Windows.Forms.ListViewItem.ListViewSubItemCollection.Add (System.Windows.Forms.ListViewItem.ListViewSubItem)' has some invalid arguments

What seems to be the problem? :(

ListViewSubItemCollection class has two Add methods - one accepts string another one accepts ListViewSubItem instance. I think you should use first one in this case - just convert row field value to string:

lv.SubItems.Add(Dt.Rows[indx]["GuestFName"].ToString());

NOTE: You can use LINQ to DataTable here:

foreach(var row in Dt.AsEnumerable())
{
    ListViewItem item = new ListViewItem();
    item.Text = row.Field<string>("ID");
    item.SubItems.Add(row.Field<string>("GuestFName"));
    lvGuest.Items.Add(item);    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM