我正在尝试以pragmatically方式向dataGrid添加新的新行。首先,代码检查该行是否已经存在,如果不存在,则将新行放入;否则它不会进入DatagridView
中的行我的代码的问题在于它是连续循环,在事件触发时添加相同的行。
这是我的代码:
public static void addJobstoDespatchGrid(DataGridView jobDataGridView)
{
using (DBEntities db = new DBEntities())
{
var data = (from a in db.Active where a.Status == "Hold"
select a);
foreach (var row in Data)
{
if (GridView.RowCount >= 0)
{
foreach (DataGridViewRow r in GridView.Rows)
{
Int32 id = Convert.ToInt32(r.Cells[0].Value);
if (row.jobID !=id)
{
DataGridViewRow rw = new DataGridViewRow();
rw.CreateCells(GridView);
rw.Cells[0].Value = row.jobID;
rw.Cells[1].Value = row.JobType;
rw.Cells[2].Value = row.dateTime;
rw.Cells[3].Value = row.jobStatus;
GridView.Rows.Add(rw);
}
}
}
else
{
DataGridViewRow rw = new DataGridViewRow();
rw.CreateCells(GridView);
rw.Cells[0].Value = row.jobID;
rw.Cells[1].Value = row.JobType;
rw.Cells[2].Value = row.dateTime;
rw.Cells[3].Value = row.jobStatus;
GridView.Rows.Add(rw);
}
}
}
}