我正在尝试实用地向 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);
}
}
}
}