none
How to query datagridview by LINQ RRS feed

  • Question

  • see my code first

    string strBrokerGridXML = ConfigurationManager.AppSettings["OutputPath"].ToString().Trim() + cmbTicker.Text.ToString() + "\\" + "Broker_History.xml";
    
                string selectedhistory = "", brokercode = "";
    
     
    
                if (File.Exists(strBrokerGridXML))
    
                {
    
                    DataSet ds = new DataSet();
    
                    ds.ReadXml(strBrokerGridXML);
    
     
    
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    
                    {
    
                        brokercode = ds.Tables[0].Rows[i]["BrokerCode"].ToString();
    
                        selectedhistory = ds.Tables[0].Rows[i]["IsHistorySelected"].ToString();
    
     
    
                        //((DataGridViewCheckBoxCell)dgvBrokers.Rows[i].Cells["colSelect"]).Value = (selectedBroker.ToUpper()=="TRUE" ? true : false);
    
     
    
                        var dgvrows =  dgvBrokers.Rows.OfType<DataGridViewRow>()
    
                        .Where(r => r.Cells["colBrokers"].Value.ToString().ToUpper() == brokercode.ToUpper())
    
                        .ToList();
    
     
    
                        foreach (DataGridViewRow row in dgvrows)
    
                        {
    
                            ((DataGridViewCheckBoxCell)row.Cells["colHist"]).Value = (selectedhistory.ToUpper() == "TRUE" ? true : false);
    
                        }
    
     
    
                    }
    
                }
     

    i am iterate in rows of datatable and query grid by LINQ and setting true/false value in grid. can i avoid the above iteration and query datagridview & set cell value in one go ?

    looking for code example if possible.

    Monday, July 1, 2019 7:26 PM

All replies

  • Hello,

    No matter how you write this code a for-next is needed. Even with direct access to a DataTable you need to use row.SetField<bool>("fieldname","value")


    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

    Monday, July 1, 2019 8:46 PM
    Moderator
  • Hi Sudip_inn,

    Thank you for posting here.

    I agree with Karn's thought, we need to use loop to do that whatever we write.

    However, I could simplify your code to get it.

    Since you don't give the xml, I create a xml to test it.

    xml:

    <?xml version="1.0" encoding="utf-8" ?>  
    <books>  
      <book ISBN="9831123212" yearpublished="2002">  
        <title>Test1</title>  
        <author>  
          <first-name>Mahesh</first-name>  
          <last-name>Chand</last-name>  
        </author>  
        <publisher>Apress</publisher>  
        <selectedhistory>true</selectedhistory>  
      </book>  
    </books>  

    Code:

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("male", typeof(bool));
                table.Rows.Add("Test1", false);
                table.Rows.Add("Test2", false);
                this.dataGridView1.DataSource = table;
    
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                var dt = (DataTable)dataGridView1.DataSource;
                string path = @"D:\t.xml";
                if (File.Exists(path))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(path);
                    var data1 = ds.Tables[0].AsEnumerable().Select(r => r.Field<string>("title")).ToList();
                    var test= ds.Tables[0].AsEnumerable().Select(r => r.Field<string>("selectedhistory")).ToList();
                    int count = 0;
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        string m = row.Cells["Name"].Value.ToString();
                        if (m != null)
                        {
                            for (int i = 0; i < data1.Count; i++)
                            {
                                if (data1[i].ToUpper() == m.ToUpper())
                                {
                                    row.Cells[1].Value = (test[i].ToUpper() == "TRUE" ? true : false);
                                }
                            }
                        }
                        count++;
                        if (count == dataGridView1.Rows.Count-1)
                        {
                            break;
                        }
                    }
                }
            }

    Result:

    Best Regards,

    Jack


    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.

    Tuesday, July 2, 2019 6:25 AM
    Moderator
  • probably you do not understand my question. the answer you posted not good for me. thanks
    Thursday, July 4, 2019 12:00 PM

  • i am iterate in rows of datatable and query grid by LINQ and setting true/false value in grid. can i avoid the above iteration and query datagridview & set cell value in one go ?

    looking for code example if possible.

    Yes there is a much easier way. 

    Use the Datasource of the DataGridView while you have added a column to the datatable. 

    Because of the fact that the column is added by using the datagridview there is no need to use the DataGridView as a datacontainer.

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/adding-columns-to-a-datatable


    Success
    Cor

    Thursday, July 4, 2019 12:17 PM