GridControl 主从模式(Master-detail)子表格获取行数据

今天遇到一个问题,gridcontrol使用主从表的时候,在子表中获取子表的行数据时居然获取不到,郁闷了很久。然后在网上找到方法(出处在这里:https://q.cnblogs.com/q/83412/),怕那天又忘记了,所以记下来。

关键代码:

 DevExpress.XtraGrid.Views.Grid.GridView currentView = (DevExpress.XtraGrid.Views.Grid.GridView)this.gridControl1.FocusedView;
            //     DataRow focusRow = currentView.GetFocusedDataRow();
            var res = currentView.GetRow(e.RowHandle) as ClassB;
            MessageBox.Show(res.IDB);

 

 

  

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            List<ClassA> resList = new List<ClassA>();
            for (int i = 0; i < 20; i++)
            {
                ClassA A = new ClassA();
                A.IDA = "IDA" + i;
                A.NameA = "NameA" + i;
                A.classBList = new List<ClassB>();

                for (int j = 0; j < 10; j++)
                {
                    ClassB B = new ClassB();
                    B.IDA = A.IDA;
                    B.IDB = "IDB" + j;
                    B.NameB = "NBmeB" + j;
                    A.classBList.Add(B);

                }
                resList.Add(A);
            }
            this.classABindingSource.DataSource = resList; 
            this.gridView2.CellValueChanged += GridView2_CellValueChanged; 
        }

     
        private void GridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView currentView = (DevExpress.XtraGrid.Views.Grid.GridView)this.gridControl1.FocusedView;
            //     DataRow focusRow = currentView.GetFocusedDataRow();
            var res = currentView.GetRow(e.RowHandle) as ClassB;
            MessageBox.Show(res.IDB);
        }

      
    }
}

 

转载于:https://www.cnblogs.com/yuanjiedao/p/10084999.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值