我有一個Datagridview,至此我需要驗證某些行上的用戶輸入,並根據它的值更改它。例如,如果用戶輸入的數據庫中不存在產品代碼,則搜索產品對話框會引發並搜索產品並返回正確的代碼。 所以周圍有很多看完之後,我決定來處理CellFormating事件:但我遇到了兩個問題:DatagridView:在任何單元格上的每個「onmouseenter」上,cellFormatting事件被引發?
- 的e.value被設置好的與正確的價值,但它並沒有堅持
- 而這次事件就像數百萬次提高一樣,有時會遇到SO例外。
這就是爲什麼我把mbox放在事件處理程序中,現在程序什麼也不做,只是顯示mbox。
這裏的要點是,即使表單是新創建的,事件引發了很多,每當我將mose指針移動到任何單元格時,事件都會再次引發。
這裏最好的選擇是什麼?波紋管是事件處理程序中的代碼:
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if (e.ColumnIndex == 0 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
{
if (!Rep.Cajero_ProductoExiste(double.Parse(e.Value.ToString())))
{
BuscarEIngresarProducto(ref e);
}
}
MessageBox.Show("Event handler raised");
}
catch
{
e.FormattingApplied = false;
}
}
哪裏Rep.Cajero_ProductoExiste
是,如果產品存在返回一個布爾值= true的方法,該方法BuscarEIngresarProducto()
是如下:
private void BuscarEIngresarProducto(ref DataGridViewCellFormattingEventArgs e)
{
Busqueda b = new Busqueda(Rep, 2);
if (b.ShowDialog() == DialogResult.OK)
{
e.Value = b.ProductoCodigo;
dataGridView1.CurrentRow.Cells["pk"].Value = b.Producto;
e.FormattingApplied = true;
}
}
任何幫助將被預言。在此先感謝(PD:對不起4我的英語,我是西班牙說話者)
謝謝! datagridview中有很多事件,所以我認爲我必須開始閱讀很多事件。 – josecortesp 2009-11-25 17:26:30