Anon Anon - 3 months ago 12
C# Question

Having difficulty editing datagridview. How do i make it editable?

I've linked a combobox to the datagridview and its working perfectly. However, I need to make the datagridview editable but I can't enter anything into the datagridview. ReadOnly is off/false. I've tried to use a cellvaluechanged event when the value of the datagridview, but first I need to get the cell to change. How do I make the datagridview editable through linq to entity?

private void editDataGridView_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
editDataGridView.CellValueChanged
+= new DataGridViewCellEventHandler(editDataGridView_CellValueChanged);
}

private void editDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("Cell Changed");
}


This is where I link the combobox and the datagridview

private void cboeCID_SelectedIndexChanged_1(object sender, EventArgs e)
{
var CID = Convert.ToInt32(cboeCID.Text);

using (Entities2 db = new Entities2())
{
var course = from c in db.Student_Course where c.CID == CID select new {
SID = c.SID,
Mark = c.Mark};
editDataGridView.DataSource = course.ToList();
}
}

TaW TaW
Answer

Found here: The problem is with the anonymous type you are selecting.

Anonymous types contain one or more public read-only properties.

So being read-only is carried over into the result set and then into the DGV..

You need to select into a class, even if it is no more than a dummy for just this purpose..

class dummy
{
    public integer SID { get; set; }
    public integer Mark { get; set; }

    public dummy() { }
    public dummy(integer w, integer p) { SID = s;  Mark = m; }
}

Insert you own datatypes! Pick a less lackluster name ;-)

Now you can change the select..:

     var course = from c in db.Student_Course where c.CID == CID select new dummy
     {
         SID = c.SID,
         Mark = c.Mark
      };

     editDataGridView.DataSource = course.ToList();

..and voilĂ : The DataGridView is editatable..

Comments