Instead of the following codes below:
List<Travel> travels = logic.GetAllTravels(); DgvRecorridos.DataSource = travels;
Do it:
List<Travel> travels = logic.GetAllTravels(); BindingSource bs = new BindingSource(); bs.DataSource = travels; DgvRecorridos.AutoGenerateColumn = false; DgvRecorridos.DataSource = bs;
Then add the columns manually:
DataGridViewColumn col1 = new DataGridViewTextBoxColumn(); col1.DataPropertyName = "Service.Name"; col1.HeaderText = "Service Name"; dataGridView1.Columns.Add(col1); DataGridViewColumn col2 = new DataGridViewTextBoxColumn(); col2.DataPropertyName = "City.Name"; col2.HeaderText = "City Name"; dataGridView1.Columns.Add(col2); DataGridViewColumn col3 = new DataGridViewTextBoxColumn(); col3.DataPropertyName = "City.Name"; col3.HeaderText = "Destiny Name"; dataGridView1.Columns.Add(col3); DataGridViewColumn col4 = new DataGridViewTextBoxColumn(); col4.DataPropertyName = "Price"; col4.HeaderText = "Price"; dataGridView1.Columns.Add(col4);
Then add a cell formatting event handler for the DataGridView:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dataGridView1.Rows[e.RowIndex].DataBoundItem != null && dataGridView1.Columns[e.ColumnIndex].DataPropertyName.Contains(".")) { e.Value = BindProperty(dataGridView1.Rows[e.RowIndex].DataBoundItem, dataGridView1.Columns[e.ColumnIndex].DataPropertyName); } } private string BindProperty(object property, string propertyName) { string retValue = ""; if (propertyName.Contains(".")) { PropertyInfo[] arrayProperties; string leftPropertyName; leftPropertyName = propertyName.Substring(0, propertyName.IndexOf(".")); arrayProperties = property.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in arrayProperties) { if (propertyInfo.Name == leftPropertyName) { retValue = BindProperty(propertyInfo.GetValue(property, null), propertyName.Substring(propertyName.IndexOf(".") + 1)); break; } } } else { Type propertyType; PropertyInfo propertyInfo; propertyType = property.GetType(); propertyInfo = propertyType.GetProperty(propertyName); retValue = propertyInfo.GetValue(property, null).ToString(); } return retValue; }
For a complete guide on cell formatting, check out Antonio Bello's blog post here , where I got the idea. ^ _ ^ I also asked here about the same question two days ago and received the same answers as you, and I know that this is not what you also want to do. Hope this helps you.
source share