在数据表中显示行号

时间:2018-11-22 21:06:19

标签: c# datatable

我正在尝试使用“ TASK#”列来显示行号,但它始终获得值0。我在做什么错了?:

        public void LoadTasksTable()
    {
        try
        {
            int i = 0;
            int index = 0;
            foreach (DataRow row in tasksDT.Rows)
            {
                index = i;
                dataTasks.Rows[i].Cells["TASK #"].Value = i;
                dataTasks.Rows[i].Cells["PLATFORM"].Value = dataTasks.Rows[i].Cells["PLATFORM"].Value;
                dataTasks.Rows[i].Cells["TASK TYPE"].Value = dataTasks.Rows[i].Cells["TASK TYPE"].Value;
                dataTasks.Rows[i].Cells["KEYWORD"].Value = dataTasks.Rows[i].Cells["KEYWORD"].Value;
                dataTasks.Rows[i].Cells["LINK"].Value = dataTasks.Rows[i].Cells["LINK"].Value;
                dataTasks.Rows[i].Cells["PROFILE"].Value = dataTasks.Rows[i].Cells["PROFILE"].Value;
                i++;
            }
            connection.Close();
        }
        catch { }
    }


tasksDA = new OleDbDataAdapter("Select * From Tasks", connection);
        tasksDA.Fill(tasksDT);
        dataTasks.DataSource = tasksDT;

1 个答案:

答案 0 :(得分:0)

  public void LoadTasksTable()
{
    try
    {
        int index = 0;
        for(var i = 0; i < tasksDT.Rows.Count; i++)
        {
            dataTasks.Rows[i].Cells["TASK #"].Value = i;
            dataTasks.Rows[i].Cells["PLATFORM"].Value = dataTasks.Rows[i].Cells["PLATFORM"].Value;
            dataTasks.Rows[i].Cells["TASK TYPE"].Value = dataTasks.Rows[i].Cells["TASK TYPE"].Value;
            dataTasks.Rows[i].Cells["KEYWORD"].Value = dataTasks.Rows[i].Cells["KEYWORD"].Value;
            dataTasks.Rows[i].Cells["LINK"].Value = dataTasks.Rows[i].Cells["LINK"].Value;
            dataTasks.Rows[i].Cells["PROFILE"].Value = dataTasks.Rows[i].Cells["PROFILE"].Value;

        }
        connection.Close();
    }
    catch { }
}

我猜想使用普通的for循环应该可以解决问题,这没有经过测试,但是应该可以使用!!希望对您有帮助