【问题标题】:Giridview cell double click event is not fired?Gridview 单元格双击事件没有被触发?
【发布时间】:2017-10-06 06:07:59
【问题描述】:

我正在使用 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);
        }

    }

【问题讨论】:

  • 您要简化的内容
  • 是否有可能传递类型消息?对象变量和字段仅更改。所以请让我知道简化此代码的任何可能性

标签: c# .net winforms devexpress


【解决方案1】:

将常用逻辑提取到函数中(可扩展方法)

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();

【讨论】:

  • 我已经使用了你的代码并且收到了类似“扩展方法必须在顶级静态类中定义;DataTableExtensions 是一个嵌套类”的错误
  • @Raj - 非常清晰的错误信息 ;)。将DataTableExtensions 类放在当前类之外或单独的文件中。
  • 是的,我做了,但显示错误 DBNull is not exist in the current content.请您简单介绍一下吗?
  • 使用System.DbNull或在文件顶部添加using System;
猜你喜欢
  • 2012-01-25
  • 2017-02-14
  • 2012-10-28
  • 1970-01-01
  • 2013-03-10
  • 2017-11-11
  • 2013-02-21
  • 1970-01-01
  • 2018-03-31
相关资源
最近更新 更多