拆分字符串并考虑datagridview问题 [英] Split string and take into datagridview problems

查看:86
本文介绍了拆分字符串并考虑datagridview问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串,我正在使用此代码分割此字符串:

 split = ast.Split(new Char [] {'!'}}; 



之后我正在制作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);
table.Columns.Add(col);
}



 DataRow row = table.NewRow(); 



之后我希望这个拆分的字符串进入单元格而我正在使用我正在尝试的代码但是有一个问题我的字符串长度可能是27或者27 + 27我的字符串长度是常数27,所以我有第27列,但是如果我的代码(我尝试过的话)看到长度超过27就错了我

附加信息:找不到第27列。



请帮我这样做



我尝试了什么:



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

row [i] = split [i];
}

table.Rows.Add(row);



}
this.dataGridView1.DataSource = table;

解决方案

要做的第一件事就是看看是否有另一个分隔符:听起来你有27个元素用!分隔,然后是第二个用!分隔,然后是第三个。

在这种情况下,使用第二个分隔符分隔每一组是正常的:

 1A!1B!...!1Z!1ZA | 2A!2B!... 

(通常它是一个让人类可读的新行。)所以首先查看你的数据并检查它。如果你这样做,那么很简单:通过第二个分隔符将其溢出到每一行,然后拆分每一行并添加:

  string  [] rows = ast.Split('  |'); 
foreach string row in 行)
{
string cells = row.Split(' ');
// 在此处将单元格添加到新行。
}


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);
                        table.Columns.Add(col);
                    }


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];
                         }
                         
                         table.Rows.Add(row);
  

                      
}
                     this.dataGridView1.DataSource = table;

解决方案

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:

1A!1B!...!1Z!1ZA|2A!2B!...

(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.
   }


这篇关于拆分字符串并考虑datagridview问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆