Tom
Tom

Reputation: 2230

DataTable.Rows.InsertAt won't accept new object[] where DataTable.Rows.Add does

I feel like I must not be doing something right here. I have a DataTable and it's currently able to take my arguments using the DataTable.Rows.Add method, but when I use DataTable.Rows.InsertAt it throws an error

What I'm currently doing with no issues:

dt.Rows.Add(new object[] { txtCompID.Text, scan(TrimStart('0'), txtEtaNum.Text, txtBinLocation.Text });

What I want to do (so the insert happens at the top), which is throwing "Argument 1: Cannot convert from 'object[]' to 'System.Data.DataRow'"

dt.Rows.InsertAt(new object[] { txtCompID.Text, scan.TrimStart('0'), txtEtaNum.Text, txtBinLocation.Text }, 0);

Upvotes: 1

Views: 3568

Answers (2)

Matt Burland
Matt Burland

Reputation: 45145

InsertAt doesn't support that. See here. You need to construct a DataRow first and then you can insert it.

Upvotes: 1

Adil
Adil

Reputation: 148140

You need to add row in dt that is of type of dt. You can use dt.NewRow() method to get the row for dt. You can do it this way.

DataRow dr = dt.NewRow();

dr[0] = "coldata1";
dr[1] = "coldata2";
dr[2] = "coldata3";
dr[3] = "coldata4";

dt.Rows.Add(dr);

Upvotes: 3

Related Questions