如何在一列中插入值? C#

时间:2013-12-24 08:22:52

标签: c#

我有以下代码:

 foreach (string value in data1)
 {         
     var match = Regex.Match(value, @"(?<Number>\d+)(?<Text>.*)");
     var number = match.Groups["Number"].Value;
     var text = match.Groups["Text"].Value;
     string result2 = string.Format("{0}", text);
     data.Rows.Add(result2);
     dataGridView1.DataSource = data;
 }

此代码会将数据添加到我的第一列。如何修复它以将数据添加到第二列?

3 个答案:

答案 0 :(得分:0)

 DataColumn Col   = datatable.Columns.Add("Column Name", typeof(Boolean));

Col.SetOrdinal(0);

答案 1 :(得分:0)

DataRow newRow = data.NewRow();

newRow["Text"] = text; //if the column name is Text 

data.Rows.Add(newRow);

答案 2 :(得分:0)

我认为你需要首先将绑定数据源的行移出循环块,

foreach (string value in data1)
{

    var match = Regex.Match(value, @"(?<Number>\d+)(?<Text>.*)");
    var number = match.Groups["Number"].Value;
    var text = match.Groups["Text"].Value;
    string result2 = string.Format("{0}", text);
    data.Rows.Add(result2);
}

dataGridView1.DataSource = data;