首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何获取Devexpress XtraGrid控件的选定行

如何获取Devexpress XtraGrid控件的选定行
EN

Stack Overflow用户
提问于 2013-04-03 16:46:24
回答 1查看 9.3K关注 0票数 0

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

下面是代码快照:

代码语言:javascript
复制
    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;
        }
    }            
EN

回答 1

Stack Overflow用户

发布于 2013-04-03 16:58:37

如果我没理解错的话,你需要这样的东西:

代码语言:javascript
复制
  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似乎可以做到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15782671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档