Get value from Sfdatagrid to label in C#

Hello :)

Im wondering if you could help me... Well, I would like to get data from SfDataGrid,
i have tabel with ID, Name and Surename.
for example
ID NAME SURNAME
1 |Sara |S
2 |George |G

When the user is going to click on ID 1 or Name Sara
Label will be field with those values.

Do you have any idea?



17 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team February 18, 2021 04:22 PM UTC

Hi SFDatagridQuestion,

Thank you for contacting Syncfusion support.

Your requirement can be achieved by using CellClick event in SfDataGrid. Please refer the below code snippet for your reference,
 
private void SfDataGrid_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e) 
        { 
            if (sfDataGrid.CurrentCell != null) 
            { 
                // Get the row index value         
                var rowIndex = e.DataRow.RowIndex; 
                //Get the column index value 
                var columnIndex = e.DataColumn.ColumnIndex; 
                //Get the cell value             
                label2.Text = this.sfDataGrid.View.GetPropertyAccessProvider().GetValue(e.DataRow.RowData, e.DataColumn.GridColumn.MappingName).ToString(); 
            } 
        } 
Regards,
Vijayarasan S
 



SF SFDatagridQuestion February 18, 2021 05:27 PM UTC

Thanks 

but I tried it like that already, i this case i get only one value, 

but I need it like that: if i click on id 1 or name Sara 

I get in label1 text: 1 (as an ID)

in label2 text : Sara (as a name)

and in label3 text: S (as surname)

I know how to do it in datagridview but in Sfdatagrid its different

if you could help me with this case i would really appreciate that



MA Mohanram Anbukkarasu Syncfusion Team February 19, 2021 08:57 AM UTC

Hi SFDatagridQuestion, 

Thanks for the update.  

You can achieve this requirement by using the CellClick event as shown in the following code example.  

Code example :  

sfDataGrid.CellClick += SfDataGrid_CellClick; 
 
private void SfDataGrid_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e) 
{ 
    label2.Text = this.sfDataGrid.View.GetPropertyAccessProvider().GetValue(e.DataRow.RowData, "OrderID").ToString(); 
    label5.Text = this.sfDataGrid.View.GetPropertyAccessProvider().GetValue(e.DataRow.RowData, "CustomerID").ToString(); 
    label6.Text = this.sfDataGrid.View.GetPropertyAccessProvider().GetValue(e.DataRow.RowData, "CustomerName").ToString(); 
} 


Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 


Marked as answer

SF SFDatagridQuestion February 19, 2021 11:45 AM UTC

Thank you soo much, its working now :D


MA Mohanram Anbukkarasu Syncfusion Team February 22, 2021 04:16 AM UTC

Hi SFDatagridQuestion, 

Thanks for the update.  

We are glad to know that the provided solution worked at your end. Please let us know if you have any further queries on this. We are happy to help you. 

Regards, 
Mohanram A. 



PE Pedro April 22, 2021 10:24 PM UTC

Hello

I would like the same thing as you helped the other customer

But I needed a method out the cell click

Throw a button: When I click the button while the row in sfdatagrid is selected it does the same things

Can You Help me?


MA Mohanram Anbukkarasu Syncfusion Team April 23, 2021 06:39 AM UTC

Hi Pedro, 

Thanks for the update.  

You can achieve the same in a button click event by using SfDataGrid.SelectedItem property as shown in the following code example.  

Code example :  

private void button1_Click(object sender, EventArgs e) 
{ 
    var selectedItem = this.sfDataGrid.SelectedItem; 
    label2.Text = this.sfDataGrid.View.GetPropertyAccessProvider().GetValue(selectedItem, "OrderID").ToString(); 
    label5.Text = this.sfDataGrid.View.GetPropertyAccessProvider().GetValue(selectedItem, "CustomerID").ToString(); 
    label6.Text = this.sfDataGrid.View.GetPropertyAccessProvider().GetValue(selectedItem, "CustomerName").ToString(); 
} 


Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 



PE Pedro April 23, 2021 06:12 PM UTC

Hi

It worked

Thank you very much


MA Mohanram Anbukkarasu Syncfusion Team April 26, 2021 06:09 AM UTC

Hi Pedro, 

Thanks for the update.  

We are glad to know that the provided solution worked at your end. Please let us know if you require further assistance from us. We are happy to help you.  

Regards, 
Mohanram A. 



PE Pedro May 17, 2021 10:07 PM UTC

Hello, How are You?

I would like to use SFDATAGRID, just like excel

Add itens, edit, delete and save to the sql database automatically

Can you help me?




MA Mohanram Anbukkarasu Syncfusion Team May 18, 2021 01:51 PM UTC

Hi Pedro, 
 
Thanks for the update. 
 
SfDataGrid doesn’t have direct support to update underying database automatically when adding, deleting and editing records in the DataGrid. However you can achieve this by manually by using events like SfDataGrid.View.Records.CollectionChanged, SfDataGrid.CurrentCellEndEdit, SfDataGrid.RecordDeleting. Please refer the sample from the following link.  
 
 
Please let us know if you require further assistance from us.  
 
Regards, 
Mohanram A. 



PE Pedro May 18, 2021 03:40 PM UTC

Hi, Thanks for the Update

Would you Have this code for SQL DATA BASE instead of ACCESS?


MA Mohanram Anbukkarasu Syncfusion Team May 19, 2021 01:08 PM UTC

Hi Pedro, 

We regret for the inconvenience.  

You can use the same events SfDataGrid.View.Records.CollectionChanged, SfDataGrid.CurrentCellEndEdit, SfDataGrid.RecordDeleting which we have mentioned in our previous update to commit the changes made in the SfDataGrid to the underlying database irrespective of the type of database used. Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 



PE Pedro May 23, 2021 07:46 PM UTC

Hi, No problems

I would like to know how to pass SFDATAGRID data to a Table RDLC (ReportViewer) in C#

Can you help me?


VS Vijayarasan Sivanandham Syncfusion Team May 24, 2021 04:26 PM UTC

Hi  Pedro,

Thanks for the update.

Your requirement can be achieved by using ReportViewer and set the ReportViewer datasource as SfDataGrid DataSource. Please refer the below code snippet, 
           this.sfDataGrid1.AutoGenerateColumns = true; 
            var table = this.GetDataTable();             
            sfDataGrid1.DataSource = table;           
            ReportViewer reportViewer1 = new ReportViewer(); //Your ReportViewer Control             
            ReportDataSource rds = new ReportDataSource("ProgrammersDataSet", sfDataGrid1.DataSource); 
            reportViewer1.ProcessingMode = ProcessingMode.Local; 
            reportViewer1.LocalReport.DataSources.Clear(); 
            reportViewer1.LocalReport.DataSources.Add(rds); 
            reportViewer1.LocalReport.Refresh(); 
            reportViewer1.LocalReport.ReportEmbeddedResource = "Cerocha.Presentation.Reports.ProgrammersReport.rdlc"; 
            reportViewer1.LocalReport.Refresh(); 
Regards,
Vijayarasan S 



PE Pedro May 27, 2021 04:14 PM UTC

Hi

Thank you so much, It Worked


MA Mohanram Anbukkarasu Syncfusion Team May 28, 2021 05:29 AM UTC

Hi Pedro,  

Thanks for the update.   

We are glad to know that the provided solution worked at your end. Please let us know if you require further assistance from us. We are happy to help you.   

Regards,  
Mohanram A. 


Loader.
Up arrow icon