While we add the data to datatable using foreach loop it will get an exception i.e., "input array is longer than the number of columns in this table".
Example:-
DataTable dt = new DataTable();
public void GetData()
{
List emplist = db.Employees.ToList();
foreach(var emp in emplist)
{
dt.Row.Add(emp.Name,emp.Address,emp.City); //Input array is longer than the...
}
dataGridView.DataSource=dt;
}
public void Form_Load(object sender,EventArgs e)
{
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Address",typeof(string));
dt.Solumns.Add("City",typeof(string));
}

Jaish MathewsRank #225 · 11K reputationPosted Feb 9, 2019, 3:16 AM EST
Faisal PathanRank #291 · 8.6K reputationPosted Feb 8, 2019, 11:19 PM EST
You need to first add columns to this data table.
Salman BegRank #284 · 8.8K reputationPosted Feb 8, 2019, 11:22 PM EST
Ravikiran Suravarapu106 reputationPosted Feb 8, 2019, 11:23 PM EST