string rs = m.Groups[1].Value;
string[] result = rs.Split(',');
DataRow row = dt.NewRow();
row[0] = result[0];
row[1] = result[1];
//....
dt.Rows.Add(row);
string[] result = rs.Split(',');
DataRow row = dt.NewRow();
row[0] = result[0];
row[1] = result[1];
//....
dt.Rows.Add(row);
确定rs的值是以,号分隔的。
或者你试试添加一条记录的时候用NewRow添加。看看行不行。
楼主的写法也没有错误。怀疑是result的长度=1
运行到row[1]=result[1];的时候
string[] result = rs.Split(','); 一次就一个字段
这是怎么回事
{
string rs = m.Groups[1].Value;
string[] result = rs;
dt.Rows.Add(result);
}
每次循环到这里 都是一个值
dt.Rows.Add(result);这里 我是想把数据都放到DataTable中
然后再绑定gridview
你是想抓取tr中的td内容,每个tr中有16个td
想获取这16个然后绑定到gridview中?
tr有很多 没个tr中有16个td
DataTable dt = new DataTable();
dt.Columns.Add("shuiguo");
dt.Columns.Add("dianqi");
dt.Columns.Add("qiche");
string str = File.ReadAllText(@"E:\txt.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)(?<=<div[^>]*?id=""a""[^>]*?>(?:(?!</?div).)*)<tr[^>]*?>(.*?)</tr>");
Regex reg2 = new Regex(@"(?is)(?<=<td[^>]*?>)\s*(?:<a[^>]*?>)?(.*?)\s*(?:</a>)?(?=\s*</td>)");
foreach (Match m in reg.Matches(str))
{
MatchCollection match = reg2.Matches(m.Groups[1].Value);
DataRow row = dt.NewRow();
for (int i = 0; i < match.Count; i++)
row[i] = match[i].Groups[1].Value;
dt.Rows.Add(row);
}
GridView3.DataSource = dt;
GridView3.DataBind();
{
MatchCollection match = reg2.Matches(m.Groups[1].Value);
DataRow row = dt.NewRow();
for (int i = 0; i < match.Count; i++)
row[i] = match[i].Groups[1].Value;
dt.Rows.Add(row);
}
循环进不去
直接跳到gridview绑定那了