Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
this is my code I would like to fill a datarow with values for the columns- but every time

it comes to IndexOutOfRangeException (column 0 not found) in this line
DSRow[x] = DatStr[x];


- but I didn't know why?

I have set 12 columns when I create the datatable

What I have tried:

DataRow DSRow = DTB.NewRow();
                            for (int x = 0; x < 12; x++)
                            {
                                DSRow[x] = DatStr[x];

                            }

                            DTB.Rows.Add(DSRow);
Posted
Updated 19-Jul-21 4:30am
v5

1 solution

"Index out of range" means exactly that: you are trying to access an array element via an index (ion this case x) that doesn't exist.

At a guess - and from the tiny piece of code you show us that is all it can be - either your DataTable has insufficient (or none) columns defined, or DatStr does.

Why? We can't tell as it needs your code running with your data to find out.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Jim Jupiter 2021 19-Jul-21 9:29am    
I've done it already

Here are more code

public void init()
{

DTB.Columns.Add("ID", typeof(string));
DTB.Columns.Add("Name", typeof(string));
DTB.Columns.Add("I1", typeof(string));
DTB.Columns.Add("I2", typeof(string));
DTB.Columns.Add("I3", typeof(string));
DTB.Columns.Add("I4", typeof(string));
DTB.Columns.Add("I5", typeof(string));
DTB.Columns.Add("I6", typeof(string));
DTB.Columns.Add("I7", typeof(string));
DTB.Columns.Add("I8", typeof(string));
DTB.Columns.Add("I9", typeof(string));
DTB.Columns.Add("10", typeof(string));

}
DatStr is here

string[] DatStr = new string[11]; and

while ((SRline = Sreader.ReadLine()) != null)
{

DatStr[DFid] = SRline;


As I mentioned at the line it says column 0 not be found ...

Maybe the assignment is not right - need I a separator?

Declaration and assignment to DatStr works
OriginalGriff 19-Jul-21 9:44am    
What did the debugger show you was in the Columns collection of the DGV?
Jim Jupiter 2021 19-Jul-21 10:00am    
??? Here I didnt have a datagridview - just reading file to a datatable
OriginalGriff 19-Jul-21 10:13am    
Sorry, my bad - what did the debugger show you was in the Columns collection of the DT?
Jim Jupiter 2021 19-Jul-21 10:31am    
Where can I find it?

If I debug it and hover over DTB - below ChildRelations ColumnsCount says 0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900