Hack Forums
[C#] How to associate the result of a LINQ query with the dataSource of a dataGridView - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] How to associate the result of a LINQ query with the dataSource of a dataGridView (/showthread.php?tid=6097462)



How to associate the result of a LINQ query with the dataSource of a dataGridView - Nemo2478 - Jul 24, 2020

Hello everybody,

I have a dataGridView with the result of a query, this dataGridView allows me to select the records I want:

Code
private void button1_Click(object sender, EventArgs e)
{

    var values =
        from DataGridViewRow row in ewgrid1.Rows
        where Convert.ToBoolean(row.Cells["colCheck"].Value) == true
        select new {id = row.Cells[1],name=row.Cells[2] };

    frmResult formResultat = new frmResult();
    formResultat.data = values.ToList();
    
    formResultat.Show();

}

The idea is to associate the result of this query in the grid of the frmResult form, but I don't know with what type of data to collect and associate it to the dataSource of the dataGridView.

The frmResult:
Code
public partial class frmResult : Form
    {
        public object data { get; set; }
        public frmResult()
        {
            InitializeComponent();
        }

        private void frmResult_Load(object sender, EventArgs e)
        {
            ewgrid1.DataSource = data;
        }
    }

Waiting for the result of the Select as a generic object type as it is in the code is the closest I've ever been to working, but I don't know what data type to put the result in to make it work.

Can you please help me?


Regards and thanks


RE: How to associate the result of a LINQ query with the dataSource of a dataGridView - glitchgxd - Jul 25, 2020

Can't you just set the RowFilter to something like-

Code
dataView.RowFilter = "colCheck = true";

Instead of building out that linq query?