Databound Master-Detail Datagrid, getting detail cell values?

I have setup a Master-Details datagrid according to the Instructions provided by this site.  I am able to get cell values of any cell on the Master grid but unable to figure out out to get any of the cell values on the details grid.


I have looked at this article:

https://support.syncfusion.com/kb/article/9784/how-to-get-a-cell-value-in-detailsviewdatagrid-in-datagridsfdatagrid


but this article shows a sample where the child grid is added manually, where my child grid is added through databound relationships.


problem.png


how can I go about getting cell values WITHOUT button clicks AND with button clicks from my detail grid that is databound?


1 Reply

MS Malini Selvarasu Syncfusion Team October 2, 2024 12:14 PM UTC

HI Kevin Davis,

Thank you for providing the detailed information regarding this requirement. Based on your input, we understand that you need to obtain the child data values both with and without button clicks.
Query 1: How can we retrieve child data using a button click?
To achieve this, you can Call the GetcellValue() method within the button click event by passing the appropriate row and column indices. This method returns the corresponding cell values for the specified row and column. Please review the below code snippet,

Code Snippet:

private void button1_Click(object sender, EventArgs e)
{
    SfDataGrid sfDataGrid = this.sfDataGrid1.GetDetailsViewGrid(2);
    Text.Text = GetCellValue(sfDataGrid, Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox1.Text));

}
private static string GetCellValue(Syncfusion.WinForms.DataGrid.SfDataGrid dGrid, int rowIndex, int columnIndex)
{
    string cellValue;
    if (columnIndex < 0)
        return string.Empty;
    var mappingName = dGrid.Columns[columnIndex].MappingName;
    var recordIndex = dGrid.TableControl.ResolveToRecordIndex(rowIndex);
    if (recordIndex < 0)
        return string.Empty;
    if (dGrid.View.TopLevelGroup != null)
    {
        var record = dGrid.View.TopLevelGroup.DisplayElements[recordIndex];
        if (!record.IsRecords)
            return string.Empty;
        var data = (record as RecordEntry).Data;
        cellValue = (data.GetType().GetProperty(mappingName).GetValue(data, null).ToString());
    }
    else
    {
        var record1 = dGrid.View.Records.GetItemAt(recordIndex);
        cellValue = (record1.GetType().GetProperty(mappingName).GetValue(record1, null).ToString());
    }
     return cellValue;
}


Query 2: How can we retrieve child data without a button click?**
This can be accomplished by utilizing the SfDataGrid.CellClick event, which is triggered when a cell in the DataGrid is clicked. Upon clicking a cell, the event allows you to obtain the Row index, Column index, and Cell value. Please refer to the code snippet below for further clarification.
Code Snippet:
(this.sfDataGrid1.DetailsViewDefinitions[0] as GridViewDefinition).DataGrid.CellClick += DataGrid_CellClick;
private void DataGrid_CellClick(object sender, CellClickEventArgs e)
{
    // Get the row index value        
    var rowIndex = e.DataRow.RowIndex;
    //Get the column index value
    var columnIndex = e.DataColumn.ColumnIndex;
    //Get the cell value            
    var cellValue = (e.OriginalSender as DetailsViewDataGrid).View.GetPropertyAccessProvider().GetValue(e.DataRow.RowData, e.DataColumn.GridColumn.MappingName);
    MessageBox.Show("Cell Value \t:    " + cellValue + "\n" + "Row Index \t:    " + rowIndex + "\n" + "Column Index \t:    " + columnIndex, "Cell Value"); 
}

We have created a sample to demonstrate this approach and have attached it for your review. Please evaluate the sample and let us know if this solution meets your needs. If there is any misunderstanding regarding your requirements, please provide more details so we can assist you further. Thank you for your understanding and cooperation.
Regards,
Malini Selvarasu

Attachment: DetailsView_Demo_f247bc1d.zip

Loader.
Up arrow icon