【问题标题】:How to get Devexpress XtraGrid control selected row如何获取 Devexpress XtraGrid 控件选定的行
【发布时间】:2013-04-03 08:46:23
【问题描述】:

我有一个 devexpress XtraGrid 控件。但是,当 winform 加载时,我无法获取默认选定行的 ID。当用户点击网格时,我知道如何获取它。

这里是代码快照:

    private void Form1_Load(object sender, EventArgs e)
    {
     grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());

     ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID"));
     XtraMessageBox.Show(ID.ToString());
    }


    public BindingSource bindData(object obj)
    {
        BindingSource ctBinding;
        try
        {
            ctBinding = new BindingSource();

            ctBinding.DataSource = obj;

            return ctBinding;
        }
        catch (Exception ex)
        {
            XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return null;
        }
    }            

【问题讨论】:

  • Table1 中的类型是什么?
  • 它是一个使用 LINQ to SQL 访问的表对象
  • 您能具体说明您遇到的问题吗?我想我不明白你需要什么。
  • 问题是每次加载表单时都会抛出“对象引用未设置为对象的实例”。当我深入研究代码时,问题在于获取焦点行(默认情况下)值。即使列表中有太多行,它也会返回 0(对于 ID)。当我点击行时它工作正常。但是,我希望它在表单加载时获取 ID
  • 您真的应该将其添加到您的问题中。如果它返回 0,则您正在查看 RowHandle,而不是行值。尝试将您的代码移动到 form_Shown 事件而不是 form_Load 事件

标签: devexpress xtragrid


【解决方案1】:

如果我理解正确,你需要这样的东西:

  private void Form1_Shown(object sender, EventArgs e)
  {
     grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());

     var item = gridView.GetFocusedRow() as YourDataType
     if(item != null)
     {
       ID = item.ID;
       XtraMessageBox.Show(ID.ToString());
     }
  } 

假设您的 bindData 返回某种类型的集合。

** 更新**

将代码移至form_Shown 似乎可以解决问题。

【讨论】:

    猜你喜欢
    • 2012-09-27
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2011-11-29
    相关资源
    最近更新 更多