We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

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

Thread ID:

Created:

Updated:

Platform:

Replies:

123243 Feb 29,2016 10:25 AM Mar 29,2016 03:27 AM UWP 4
loading
Tags: SfDataGrid
Garling Beard
Asked On February 29, 2016 10:25 AM

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.   

Garling Beard
Replied On February 29, 2016 11:03 AM

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. 

Sivakumar R [Syncfusion]
Replied On February 29, 2016 11:05 AM

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

Garling Beard
Replied On March 28, 2016 11:50 AM

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? 

Srinivasan Vasu [Syncfusion]
Replied On March 29, 2016 03:27 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;