1

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);

4
  • Can you tell me why you want it inserted at the top? What's the driver for that? Commented Sep 27, 2012 at 18:33
  • 1
    @Blam, nope the reason it can't be done is because InsertAt requires a data row to be passed to it. Commented Sep 27, 2012 at 18:35
  • 1
    DataRow.Add has two Overloads one takes an object which you are using the other takes a DataRow. DataRow.InsertAt only has the DataRow Method. Commented Sep 27, 2012 at 18:42
  • Thanks everyone for the input. It was most definitely that InsertAt only has one overload. I was able to create a new DataRow to do this. Commented Sep 27, 2012 at 18:46

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.