2

I have the following set of codes for adding values to my DataGridView through DataTable as my datasource. However, it keeps on giving me the error "Child list for field tbl_main cannot be created". Anybody can help me identify the problem?

dgvMySchedule.Columns.Clear();
dgvMySchedule.Rows.Clear();
dgvMySchedule.ClearSelection();

dataSet.Tables.Add(tbl_main);
dgvMySchedule.DataSource = dataSet;
dgvMySchedule.DataMember = "tbl_main";

tbl_main.Columns.Add("TIME");
tbl_main.Columns.Add("CLASS");

DataRow row;
dgvMySchedule.RowTemplate.Height = 8;
for (int i = 0; i <= 71; i++)
{
    row = tbl_main.NewRow();
    row["TIME"] = i;
    row["CLASS"] = i;
    tbl_main.Rows.Add(row);
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
Amore
  • 101
  • 3
  • 11
  • Why are you creating a dataset for one table? Anyway you still need to add the created row to the table and rebind it. – Poody Oct 31 '16 at 09:00

3 Answers3

3

It looks like your tbl_main is unnamed, or it has a different name than "tbl_main". There are three possible solutions:

  1. Remove the DataSet, instead use dgvMySchedule.DataSource = tbl_main;
  2. If you need/want the DataSet, add tbl_main.TableName = "tbl_main";
  3. If you need/want the DataSet, remove the line saying dgvMySchedule.DataMember = "tbl_main" (the grid will then automatically use the one DataTable present).
Peter B
  • 22,460
  • 5
  • 32
  • 69
  • hi! I finally realized my mistake. I forgot to initialize my datatable during declaration. I used DataTable tbl_main = new DataTable(); instead of DataTable tbl_main = new DataTable("tbl_main"); The changes pretty much answered my concern. Thank you! – Amore Nov 01 '16 at 03:10
1

I finally realized my mistake. I forgot to initialize my datatable during declaration. I used DataTable tbl_main = new DataTable(); instead of DataTable tbl_main = new DataTable("tbl_main"); The changes pretty much answered my concern. Thank you!

Amore
  • 101
  • 3
  • 11
  • please mark this as the answer so that this question wont linger as an open ended question. – Nap Jun 04 '18 at 00:20
0

I had this problem using binding in winforms. I had set binding to a non existent field name.

Kirsten
  • 15,730
  • 41
  • 179
  • 318