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.

Populate a Grid Control when a row is selected on another Grid Control

Thread ID:

Created:

Updated:

Platform:

Replies:

131511 Jul 14,2017 01:54 AM Jul 19,2017 12:03 AM Windows Forms 3
loading
Tags: GridControl
JeeNew
Asked On July 14, 2017 01:54 AM

Hi,

I have two Grid Controls. I want to know how to populate the second grid control when a row a selected on first grid control. When the row selected is changed the second grid control deletes the previous entries and populates with a new set of values.

I am triggering the event of selection by using 

this.gridControl.CurrentCellActivating += this.GridControlOnCurrentCellActivating;

Is it possible to provide a working example for reference?



Mohanraj Gunasekaran [Syncfusion]
Replied On July 17, 2017 07:34 AM

Hi JeeNew, 
 
Thanks for using Syncfusion product. 
 
Query 
Solution 
have two Grid Controls. I want to know how to populate the second grid control when a row a selected on first grid control. When the row selected is changed the second grid control deletes the previous entries and populates with a new set of values. 
Suggestion 1 
In order to update the selected value in second grid, you can use the PopulateValues method in SelectionChanged event. Please refer to the below code example, 
 
Code example 
this.gridControl1.SelectionChanged += gridControl1_SelectionChanged; 
 
void gridControl1_SelectionChanged(object sender, GridSelectionChangedEventArgs e) 
{ 
    this.gridControl2.ColCount = this.gridControl1.ColCount; 
    this.gridControl2.RowCount = e.Range.Height; 
    
    DataTable table = new DataTable(); 
    foreach(DataColumn column in dt.Columns) 
    { 
        table.Columns.Add(column.ColumnName); 
    } 
 
    for (int j = 0; j < e.Range.Height; j++) 
    { 
        DataRow dr = table.NewRow(); 
        for (int i = 0; i < this.gridControl1.ColCount; i++) 
        { 
            dr[i] = this.gridControl1[e.Range.Top + j, i+1].CellValue; 
        } 
        table.Rows.Add(dr); 
    } 
 
    this.gridControl2.PopulateValues(GridRangeInfo.Cells(1, 1, this.gridControl2.RowCount, this.gridControl2.ColCount),table); 
    this.gridControl2.Refresh(); 
} 
Suggestion 2 
Also, you can achieve your scenario by setting the value for cells directly in SelectionChanged event. Please refer to the below code, 
 
Code example 
void gridControl1_SelectionChanged(object sender, GridSelectionChangedEventArgs e) 
{ 
    this.gridControl2.ColCount = this.gridControl1.ColCount; 
    this.gridControl2.RowCount = e.Range.Height; 
    this.gridControl2.PopulateHeaders(GridRangeInfo.Cells(0, 1, gridControl2.RowCount, gridControl2.ColCount), dt); 
 
    for (int j = 0; j < e.Range.Height; j++) 
    { 
        for (int i = 0; i < this.gridControl1.ColCount; i++) 
        { 
            this.gridControl2[1 + j, i].CellValue = this.gridControl1[e.Range.Top + j, i].CellValue; 
        } 
    } 
} 
 
Sample link: GridControl 
 
Suggestion 3 
By default, GridControl does not have the direct support to update the values automatically in second GridControl. If you want to update the changes in GridControl2 based on GridControl1, you can use the CurrentCellEditingComplete event. Please refer to the below code example, 
 
Code example 
this.gridControl1.CurrentCellEditingComplete += gridControl2_CurrentCellEditingComplete; 
 
void gridControl2_CurrentCellEditingComplete(object sender, EventArgs e) 
{ 
    GridCurrentCell currentCell = this.gridControl1.CurrentCell; 
    this.gridControl2[currentCell.RowIndex, currentCell.ColIndex].CellValue = this.gridControl1[currentCell.RowIndex, currentCell.ColIndex].CellValue; 
             
    this.gridControl2.Refresh(); 
} 
 
Sample link: GridControl 
 
 
Please let us know, if we misunderstood your scenario. 
 
Regards, 
Mohanraj G 


JeeNew
Replied On July 18, 2017 01:34 AM

Hi Mohanraj,

Thanks, your suggestions worked well for me.

Thanks,


Mohanraj Gunasekaran [Syncfusion]
Replied On July 19, 2017 12:03 AM

Hi JeeNew,  
 
Thanks for your update. 
 
We are glad to know that your reported problem has resolved. 
 
Please let us know, if you have any concerns. 
 
Regards, 
Mohanraj G 


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.

;