Get the value from the first column of a selected row

So I have been messing with this issue for a bit now. What I have a datagrid that displays all of the users for the software. The datatable doesn't show all of the information so I would like to get the first column text of a selected row. From the documentation none of it applies to how I do this with a database. I am using a remote SQL database if that helps. So, if I have a table like below, I want to get the Username field which should be "Admin".

Username       First Name      Last Name
Admin             Jon                  Doe <--- Selected ROW
userq               max                 lo

1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team December 16, 2020 12:09 PM UTC

Hi Andrew,

Thank you for contacting Syncfusion support.

Your requirement can be achieved by using SfDataGird.CurrentItem when click on the row by using the SelectionChanged event in SfDataGrid. Please refer the below code snippet for your reference, 
sfDataGrid.SelectionChanged += sfDataGrid_SelectionChanged; 

void
sfDataGrid_SelectionChanged(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangedEventArgs e) 
{ 
            var selectedItem = this.sfDataGrid.CurrentItem as DataRowView; 
            var dataRow = (selectedItem as DataRowView).Row;
          //mention the first column mapping name to get first column value in Selected row 
            var cellValue = dataRow["ID"].ToString(); 
            textBox2.Text = cellValue; 
} 


Your requirement can be achieved by using SfDataGrid.SelectedItems[0] in SfDataGrid, by converting SelectedItem into underlying business object. Please refer the below code snippet for your reference, 
private void button1_Click(object sender, EventArgs e) 
{ 
            if(sfDataGrid.SelectedItems.Count > 0) 
            { 
                //get the selected row value in SelectedItems collection in SfDataGrid 
                var selectedItem = sfDataGrid.SelectedItems[0]; 
                var dataRow = (selectedItem as DataRowView).Row; 
                //mention the first column mapping name to get first column value in Selected row 
                var cellValue = dataRow["ID"].ToString(); 
                textBox1.Text = cellValue; 
            } 
}    
https://help.syncfusion.com/windowsforms/datagrid/selection?cs-save-lang=1&cs-lang=csharp#get-the--cell-value

We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon