unable to edit DataGridView populated with results of LINQ query

前端 未结 3 1882
-上瘾入骨i
-上瘾入骨i 2020-12-19 23:50

When i use the results of a linq-to-xml query to populate a datagridview, i cannot edit the datagridview. i\'ve tried setting the datagridview\'s readonly property to false

相关标签:
3条回答
  • 2020-12-19 23:55

    Yes, it is possible to bind to a list created from Linq-To-Xml. I tried to reproduce your problem. I did the following:

    1. Created an empty WindForm project (VS 2008 and .Net 3.5)
    2. Added a DataGridView to the Form.
    3. Wired the CellBeginEdit and CellEndEdit.
    4. Placed the following code in the Form's constructor

    string testXML =
            @"<p><entry>
              <author>TestAuthor1</author>
              <msg>TestMsg1</msg>  
              </entry></p>
            ";
    
    XElement xmlDoc = XElement.Parse(testXML);
    
    var query = from entry in xmlDoc.Descendants("entry")
                select new MergeEntry
                {
                    author = entry.Element("author").Value,
                    message = entry.Element("msg").Value,
                }; //You were missing the ";" in your post, I am assuming that was a typo.
    
    //I first binded to a List, that worked fine. I then changed it to use a BindingList
    //to support two-way binding.
    var queryAsList = new BindingList<MergeEntry>(query.ToList());
    
    bindingSource1.DataSource = queryAsList;
    dataGridView1.DataSource = bindingSource1;
    

    When running the WinForm app, an editable grid was displayed and the CellBeginEdit and CellEndEdit events were fired when they should have been. Hopefully trying to reproduce using the above steps will help you locate the issue you are facing.

    0 讨论(0)
  • 2020-12-20 00:08

    Since you're using ToList, your DataSource is actually a List<MergeEntry>, so the fact that it comes from a Linq query doesn't change anything. I suspect the ReadOnly property of the columns (not the DGV) is set to true... I see no other reason why the grid wouldn't be editable

    0 讨论(0)
  • 2020-12-20 00:09

    This solution may not efficient but it works for me, I moved all data (which is from LINQ) into a new collection and passed that new collection as datasource to gridview. Now gridview allow us to edit cells.

    0 讨论(0)
提交回复
热议问题