【问题标题】:DataTable Manual Rows Insert: This row already belong to this tableDataTable Manual Rows Insert:该行已经属于该表
【发布时间】:2017-06-01 04:30:25
【问题描述】:

我正在尝试使用手动创建的数据表填充网格视图。我有两张表,一张是“hr_vacancy”,第二张是“hr_profile”。有匹配的列“'designation'和'scale'。在hr_vacancy中有一个'working'列。

我必须在 gridview 中显示取决于“工作”数量的记录数。如果在工作中我有(例如 3 个数量),那么我将检查配置文件天气有 3 个配置文件插入了相同的“名称”,在“hr_vacancy.working”列中有 3 个数量。

假设如果输入了两个配置文件,gridview 将显示来自“hr_profile”的两条记录和一个仅包含该名称的空行。我写了一些附加的代码。但是当我将新的'dr'绑定到数据表时,它显示错误'这一行已经属于这个表'

User user = new User();
Util myUtil = new Util();
DbManager dbManager = new DbManager();
string strSQL = "";

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dttempEmployee = new DataTable("TempEmployee");
        strSQL = "Select hfmiscode from profile where hfmiscode='1'";
        dttempEmployee = dbManager.FillDataTable(strSQL);
        DataRow dr = dttempEmployee.NewRow();

        strSQL = "select * from hr_vacancy where hfmiscode='398101' order by CONVERT(int, scale) desc";
        DataTable dtVacancy = dbManager.FillDataTable(strSQL);

        if (dtVacancy.Rows.Count > 0)
        {
            int intRowCountVacancy=0;
            string strDesignation = string.Empty;
            string strWorking = string.Empty;

            intRowCountVacancy = dtVacancy.Rows.Count;

            for (int i = 0; i < intRowCountVacancy; i++)
            {
                strDesignation = Convert.ToString(dtVacancy.Rows[i]["Designation"]);
                strWorking = Convert.ToString(dtVacancy.Rows[i]["working"]);

                for (int j = 0; j < Convert.ToInt32(strWorking); j++)
                {
                    strSQL = "select * from profile where hfmiscode='398101' and designation='" + strDesignation +"'";
                    DataTable dtProfileGet = dbManager.FillDataTable(strSQL);

                    if (dtProfileGet.Rows.Count > 0 || dtProfileGet.Rows.Count == Convert.ToInt32(strWorking))
                    {
                        if (j != Convert.ToInt32(strWorking))
                        {
                           // dttempEmployee.NewRow();

                            if (dtProfileGet.Rows[j]["hfmiscode"] == DBNull.Value)
                            {
                                dr["hfmiscode"] = "-";
                            }
                            else
                            {
                                dr["hfmiscode"] = Convert.ToString(dtProfileGet.Rows[j]["hfmiscode"]);
                            }
                        }
                    }
                    else
                    {
                        if (j != Convert.ToInt32(strWorking))
                        {
                               // dttempEmployee.NewRow();
                                dr["hfmiscode"] = "-";
                        }
                    }
                    dttempEmployee.Rows.Add(dr);
                }
            }
        }
        GridView1.DataSource = dttempEmployee;
        GridView1.DataBind();
    }
}

注意:我只使用了 hr_profile 表中的一列“hfmiscode”。稍后我会添加更多的列。

【问题讨论】:

  • 提出问题的专业提示:不要添加紧急/尽快乞求 - 你的问题并不比其他人更重要。努力写好你的帖子,确保句子以大写字母开头,在提到你自己时,“I”也是如此。不要通过 TeamViewer 寻求一对一的支持 - 这需要的时间远远超过志愿者通常能够提供的时间。

标签: c# for-loop gridview datatable


【解决方案1】:

当您执行 datatable.NewRow() 时,它会在 datatable 中创建,其中所有列的默认值。它返回新创建的数据行对象,您需要使用适当的值填充列。

这里的问题是您在 for 循环之外有 datatable.NewRow() 语句,并且在 for 循环中调用了 datatable.Row.Add。 所以基本上你是在尝试多次添加同一行。

您需要将 datatable.NewRow() 语句移动到内部 for 循环中,如下所示。

 for (int j = 0; j < Convert.ToInt32(strWorking); j++)
 {
    var dr = dttempEmployee.NewRow();

    // Other logic

    dttempEmployee.Rows.Add(dr);
 }

【讨论】:

  • 谢谢。但我遇到了一个新问题。经过一些迭代后,突然收到此错误“位置 2 处没有行。”。这个错误来自if (dtProfileGet.Rows[j]["hfmiscode"] == DBNull.Value) { dr["hfmiscode"] = "-"; }
  • 这背后的原因是 dtProfileGet 只有 2 行,而您正试图通过 dtProfileGet.Rows[2] 访问第三行。您需要确保传递的值不超过 totalrows-1 到 dtProfileGet.Rows
  • 亲爱的,我无法在其中找到确切的错误点。如果我给你我的teamview id。请你连接并告诉我问题。将不胜感激。请
  • 我现在不在我的电脑附近。你能调试你的代码并找出是哪一行导致了错误吗?
  • 第 59 行:if (dtProfileGet.Rows[j]["hfmiscode"] == DBNull.Value)
猜你喜欢
  • 1970-01-01
  • 2013-12-21
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多