How do I select a row in the DataGrid and get the value of a row?

I am using the SfDataGrid (UWP) with data from SQLite, I need to select a row and get the value of a column so I can pass the data to another XMAL page and display the details.   

4 Replies

GB Garling Beard February 29, 2016 04:03 PM UTC

I believe I found something that will work:

 this.dgCustomers.SelectionChanging += dgCustomers_SelectionChanging;

void dgCustomers_SelectionChanging(object sender, GridSelectionChangingEventArgs e)
        {
            var record = dgCustomers.GetRecordAtRowIndex(dgCustomers.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex);
            if (record == null)
                return;
            
            var colIndex = dgCustomers.ResolveToGridVisibleColumnIndex(dgCustomers.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex);
            if (colIndex >= 0)
            {
                var column = dgCustomers.Columns[colIndex];
                var cellValue = dgCustomers.View.GetPropertyAccessProvider().GetValue(record, column.MappingName);
                
            }
            

        }

This gives me the value of the selected cell. 


SR Sivakumar R Syncfusion Team February 29, 2016 04:05 PM UTC

Hi Garling,

Yes, you can use SelectionChanging as you mentioned or SelectionChanged event with SfDataGrid.SelectedItem.

private void Button_Click(object sender, RoutedEventArgs e)
{
    listBox.Items.Clear();
    // Get the selected items of SfDataGrid
    var reflector = this.dataGrid.View.GetPropertyAccessProvider();
   var row = this.dataGrid.SelectedItem;
    
        foreach (var column in dataGrid.Columns)
        {
            //Get the value from data object based on MappingName
            var cellvalue = reflector.GetValue(row, column.MappingName);
            //Returns the display value of the cell from data object based on MappingName
            //var displayValue = reflector.GetFormattedValue(row, column.MappingName);
            listBox.Items.Add(cellvalue.ToString());
        }
}

Refer below KB for sample and details:
https://www.syncfusion.com/kb

Thanks,
Sivakumar


GB Garling Beard March 28, 2016 03:50 PM UTC

I have this working but now I need to get the values of multiple columns in the row for example:

var col1 should be column[0]  
var col2 should be column[3]

How can I do this? 


SV Srinivasan Vasu Syncfusion Team March 29, 2016 07:27 AM UTC

Hi Garling,

We have analyzed your query. You can refer below code example for get particular column value from selected row in SfDataGrid.

Code snippet:

private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            if (this.dataGrid.SelectedItem != null)

            {

                listbox.Items.Clear();

                var reflector = this.dataGrid.View.GetPropertyAccessProvider();

                var row = this.dataGrid.SelectedItem;


                //Get the value from data object based on Column index              

                var cellvalue = dataGrid.Columns[1].MappingName; // Use any column index

                var cell_value = reflector.GetValue(row, cellvalue);// Reflect the cell Value              


                listbox.Items.Add(cell_value.ToString());

            }
        }


We have also prepared the sample based on this and please find the sample under the following location,

Sample: http://www.syncfusion.com/downloads/support/forum/123243/ze/CellValue1895583630

Regards,
Srinivasan


Loader.
Up arrow icon