C# DataTable手动行插入:此行已属于此表

C# DataTable手动行插入:此行已属于此表,c#,for-loop,gridview,datatable,C#,For Loop,Gridview,Datatable,我试图用手动创建的数据表填充gridview。我有两个表格,一个是“人力资源空缺”,第二个是“人力资源概况”。有匹配的列“‘指定’和‘规模’。在人力资源部空缺中有一列‘工作’ 我必须在gridview中显示取决于“工作”数量的记录数量。如果我在工作中有(例如3个数量),那么我将检查配置文件中是否插入了3个具有相同“名称”的配置文件,其中“hr\U空缺.工作”列中有3个数量 假设输入了两个配置文件,gridview将显示“hr_profile”中的两条记录和一个仅包含该名称的空行。我已经编写了一些

我试图用手动创建的数据表填充gridview。我有两个表格,一个是“人力资源空缺”,第二个是“人力资源概况”。有匹配的列“‘指定’和‘规模’。在人力资源部空缺中有一列‘工作’

我必须在gridview中显示取决于“工作”数量的记录数量。如果我在工作中有(例如3个数量),那么我将检查配置文件中是否插入了3个具有相同“名称”的配置文件,其中“hr\U空缺.工作”列中有3个数量

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

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();
    }
}
User User=新用户();
Util myUtil=new Util();
DbManager DbManager=新的DbManager();
字符串strSQL=“”;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
DataTable dttempEmployee=新数据表(“TempEmployee”);
strSQL=“从配置文件中选择hfmiscode,其中hfmiscode='1';
dttempeployee=dbManager.FillDataTable(strSQL);
DataRow dr=dttempEmployee.NewRow();
strSQL=“从人力资源单元空缺中选择*,其中hfmiscode='398101'按转换顺序(整数、小数位数)描述”;
DataTable DTException=dbManager.FillDataTable(strSQL);
如果(dtemptance.Rows.Count>0)
{
int introwcount=0;
string strDesignation=string.Empty;
string strWorking=string.Empty;
IntrowcountSpacement=dtSpacement.Rows.Count;
for(int i=0;i0 | | dtProfileGet.Rows.Count==Convert.ToInt32(strWorking))
{
如果(j!=转换为32(标准工作))
{
//dttempEmployee.NewRow();
if(dtProfileGet.Rows[j][“hfmiscode”]==DBNull.Value)
{
dr[“hfmiscode”]=“-”;
}
其他的
{
dr[“hfmiscode”]=Convert.ToString(dtProfileGet.Rows[j][“hfmiscode”]);
}
}
}
其他的
{
如果(j!=转换为32(标准工作))
{
//dttempEmployee.NewRow();
dr[“hfmiscode”]=“-”;
}
}
dttempEmployee.Rows.Add(dr);
}
}
}
GridView1.DataSource=dttempEmployee;
GridView1.DataBind();
}
}

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

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

这里的问题是,在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);
 }
for(int j=0;j
谢谢。但我遇到了一个新问题。经过一些迭代之后,突然出现了“位置2没有行”的错误。如果(dtProfileGet.Rows[j][“hfmiscode”]==DBNull.Value){dr[“hfmiscode”]=“-”;},则出现此错误这背后的原因是dtProfileGet只有2行,而您正试图通过执行dtProfileGet.rows[2]来访问第三行。您需要确保向dtProfileGet.RowsDear传递的值不超过totalrows-1。我无法在其中找到确切的错误点。如果我给您我的teamview id。请您连接并告诉我问题。我将非常感激。请您现在不在我的计算机附近。您能调试代码并找出哪一行导致错误吗错误?第59行:如果(dtProfileGet.Rows[j][“hfmiscode”]==DBNull.Value)专业提问提示:不要添加紧急/尽快乞讨-你的问题并不比任何人都重要。在你的帖子上下功夫,确保句子以大写字母开头,对于“I”也是如此“当提到你自己的时候。不要通过TeamViewer寻求一对一的支持——这比志愿者通常能提供的时间要长得多。