-1

we have datagridview which is bound to an object(fiddler core) now this object as disctionart oflags We are not able to bind oflags to a datagridView but are able to bind other fields. We are using datagridview in C# that has as request id, url and oflag[“Deviation”] values Can you please send any sample code that can be used to bind Oflags

dataGridView1.Columns[0].Name = "id";
dataGridView1.Columns[0].HeaderText = "id";
dataGridView1.Columns[0].DataPropertyName = "id";
dataGridView1.Columns[1].Name = "fullUrl";
dataGridView1.Columns[1].HeaderText = "fullUrl";
dataGridView1.Columns[1].DataPropertyName = "fullUrl";
dataGridView1.Columns[2].Name = "LocalProcess";
dataGridView1.Columns[2].HeaderText = "LocalProcess";
dataGridView1.Columns[2].DataPropertyName = "LocalProcess";
dataGridView1.Columns[3].Name = "oFlags";
dataGridView1.Columns[3].HeaderText = "oFlags";
**dataGridView1.Columns[3].DataPropertyName = "oFlags[Deviation]";**
Chetan
  • 1
  • 1
  • Does this answer your question? [DataGrid Column Binding with Dictionary](https://stackoverflow.com/questions/38449597/datagrid-column-binding-with-dictionary) –  Aug 02 '21 at 14:03
  • If you have a “collection” of objects, and you set the `DataGridView.DataSource` to this collection of objects, then each publicly exposed property of that object will be translated to a column in the grid. This works for primitive and single value properties, HOWEVER, IF one of the publicly exposed properties of the object is another Class or “collection" (like a dictionary), then those properties will NOT have a column generated because those properties are “collections” and the grid is not smart enough to flatted the collection into a single value to display in a single cell. – JohnG Aug 02 '21 at 23:18
  • 2
    Basically the grid does not know how to put “multiple” items into a single cell. One possible solution is to create another property in the object that returns a flattened version of the collection. Then the grid will automatically create a column for that property. Another solution is to create a Master-Detail scenario with two grids. The first grid shows the objects in each row and the other grid would display the `oFlags` for the selected object in the first grid (Master-detail). Sorry if I am missing something. – JohnG Aug 02 '21 at 23:19

1 Answers1

-1
//Set AutoGenerateColumns False

dataGridView1.AutoGenerateColumns = false;

//Set Columns Count

dataGridView1.ColumnCount = 3;

//Add Columns

dataGridView1.Columns[0].Name = "CustomerId";
dataGridView1.Columns[0].HeaderText = "Customer Id";
dataGridView1.Columns[0].DataPropertyName = "CustomerID";
 
dataGridView1.Columns[1].HeaderText = "Contact Name";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].DataPropertyName = "ContactName";
 
dataGridView1.Columns[2].Name = "Country";
dataGridView1.Columns[2].HeaderText = "Country";
dataGridView1.Columns[2].DataPropertyName = "Country";

See also Bind data to DataGridView in Windows Forms Application in C# and VB.Net.

  • this does not solve my program as oflag is dictionary, dataGridView1.Columns[3].DataPropertyName = "oFlags[Deviation]" – Chetan Aug 02 '21 at 13:58