我正在使用C#开发winform应用程序,并且不会触发Gridview单元格双击事件。我在下面给出了我的源代码
private void gridView2_DoubleClick(object sender, EventArgs e)
{
try
{
GridView view = (GridView)sender;
DataTable getGrid = (DataTable)Popup_Grid.DataSource;
int RowVal = gridView2.FocusedRowHandle;
string FieldVal = ERPModule.isnull(Convert.ToString(gridView2.GetRowCellValue(RowVal, gridView2.Columns["DisplayName"])), "");
if (RowVal != -1)
{
string SelectionStart = richTextBox1.Text.Substring(0, richTextBox1.SelectionStart);
string SelectionEnd = richTextBox1.Text.Substring(richTextBox1.SelectionStart, richTextBox1.Text.Length - richTextBox1.SelectionStart);
richTextBox1.Text = SelectionStart + FieldVal + SelectionEnd;
Displaydata_Popup_Container.Parent.FindForm().Close();
}
else
{
Displaydata_Popup_Container.Parent.FindForm().Close();
}
}
catch (Exception ex)
{
Utility.ErrorLog.WriteToFile(DataFile.errorFile, "PrintList : gridView2_DoubleClick()", ex.Message);
}
}
答案 0 :(得分:2)
提取函数的公共逻辑(可能是扩展方法)
public static class DataTableExtensions
{
public static int CountChequeStatusOf(this DataTable table, string condition)
{
var count = table.Compute("Count(ChequeStatus)", $"ChequeStatus = '{condition}'");
return count == DbNull.Value ? 0 : (int)count;
}
}
然后"重复使用"它
var table = (DataTable)grid_cheque.DataSource;
this.lblNew.Text = table.CountChequeStatusOf("New").ToString();
this.lbl_Issued.Text = table.CountChequeStatusOf("Issued").ToString();
this.lblCancel.Text = table.CountChequeStatusOf("Cancelled").ToString();
this.lbldishonour.Text = table.CountChequeStatusOf("DisHonour").ToString();