Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ绑定DataGridView时出现问题?_C#_.net_Winforms_Linq_Datagridview - Fatal编程技术网

C# 使用LINQ绑定DataGridView时出现问题?

C# 使用LINQ绑定DataGridView时出现问题?,c#,.net,winforms,linq,datagridview,C#,.net,Winforms,Linq,Datagridview,当我将linq查询绑定到datagridview的数据源时,我无法更改gridview中的任何单元格值。columns readonly属性自动设置为true,当我尝试将其设置为false时,会出现以下异常:- 绑定到只读字段的DataGridView列必须将ReadOnly设置为True。林克 这是我的代码 DataClasses1DataContext db = new DataClasses1DataContext(); var selectquery = from s in db.S

当我将linq查询绑定到datagridview的数据源时,我无法更改gridview中的任何单元格值。columns readonly属性自动设置为true,当我尝试将其设置为false时,会出现以下异常:-

绑定到只读字段的DataGridView列必须将ReadOnly设置为True。林克

这是我的代码

 DataClasses1DataContext db = new DataClasses1DataContext();
 var selectquery = from s in db.Sarees where s.Bill.BillNo == billno select new { s.BillID,s.Price };
我找到了这个问题的一个解决方案,如果表中有很多列,并且我只想选择两列,那么这个解决方案就不脏了。。。 第一种解决方案是:-

 var selectquery = db.Sarees.Where(s => s.Bill.BillNo == billno);
当我提出这个问题时,它工作得很好。。
但是我想要一个解决方案,在这个解决方案中,我只能通过LINQ选择一些列,并且可以在通过datagridview绑定时更改其值…

问题是,此查询不返回
saree的集合,因此无法按您的需要对其进行编辑。它返回一组新对象,其中包含
BillID
Price
属性

DataClasses1DataContext db = new DataClasses1DataContext();
var selectquery = from s in db.Sarees
                  where s.Bill.BillNo == billno 
                  select new { s.BillID, s.Price };
是否可以使用第二个查询的版本,并仅将要编辑的列绑定到
DataGridView



您可能还需要探索使用。

如何使用第二个版本并仅绑定某些列(例如ID和价格)?这就是CellFormatting事件的作用所在。即使这样,我也不知道字段更改是否会自动更新。我不认为它是为了像OP所要求的那样保存具有深度的对象。