There are two DataGridViews with the same number of columns whose widths also correspond. Actually, they are located under each other. And the second shows the results of the first. How to make, that when the user changes width in one of DataGridView, it would change and in the second. The first one that comes to mind is to use the ColumnWidthChanged event, but if we change the column widths of another DataGridView in the handler, it will also raise an event, and so on until the exceptions sprinkle. So how to do?
1 answer
We sign both DataGridView
on the same event handler. In it we check the width of the columns: if they match, then we simply exit the method. Otherwise, we make the width of the second datagrid column as the first one.
private void DataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) { DataGridView first = (DataGridView)sender; DataGridView second = first == dataGridView1 ? dataGridView2 : dataGridView1; int index = e.Column.Index; if (first.Columns[index].Width == second.Columns[index].Width) return; second.Columns[index].Width = first.Columns[index].Width; }
|