asmgx
asmgx

Reputation: 7984

from dataGridView to ListView

My data is loaded into dataGridView

I have to filter the dataGridView and distinct data then put some of it in a list view

I tried doing that but I am getting error when Adding to the listview

not sure if I am using the right approach?

Here is my code

    var lstPts2 = grdSearch.Rows.Cast<DataGridViewRow>()
                   .Where(x => !x.IsNewRow)                   
                   .Where(x => x.Cells["PtsCode"].Value != null) 
                   .Select(x => (x.Cells["PtsCode"].Value.ToString() , x.Cells["PtsName"].Value.ToString() , x.Cells["DOB"].Value.ToString()))
                   .Distinct()
                   .ToList();
    lbxPts.DataSource = lstPts2;


    listView1.Items.Add(lstPts2);  // <----- Getting Error HERE

Upvotes: 0

Views: 696

Answers (2)

PeterN
PeterN

Reputation: 217

I would create a DataTable for your data in DataGridView. Then you can create a BindingSource to access the data for your DataGridView and ListView. If your data is updated, BindingSource would automatically update every elements with the new data.

hier you can find BindingSource: https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.bindingsource?view=netcore-3.1

short example:

        public static DataTable Ignorieren = GetTable();
        
        static DataTable GetTable()
                {
                    DataTable Ignorieren = new DataTable();
                    Ignorieren.Columns.Add("Date", typeof(DateTime));
                    Ignorieren.Columns.Add("text", typeof(string));
                    
                    return Ignorieren;
                }



//use it in your method
     BindingSource dataSource = new BindingSource();
                dataSource.DataSource = Ignorieren;
    
                DataGridView Table2 = new DataGridView();
                Table2.DataSource = dataSource;
    
                ListView List1 = new ListView();
                List1.DataBindings.Add("Date", dataSource, "Date", true);

Upvotes: 0

Silny ToJa
Silny ToJa

Reputation: 2261

I think you should update List

List<string> l = new List<string>();
l.Add("A");
InitializeComponent();
listView1.ItemsSource = l;
l.Add("B");

listView1.Items have only property Get.

Upvotes: 1

Related Questions