3

Whenever I type the .DataSource it keeps having red on the bottom or having an error:

'ListView' does not contain a defenition for 'DataSource' and no extension method 'DataSource' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)

public Form4()
{
    InitializeComponent();

    lstProductsChosen.DataSource = products;
}

What should I do? Do I need to add reference?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Stance19
  • 43
  • 6
  • 1
    What is `lstProductsChosen` ? – Anik Islam Abhi Mar 08 '17 at 10:16
  • You should have an error message when you hover over the red line on the bottom. What is it? and what type is `lstProductsChosen` ? It seems that this type does not have a property `DataSource` – Mong Zhu Mar 08 '17 at 10:17
  • the "lstProductsChosen" is the name of my listview – Stance19 Mar 08 '17 at 10:19
  • 1
    are you using WinForms or WPF ? or something else? – Mong Zhu Mar 08 '17 at 10:23
  • When I hover it says "'ListView' does not contain a defenition for 'DataSource' and no extension method 'DataSource' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)" – Stance19 Mar 08 '17 at 10:26
  • It says "form", more likely it's winforms, `ListView` in winforms doesn't have such property (perhaps you create a wrong project, shouldn't it be wpf?). – Sinatr Mar 08 '17 at 10:27
  • Im using Windows Form – Stance19 Mar 08 '17 at 10:27
  • Then why do you expect [System.Windows.Forms.ListView](https://msdn.microsoft.com/en-us/library/system.windows.forms.listview(v=vs.110).aspx) to have `DataSource`. Are you trying some tutorial code or what? – Sinatr Mar 08 '17 at 10:28
  • Im trying to learn this tutorial on youtube. https://www.youtube.com/watch?v=99LWDWmSAqM it's working on him – Stance19 Mar 08 '17 at 10:31
  • It's another control called `ListBox` (not `ListView`) in this tutorial. – Sinatr Mar 08 '17 at 10:33
  • OMG How idiot I am. hahaha Thanks guys love you :) – Stance19 Mar 08 '17 at 10:35

1 Answers1

3

ListView does not have a DataSource property.

ListBox has one. If you use a ListBox instead your code should work.

If you are using WPF you will find a Simple ListView data binding example behind the link.

If you are using WinForms then please check this short and consice answer by Hans Passant

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76