本文介绍了在数据集索引号或datagridveiw索引号上使用的正则表达式替换方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发人员,

我想替换数据集中的一些通配符.当我在所有有效的列和行上使用regex.replace方法时,但如果在特定的索引号上使用它,则该方法不起作用.
我还可以告诉您,我想对索引号而不是标头名称应用regex.replace方法.

请阅读以下代码:

Hi developers,

I want to replace some wildcards in dataset. When I used regex.replace method on all columns and rows that works but If I use it on particular index number, the method does not work.
I can also tell you that I want to apply a regex.replace method on a index number not on a header name.

Please read this code:

foreach (DataRow datarow in dv.Table.Rows)
{
  foreach (DataColumn datacol in dv.Table.Columns)
  {
    datarow[datacol] = System.Text.RegularExpressions.Regex.Replace(datarow[datacol].ToString(), comma.ToString(), slash.ToString());
    datarow[datacol] = System.Text.RegularExpressions.Regex.Replace(datarow[datacol].ToString(), colon.ToString(), Blank.ToString());
  }
}


请帮助我.


Please help me.

推荐答案

string strColon = colon.ToString();
string strBlank = Blank.ToString();
foreach (DataRow datarow in dv.Table.Rows)
   {
   foreach (DataColumn datacol in dv.Table.Columns)
      {
      string s = datarow[datacol].ToString();
      s = s.Replace(comma, slash);
      s = s.Replace(strColon, strBlank);
      datarow[datacol] = s;
      }
   }

您不需要像我一样将临时值存储在命名变量中,但是它使读取变得更容易.

如果您将DataColumn用作索引(如您目前所做的那样)或基于表中的行数/列数的整数索引,则此代码(以及您自己的代码)应该可以正常工作.如果没有,您将不得不给我们一些无效代码的示例,然后我们才能发表评论.编辑您的问题以包括不起作用的版本.

You don''t need to store the temporary value in a named variable as I have, but it makes it easier to read.

This code (and yours for that matter) should work fine if you use the DataColumn as an index (as you currently do) or an integer index based on the number of rows / columns in the table. If it doesn''t, you will have to give us a sample of the non-working code before we could comment. Edit your question to include the non-working version.


这篇关于在数据集索引号或datagridveiw索引号上使用的正则表达式替换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 05:23