Change value in DataGridView directly

23,264

Solution 1

You can simply do this (assuming that you want to change one value programmatically):

dataGridView1.Rows[RowNumber].Cells[CC.Index].Value = newValue;

And how to enable editing of certain cell was already explained here.

Solution 2

If you are setting the datasource to an object, then the property linked to a certain field has to have a setter in order to change the value. Otherwise it is a read only property and will be treated as such. Not sure if that helps, its a bit unclear what you are after, and what you are working with.

Share:
23,264
Uni Le
Author by

Uni Le

Updated on July 25, 2020

Comments

  • Uni Le
    Uni Le almost 4 years

    How can I change the value from all cells in DataGridView Windows Forms? I want to change directly in Windows Forms, like typing directly in the cell

    I have a table like:

    AA    BB    CC
    --------------
    1     aa    ac
    2     bb    fd// I type here and change the value to kk
    

    Code:

    DataGridViewTextBoxColumn AA= new DataGridViewTextBoxColumn();
    MsgIDHex.HeaderText = "AA";
    MsgIDHex.DataPropertyName = "AA";
    DataGridViewTextBoxColumn BB= new DataGridViewTextBoxColumn();
    MsgIDHex.HeaderText = "BB";
    MsgIDHex.DataPropertyName = "BB";
    DataGridViewTextBoxColumn CC= new DataGridViewTextBoxColumn();
    MsgIDHex.HeaderText = "CC";
    MsgIDHex.DataPropertyName = "CC;
    dataGridView1.DataSource = result;
    dataGridView1.Columns.AddRange(AA, BB, CC};
    

    Should I do something with DataGridViewTextBoxEditingControl?

  • Uni Le
    Uni Le over 11 years
    I want to change directly in Windows Forms
  • Vyktor
    Vyktor over 11 years
    @UniLe stackoverflow.com/questions/1814423/… is'n this helping? :)
  • Uni Le
    Uni Le over 11 years
    yes, your are correct. It has just read property. How can I change my datasource that I can read and write there?
  • Wanabrutbeer
    Wanabrutbeer over 11 years
    if you are using a custom object as your data source, the property in question needs to have a setter method public <Type> PropertyName { get; set; } Without the setter the data binding has no way to update the value in the bound object