How to fire an event on changing the text of a DataGridViewTextBoxColumn?
I got a DataGridViewTextBoxColumn. how can i fire a textchanged event on the same.
DataGridViewTextBoxColumn dgvColumnTextBox = new DataGridViewTextBoxColumn();
dgvColumnTextBox.Name = "Alias"开发者_如何转开发;
dataGridView.Columns.Add(dgvColumnTextBox);
I need to fire the event on changing the text of any of the cells in the column "Alias". Please help.
Try the CellValueChanged event . http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx , or look for a CellChanged event in the control.
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;
}
精彩评论