使用条件从数据表中删除列

时间:2014-07-19 07:45:24

标签: c# datatable

我有一个数据表,如果所有单元格的值都是“0”,我必须删除所有列。数据表有一个最后一行,称为Total,我应该在其中验证条件。我试过这个功能:

for (int counter = dt.Columns.Count-1; counter > 0; counter--) { if (dr[counter]=="0") {dt.Columns.RemoveAt(counter);} }

DataTable的架构是:
`DataTable dt = new DataTable();             SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings [“bootstrap1”]。ConnectionString);                 connection.Open();                 string sqlStatement =“选择查询”

            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)
            {

                //Pivot the Original data from the DataTable by calling the
                //method PivotTable and pass the dt as the parameter

                DataTable pivotedTable = ArangeData(dt);
                GridView1.DataSource = pivotedTable;
                GridView1.DataBind();
            }
        }
}
private DataTable ArangeData(DataTable dt)
{
    DataTable newdt = new DataTable();
    SqlServerUserStore ss = new SqlServerUserStore();
    Televizioni channelslist = ss.GetChannelsList();
    DataRow dr = null;

    newdt.Columns.Add(new DataColumn("Sectors", typeof(String)));
    for (int i = 1; i < channelslist.televizioni.Count + 1; i++)
    {
        newdt.Columns.Add(new DataColumn(channelslist.televizioni[i - 1], typeof(String)));
    }
    newdt.Columns.Add(new DataColumn("Total", typeof(String)));

    int lastsector = -1;

    int shuma = 0;

    bool first = true;

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        int sector = Convert.ToInt16(dt.Rows[i][0].ToString());
        if (sector != lastsector)
        {

            if (first)
            {
                first = false;
            }
            else
            {
                dr[channelslist.idtelevizioni.Count + 1] = shuma;
                newdt.Rows.Add(dr);
            }

            lastsector = sector;
            dr = newdt.NewRow();
            for (int j = 0; j < newdt.Columns.Count; j++)
            {
                dr[j] = "0";
                dr[0] = dt.Rows[i][3];
            }
            shuma = 0;
            shuma += Convert.ToInt16(dt.Rows[i][2].ToString());
            dr[merrindeksinNgaIdTelevizioni(Convert.ToInt16(dt.Rows[i][1].ToString()), channelslist) + 1] = dt.Rows[i][2];

        }
        else
        {

            dr[merrindeksinNgaIdTelevizioni(Convert.ToInt16(dt.Rows[i][1].ToString()), channelslist) + 1] = dt.Rows[i][2];
            shuma += Convert.ToInt32(dt.Rows[i][2].ToString());
        }

    }

    dr[channelslist.idtelevizioni.Count + 1] = shuma;
    newdt.Rows.Add(dr);



    dr = newdt.NewRow();
    dr[0] = "Total";
    int shumatore = 0;
    for (int i = 1; i < newdt.Columns.Count; i++)
    {
        shumatore = 0;
        for (int j = 0; j < newdt.Rows.Count; j++)
        {
            shumatore += Convert.ToInt32(newdt.Rows[j][i].ToString());
        }
        dr[i] = shumatore;

    }
     newdt.Rows.Add(dr);
     return newdt;

`

其中dt是DataTable。但是,它似乎不起作用,所以我无法理解出了什么问题。有人可以帮助我吗?

0 个答案:

没有答案