DataGridView 內觸發物件事件 ComboBox
剛好用到紀錄下來
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//判定更新欄位 抓位子
//方法一
if (dataGridView1.CurrentCell.RowIndex != -1 && this.dataGridView1.CurrentCell.ColumnIndex == 4) //指定選擇的欄位索引 0 1.2.3.4
{
//也可參考之前寫的 http://www.dotblogs.com.tw/joe80075/archive/2012/11/29/85232.aspx
//把觸發的事件內容寫成類別之後可以重複使用
}
//判定更新欄位 抓TypeName
//方法二
if (dataGridView1.CurrentCell.RowIndex != -1 && dataGridView1.CurrentCell.GetType().Name == "DataGridViewComboBoxCell")
{
ComboBox comboBox = (ComboBox)e.Control;
rowflag = dataGridView1.CurrentRow.Index;
comboBox.Leave += new EventHandler(comboBoxLeave); 加入觸發事件
}
}
int rowflag = 0;
void comboBoxLeave(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (comboBox.Text == "結案")
{
this.dataGridView1.Rows[rowflag].Cells[7].Value = DateTime.Now.ToShortDateString();
}
}
大家一起加入blogads 賺零用錢!!