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. Image for the cookie policy date

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

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?



3 Replies

MG Mohanraj Gunasekaran Syncfusion Team July 17, 2017 11:34 AM UTC

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 



JE JeeNew July 18, 2017 05:34 AM UTC

Hi Mohanraj,

Thanks, your suggestions worked well for me.

Thanks,



MG Mohanraj Gunasekaran Syncfusion Team July 19, 2017 04:03 AM UTC

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 


Loader.
Live Chat Icon For mobile
Up arrow icon