0
votes

When i bind my linq query to the datasource of the datagridview, i cannot change any of the cells values in the gridview. The columns readonly property is automatically set to true and when i tried to set it false it gives following exception:-

DataGridView column bound to a read-only field must have ReadOnly set to True. LINQ

This is my code for it

 DataClasses1DataContext db = new DataClasses1DataContext();
 var selectquery = from s in db.Sarees where s.Bill.BillNo == billno select new { s.BillID,s.Price };

I found 1 solution to this problem which is not filthy if there are many columns in the table and i want to select only two... The 1 solution is :-

 var selectquery = db.Sarees.Where(s => s.Bill.BillNo == billno);

When i gave this query it works fine.. But i want a solution in which i can select only some columns by LINQ and can change its value when binded through a datagridview...

1

1 Answers

2
votes

The problem is that this query does not return a collection of Sarees so it cannot be edited like you want. It is a returning a collection of new objects with a BillID and a Price property.

DataClasses1DataContext db = new DataClasses1DataContext();
var selectquery = from s in db.Sarees
                  where s.Bill.BillNo == billno 
                  select new { s.BillID, s.Price };

Can you use a version of the 2nd query and bind only the columns you want to edit to the DataGridView?


You may also need to explore using the DataGridView.CellFormatting Event.