如何在更改DataGridViewTextBoxColumn的文本时触发事件?

时间:2011-04-14 03:01:55

标签: c#

我有一个DataGridViewTextBoxColumn。我怎样才能在同一个事件上触发textchanged事件。

        DataGridViewTextBoxColumn dgvColumnTextBox = new DataGridViewTextBoxColumn();
        dgvColumnTextBox.Name = "Alias";
        dataGridView.Columns.Add(dgvColumnTextBox);

我需要在更改“别名”列中任何单元格的文本时触发事件。请帮忙。

2 个答案:

答案 0 :(得分:0)

尝试CellValueChanged事件。 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx,或在控件中查找CellChanged事件。

答案 1 :(得分:0)

DataGridViewCell cell_Changed = null;
DataGridViewCell cell_Begin = null;
string cell_Begin_Save = "";
bool cell_Reset = false;

private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    int colIndex = e.ColumnIndex;
    int rowIndex = e.RowIndex;

    if (e.RowIndex >= 0)
    {
        cell_Changed = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
    }

    if (cell_Changed != null)
    {
        if (cell_Begin_Save != cell_Changed.Value.ToString())
        {
            DataGridView1.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Green;
            cell_Reset = false;
        }
    }
}

private void DataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    int colIndex = e.ColumnIndex;
    int rowIndex = e.RowIndex;

    if (e.RowIndex >= 0 && !cell_Reset)
    {
        cell_Begin = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        cell_Begin_Save = cell_Begin.Value.ToString();
        cell_Reset = true;
    }
}

private void DataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    cell_Reset = false;
}