The ProblemWhen we are bind list of collection custom object in windows form DataGridView, this error occurred "Child list for field '' cannot be created". Error Code SnippetTo reproduce this problem, just create a simple windows form application with datagrid and add a column , then create a custom object. in my sample i have created "TagInfo", then you will create List with custom object taginfo.below code, using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SampleError
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
List<TagInfo> collectionInfo = new List<TagInfo>();
collectionInfo.Add(new TagInfo("Tag1", "Tag1"));
collectionInfo.Add(new TagInfo("Tag2", "Tag2"));
collectionInfo.Add(new TagInfo("Tag3", "Tag3"));
collectionInfo.Add(new TagInfo("Tag4", "Tag4"));
collectionInfo.Add(new TagInfo("Tag5", "Tag5"));
}
}
}
Then i have try to bind custom list to datagrid under the form load , liks using following code this.dataGridView1.DataSource = collectionInfo;
this.dataGridView1.DataMember = "TagInfo"; The application throw exception "Child list for field TagInfo cannot be created.",the reason is we have given lsit of object as datasource.the DataGridView know what is object have to bind to gridview through the DataBindingManager in .NET Framework.So we dont need specify the object name when we are list to DataGridView. SolutionsWhen we are bind to custom object collection to DataGridView through the list, just set the datasource only, dont need set the datamember. using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SampleError
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
List<TagInfo> collectionInfo = new List<TagInfo>();
collectionInfo.Add(new TagInfo("Tag1", "Tag1"));
collectionInfo.Add(new TagInfo("Tag2", "Tag2"));
collectionInfo.Add(new TagInfo("Tag3", "Tag3"));
collectionInfo.Add(new TagInfo("Tag4", "Tag4"));
collectionInfo.Add(new TagInfo("Tag5", "Tag5"));
this.dataGridView1.DataSource = collectionInfo;
}
}
}
I hope this save your time and help to all. |