且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Devexpress从gridview中获取值

更新时间:2023-10-13 13:09:28

这种情况下的主要思想是获取被点击的GridView类的一个实例。 XtraGrid创建在设计时创建的模式视图的克隆,并使用这些克隆来取消数据。这是代码应该工作:

The main idea in this case is to obtain an instance of the GridView class which was clicked. XtraGrid creates clones of the pattern View which is created at design time and use these clones to disply data. Here is the code which should work:

GridView gridView = sender as GridView;
var value = gridView.GetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["Num"));
MessageBox.Show(value.ToString());

由于您的孩子GridView是自动创建的,所以有两种方法:

Since your child GridView is created automatically, there are two approaches:

1)处理GridControl的Click事件处理程序:

1) handle the GridControl's Click event handler:

private void gridControl1_Click(object sender, EventArgs e) {
    GridControl grid = sender as GridControl;
    Point p = new Point(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y);
    GridView gridView = grid.GetViewAt(p) as GridView;
    if(gridView != null)
        MessageBox.Show(gridView.GetFocusedRowCellDisplayText("Num"));
}

2)处理GridView1 MasterRowExpanded事件处理程序:

2) handle the GridView1 MasterRowExpanded event handler:

    private void gridView1_MasterRowExpanded(object sender, CustomMasterRowEventArgs e) {
        GridView master = sender as GridView;
        GridView detail = master.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
        detail.Click += new EventHandler(detail_Click);
    }

    void detail_Click(object sender, EventArgs e) {
            GridView gridView = sender as GridView;
var value = gridView.GetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["Num"));
MessageBox.Show(value.ToString());
    }