Query |
Solution |
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. |
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;
}
}
}
| |
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
|
Hi Mohanraj,
Thanks, your suggestions worked well for me.
Thanks,