如何在波纹管代码中从forech循环内的datarow行中删除row [6] [英] How can i delete row[6] from datarow row inside forech loop in bellow code

查看:70
本文介绍了如何在波纹管代码中从forech循环内的datarow行中删除row [6]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   protected void gvTimeSheet_DataBound(object sender, EventArgs e)
    
    {
        DataTable dt = new DataTable();
        int intUserID = TypeConvert.IsNullInt32(ddlUsers.SelectedValue, 0);
        DateTime fromDate = Convert.ToDateTime(UCFromDate.selectedDate);
        DateTime toDate = Convert.ToDateTime(UCToDate.selectedDate);
        string strQuery = "EXEC SPRptUserTimeSheet_AP @User=" + intUserID + ",@dateFrom='" + fromDate + "',@dateTo='" + toDate + "'";
        dt = DataHelper.ExecuteQuery(strQuery);
        dt.Columns.Add("Booked Hrs");
        dt.Columns.Add("Time Zone");

        

        foreach (DataRow row in dt.Rows)
        {
            string userName = row[0].ToString();
            string bookedHr = row[6].ToString();
            string timezone = row[7].ToString();

            if (userName == string.Empty)
            {
                row[0] = ddlUsers.SelectedItem.Text;
            }

            if (bookedHr == string.Empty)
            {
                row["Booked Hrs"] = 0.0;
            }
            if (bookedHr != string.Empty)
            {
                row["Booked Hrs"] = objCommon.fnGetEffortInHrMinFormat(Convert.ToInt32(bookedHr));
            }

            if (timezone == "0")
            {
                row["Time Zone"] = "Offshore";

            }
            if (timezone == "1")
            {
                row["Time Zone"] = "Onsite";
            }

        }
        ViewState["dtExport"] = dt;
    }
}

What I have tried:

protected void gvTimeSheet_DataBound(object sender, EventArgs e)
    
    {
        DataTable dt = new DataTable();
        int intUserID = TypeConvert.IsNullInt32(ddlUsers.SelectedValue, 0);
        DateTime fromDate = Convert.ToDateTime(UCFromDate.selectedDate);
        DateTime toDate = Convert.ToDateTime(UCToDate.selectedDate);
        string strQuery = "EXEC SPRptUserTimeSheet_AP @User=" + intUserID + ",@dateFrom='" + fromDate + "',@dateTo='" + toDate + "'";
        dt = DataHelper.ExecuteQuery(strQuery);
        dt.Columns.Add("Booked Hrs");
        dt.Columns.Add("Time Zone");

        

        foreach (DataRow row in dt.Rows)
        {
            string userName = row[0].ToString();
            string bookedHr = row[6].ToString();
            string timezone = row[7].ToString();

            if (userName == string.Empty)
            {
                row[0] = ddlUsers.SelectedItem.Text;
            }

            if (bookedHr == string.Empty)
            {
                row["Booked Hrs"] = 0.0;
            }
            if (bookedHr != string.Empty)
            {
                row["Booked Hrs"] = objCommon.fnGetEffortInHrMinFormat(Convert.ToInt32(bookedHr));
            }

            if (timezone == "0")
            {
                row["Time Zone"] = "Offshore";

            }
            if (timezone == "1")
            {
                row["Time Zone"] = "Onsite";
            }

        }
        ViewState["dtExport"] = dt;
    }
}

推荐答案

不要更改循环的可枚举数。它总会打破循环。



而是将所需的行添加到新的数据表并使用它。



有一些选项可以将列结构从数据表克隆到新数据
Don't change the enumerable you are looping through. It will always break the loop.

Instead add the required rows to a new datatable and use that.

There are options to clone the column structure from a datatable to a new one


这篇关于如何在波纹管代码中从forech循环内的datarow行中删除row [6]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆