How to get a reference to a DataTable from the BindingSource object, if the structure is constructed like this:
BindingSource bsEmpl = new BindingSource(); bsEmpl.DataSource = ds; bsEmpl.DataMember = "table_empl"; dgvEml.DataSource = bsEmpl; BindingSource bsContr = new BindingSource(); bsContr.DataSource = bsEmpl; bsContr.DataMember = "rel_empl_contract"; dgvContr.DataSource = bsContr; where ds is an object of the DataSet class. rel_empl_contract - the name of the DataRelation, created as follows:
DataRelation rel; rel = new DataRelation("rel_empl_contract", t_empl.Columns["id"], t_contr.Columns["empl_id"]); ds.Relations.Add(rel); The essence of the problem: reading the dataGridView.DataSource field, get the DataTable table, so that you can use foreach to name the grid columns from the Caption property of the table
DataTable dt = (bsEmpl.DataSource as DataSet).Tables[bsEmpl.DataMember];? unless of course you have DataSet there - rdorn