Question

I use this code to generate the data and output it in my 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();

Guys. Pls help. i want to achieve something like this when i click on the gridview's children. thnx in advance

enter image description here

Était-ce utile?

La solution

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

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

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

Autres conseils

If you create your grid on runtime you have an instance like gridview2. Now you can add the click event with gridview2.Click += new EventHandler(gridview2_Click);

Then you will get sth. like this:

    void view_Click(object sender, EventArgs e)
    {
       //take the code from platons post...
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top