【发布时间】: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