0

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How to use the second version and bind only some columns (eg. ID and Price) ?
That's where the CellFormatting Event comes into play. Even then, I don't know if the field changes will automatically be updated. I don't think it was intended to save objects with depth like the OP is asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.