none
Facing problem to fetch data from data table RRS feed

  • Question

  • i convert my datagridview to datatable and when i issue a linq on that datatable to fetch a data from a particular column then i am not getting data. see the code.

                    DataTable dtMappedSectionNotEmpty = dgvLineMatch.ToDataTable("passed")
                        .AsEnumerable()
                        .Where(x => !string.IsNullOrEmpty(x.Field<string>("Matched Section")))
                        .Select(r => new { Col = r.Field<string>("2018 FY NEW") }).ToList().ToDataTable();

    this line convert my datagridview to datatable dgvLineMatch.ToDataTable("passed") dgvLineMatch is my datagridview

    see a screen shot of my data table which is converted from datagridview.

    see in screen shot there is a column called 2018 FY NEW in my data table but when i try to fetch data from my datatable by LINQ then i am not getting data for column 2018 FY NEW. rather getting empty data. please tell me what to rectify in my code. thanks

    Thursday, January 16, 2020 3:51 PM

Answers

  • In general it's better to have column names without spaces. If you can't change them try using 

    .Select(r => new { Col = r.Field<string>("[2018 FY NEW]") }).ToList().ToDataTable();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Sudip_inn Friday, January 17, 2020 1:33 PM
    Thursday, January 16, 2020 4:01 PM
    Moderator
  • Hi Sudip_inn,
    Based on your code, I can't use ToDataTable function.
    So I convert my datagridview to datatable by using the following code.

    DataTable dt1 = new DataTable();
    private void button1_Click(object sender, EventArgs e)
    {
        //Adding the Columns.
        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {
            dt1.Columns.Add(column.HeaderText, column.ValueType);
        }
        //Adding the Rows.
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            dt1.Rows.Add();
            foreach (DataGridViewCell cell in row.Cells)
            {
                dt1.Rows[dt1.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();
            }
        }
    }

    Then I use a linq on dt1 to fetch a data from a particular column(“2018 FY NEW”).
    Here is a code example you can refer to.

    DataTable newDataTable = new DataTable();
    private void button2_Click(object sender, EventArgs e)
    {
        newDataTable.Columns.Add("2018 FY NEW", typeof(string));
        var rowColl = dt1.AsEnumerable();
        List<string> lstResult = (from r in rowColl
                        where r.Field<int>("Id") >=1
                        select r.Field<string>("2018 FY NEW")).ToList();
            foreach (string l in lstResult) {
                newDataTable.Rows.Add(l);
            }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 17, 2020 6:46 AM

All replies

  • In general it's better to have column names without spaces. If you can't change them try using 

    .Select(r => new { Col = r.Field<string>("[2018 FY NEW]") }).ToList().ToDataTable();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Sudip_inn Friday, January 17, 2020 1:33 PM
    Thursday, January 16, 2020 4:01 PM
    Moderator
  • Are you sure that your ToDataTable function is actually working? Put that result into a temp variable. Then view the temp variable in the DataSet visualizer in the debugger. Is the data actually there in the correct column? If so then the issue is with your LINQ so add the Where clause filtering and look at the results again. Is the data gone? If so then the issue is with the where clause otherwise add the select last and see what you get left.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, January 16, 2020 4:20 PM
    Moderator
  • Hi Sudip_inn,
    Based on your code, I can't use ToDataTable function.
    So I convert my datagridview to datatable by using the following code.

    DataTable dt1 = new DataTable();
    private void button1_Click(object sender, EventArgs e)
    {
        //Adding the Columns.
        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {
            dt1.Columns.Add(column.HeaderText, column.ValueType);
        }
        //Adding the Rows.
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            dt1.Rows.Add();
            foreach (DataGridViewCell cell in row.Cells)
            {
                dt1.Rows[dt1.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();
            }
        }
    }

    Then I use a linq on dt1 to fetch a data from a particular column(“2018 FY NEW”).
    Here is a code example you can refer to.

    DataTable newDataTable = new DataTable();
    private void button2_Click(object sender, EventArgs e)
    {
        newDataTable.Columns.Add("2018 FY NEW", typeof(string));
        var rowColl = dt1.AsEnumerable();
        List<string> lstResult = (from r in rowColl
                        where r.Field<int>("Id") >=1
                        select r.Field<string>("2018 FY NEW")).ToList();
            foreach (string l in lstResult) {
                newDataTable.Rows.Add(l);
            }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 17, 2020 6:46 AM