从每个TableRow c#中删除TableCell

时间:2013-08-06 11:34:06

标签: c# datatable

我使用C#Table在aspx页面中显示HTML表。我想从该表中删除列。

DataTable getTxnOutput = // Get DataTable by Calling Stored Procedure;
Table txnOutputs = Generix.convertDataTable2HTMLTable(getTxnOutput);

foreach (TableRow trOutput in txnOutputs.Rows)
{
    if (trOutput.TableSection == TableRowSection.TableBody)
    {
    /*  Here i am doing some operation using column which i want to delete afterwards           */      
    txnOutputs.Rows[0].Cells.Remove(trOutput.Cells[6]); //Now delete that column
    }
}
Page.Controls.Add(txnOutputs);

但是上面的代码只会在第一行删除单元格。 如何在不使用更多循环的情况下删除每一行。

我找到了解决方案,但我可以在上面的循环中加入相同的内容......我只是想避免两个循环..

for (int k = 0; k < txnOutputs.Rows.Count; k++)
   {
   txnOutputs.Rows[k].Cells.Remove(txnOutputs.Rows[k].Cells[6]);
   }

3 个答案:

答案 0 :(得分:2)

不应该这一行

txnOutputs.Rows[0].Cells.Remove(trOutput.Cells[6]);

trOutput.Cells.Remove(trOutput.Cells[6]);

答案 1 :(得分:0)

你必须在if中包含forloop,并在if语句中使用类似于viewstate [X] = 1的变量,并检查

if(viewstate[X]==1)
{
  // logic here
}  
viewstate[X]=0;

运行循环 在For循环集viewstate[X]=0

结束之后

这样你只能运行一次循环,否则它将循环遍历每一行。

答案 2 :(得分:0)

试试这个:

    DataTable getTxnOutput = // Get DataTable by Calling Stored Procedure;
    Table txnOutputs = Generix.convertDataTable2HTMLTable(getTxnOutput);
    int i = 0;
    foreach (TableRow trOutput in txnOutputs.Rows)
    {
        if (trOutput.TableSection == TableRowSection.TableBody)
        {
        /*  Here i am doing some operation using column which i want to delete afterwards           */      
         txnOutputs.Rows[i].Cells.Remove(trOutput.Cells[6]); //Now delete that column
        }
        i++;
    }
    Page.Controls.Add(txnOutputs);
相关问题