如何在datatable中编辑Column的Row Value

时间:2014-09-07 21:32:12

标签: c# datatable

我使用以下方法得到了我的数据表的值

DataTable table = new DataTable();
table = GetTable();
string expression;
expression = "B.Id = " + textBox1.Text;
DataRow[] foundRows;
foundRows = table.Select(expression);
for (int i = 0; i < foundRows.Length; i++)
{

    sno.Text = foundRows[i][1].ToString();
    name.Text = foundRows[i][2].ToString();
    dcn.Text = foundRows[i][3].ToString();
    stat.Text = foundRows[i][4].ToString();
}

所以如何更改foundRows[i][4]

的值

2 个答案:

答案 0 :(得分:0)

您已使用DataRow's indexer获取第一列,因此我不明白这个问题。如果要访问第二个字段,请使用foundRows[i][1]

MessageBox.Show(foundRows[i][1].ToString());

如果要循环所有字段:

for(int i = 0; i < foundRows.Length; i ++)
{
    for(int c = 0; c < table.Columns.Count; c++)
        MessageBox.Show(foundRows[i][c].ToString());
}

我建议使用强类型Field - 扩展方法,该方法也支持可空类型。

MessageBox.Show(foundRows[i].Field<string>(0));

答案 1 :(得分:-1)

使用此

DataTable table = new DataTable();
table = GetTable();
string expression;
expression = "B.Id = " + textBox1.Text;
DataRow[] foundRows;
foundRows = table.Select(expression);
for (int i = 0; i < foundRows.Length; i++)
{

sno.Text = foundRows[i][1].ToString();
name.Text = foundRows[i][2].ToString();
dcn.Text = foundRows[i][3].ToString();
stat.Text = foundRows[i][4].ToString();
}


DataTable table = new DataTable();
    table = GetTable();
    string expression;
    expression = "B.Id = "+textBox1.Text;
    DataRow[] foundRows;
    foundRows = table.Select(expression);
    for(int i = 0; i < foundRows.Length; i ++)
    {
     if(button1.Text == "Make Preasent!")
     {
         foundRows[i][4] = true;
        table.AcceptChanges();
     }
     else
     {
         foundRows[i].SetField(4, false);
         table.AcceptChanges();
     }
    }