Devexpress从gridview child获取值

时间:2013-01-03 08:31:12

标签: c# winforms devexpress devexpress-windows-ui

我使用此代码生成数据并将其输出到gridview

string sql = "Sql Query";
string sqlCredit= "Sql Query";
string sqlCreditPayment = "Sql Query";

  SqlDataAdapter da = new SqlDataAdapter();

    DataSet ds = new DataSet();
    ds.EnforceConstraints = false;
    ds.DataSetName = "Receivables";

    ds.Tables.Add((con.ShowResult(sql, ref da)).Tables[0].Copy());
    ds.Tables[0].TableName = "dtReceivables";

    ds.Tables.Add((con.ShowResult(sqlCredit, ref da)).Tables[0].Copy());
    ds.Tables[1].TableName = "dtCredit";
    ds.Tables[1].Columns[1].ColumnMapping = MappingType.Hidden;
    ds.Tables[1].Columns[7].ColumnMapping = MappingType.Hidden;

    ds.Tables.Add((con.ShowResult(sqlCreditPayment, ref da)).Tables[0].Copy());
    ds.Tables[2].TableName = "dtCreditPayment";
    ds.Tables[2].Columns[0].ColumnMapping = MappingType.Hidden;

    DataRelation dr0 = new DataRelation("CreditList", ds.Tables[0].Columns["Id"], ds.Tables[1].Columns["DocSupplierId"]);

    ds.Relations.Add(dr0);


    DataRelation dr1 = new DataRelation("CreditPaymentList", ds.Tables[1].Columns["Id"], ds.Tables[2].Columns["SourceId"]);
    ds.Relations.Add(dr1);



    slipDashBoard.DataSource = ds.Tables["dtReceivables"];

    slipDashBoard.ForceInitialize();
    gridView1.BestFitColumns();

专家。请帮忙。当我点击gridview的孩子时,我希望实现这样的目标。 thnx提前

enter image description here

2 个答案:

答案 0 :(得分:7)

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

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

由于您的子GridView是自动创建的,因此有两种方法:

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

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事件处理程序:

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

答案 1 :(得分:0)

如果在运行时创建网格,则有一个像gridview2这样的实例。现在,您可以使用gridview2.Click += new EventHandler(gridview2_Click);

添加点击事件

然后你会得到某事。像这样:

    void view_Click(object sender, EventArgs e)
    {
       //take the code from platons post...
    }