-1

我有问题。我有一个 datagridview 和一个可编辑的列,用户可以自己写一个数字。但是....我需要在按钮的帮助下写下数字。因此,例如,我有按钮 1、2、3、...9,如果用户单击此可编辑列(当然是在一个单元格上),然后单击按钮 3,则单元格中会出现 3。我不知道该怎么做。我知道 DataGridView 中有这个 EditMode 但我不知道如何使用它。

编辑:我这样做了。它有效:)。但是......当我改变总和的值时,有没有办法查看所选单元格的变化?例如,我选择一个单元格并且 sum=0,一段时间后(当仍然选择同一个单元格时)总和变为 13,但我不会在所选单元格中看到这些变化,当我选择不同的单元格时它会有 13. 有什么方法可以在选定单元格的值发生变化时查看它吗?

dataGridView1.CellClick += CellClicked;
private void CellClicked(object sender,DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            int col = e.ColumnIndex;
            dataGridView1.Rows[row].Cells[col].Value = sum;

         }
4

1 回答 1

1

在您的班级的根目录中创建一个新变量,您可以在其中保存最后点击的单元格:

DataGridViewCell activatedCell;

然后在“CellClicked”事件中设置活动单元格:

private void CellClicked(object sender,DataGridViewCellEventArgs e)
{
   activatedCell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
}

然后对您的按钮进行单击事件,在其中为该激活的单元格设置值:

void Button_Click(Object sender, EventArgs e)
{
    // If the cell wasn't set, return
    if (activatedCell == null) { return; }

    // Set the number to your buttons' "Tag"-property, and read it to Cell
    if (activatedCell.Value != null) { activatedCell.Value = Convert.ToDouble(((Button)sender).Tag) + Convert.ToDouble(activatedCell.Value);
    else { activatedCell.Value = Convert.ToDouble(((Button)sender).Tag); }

    dataGridView1.Refresh();
    dataGridView1.Invalidate();
    dataGridView1.ClearSelection();
}
于 2014-05-27T22:37:30.980 回答