Click here to Skip to main content
15,882,376 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i have string and i am splting this string using this code :
split = ast.Split(new Char[] { '!' });

after that i am making DataTable
DataTable table = new DataTable();

DataGridViewRow addRow = new DataGridViewRow();

int columnCount = 27;
                    for (int i = 1; i <= columnCount; i++)
                        DataColumn col = new DataColumn("col" + i);

DataRow row = table.NewRow();

after this i want to this splited string take into cells and i am using code that i am trying but there is 1 problem my string length may be 27 or 27+27 my string length is growig with constant number 27 soo i have columns 27 but if my code(what i have tryed) see that length is more then 27 it is erroring me
Additional information: Cannot find column 27.

plz help me do this

What I have tried:

                         for (int i = 0; i <= split.length; i++)

                             row[i] = split[i];

                     this.dataGridView1.DataSource = table;
Updated 24-Apr-17 2:34am

1 solution

The first thing to do is to see if there is another separator in there: it sound like you have 27 elements separated by "!", then an second set also separated by "!", then a third.
It would be normal under these circumstances to have a second separator to delimit each set:
(often it's a newline to make it humanly readable.) So start by looking at your data and checking that. If you do, then it's simple: spilt it into each row via teh second separator, then split each row and add that:
string[] rows = ast.Split('|');
foreach (string row in rows)
   string cells = row.Split('!');
   // Add cells to a new row here.
Share this answer
GTR0123 24-Apr-17 8:40am    
the problem is that there is no other seperator for example string looks like
i want this 1 2 3 go on 1 cell
and 4 5 6 on another sell but there can be more then 2 rows
sameer549 24-Apr-17 8:45am    
give your exact input string.. is it contains multiple lines as you given above?
GTR0123 24-Apr-17 8:47am    
if this string is coming i mean length it is doing well everything
when it is twice more it is erroring me that
OriginalGriff 24-Apr-17 10:18am    
That data is broken into "natural" lengths: you even placed them on separate lines.
It looks like PABX log data to me, and I suspect that it's in a very specific format, probably the lines you have broken it into.
Grab your raw data again, and use a hex editor to look at it closely: I suspect that each line is terminated with a #0D or #0A (CR or LF ASCII characters) and you should be observing that.

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