简体   繁体   中英

How do I change value of selected cell on Gridview

How do I change the value of a selected cell using a button for eg changing a cell value from 1 to 2

int row = 0;
            row = dataGridView1.Rows.Count - 2;
            dataGridView1["Amount", row].Value = Keytxt.Text;
            Keytxt.Clear();

this is my current solution but it only changes the value of the last cell in the column

if you want to change it using button and textBox try this code,but at the first take row and cell number from user like this...

private void button1_click(object sender,Eventargs e){
  int rowIndex = int.Parse(txtRowIndex.Text);
  int cellIndex = int.Parse(txtCellIndex.Text);
  //but here you should validate that textBoxs of row and cell index is only numeric or you can use numericUpDown it will be super easy to use
  dataGridView1.Rows[rowIndex-1].Cells[cellIndex-1].value = Keytxt.Text;
  Keytxt.Clear();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM