我有以下 code1 ,它可以按预期完成从DataTable中绘制甘特图。但是,当我为我的DataGridView点击事件 code2 使用相同的foreach循环时,它无法正常工作 img2 - 我希望甘特图对应于DataGridView表,按照 IMG1 即可。请不要在 code2 中更改为foreach循环,以便它使用DataGridView语法。
任何想法?
备受赞赏
代码1
// Draw Gantt Chart
foreach (DataRow rowCapPlan in dtCapPlan.Rows)
{
id = rowCapPlan["Program ID"].ToString();
foreach (DataRow rowMilestone in dtMilestones.Rows)
{
if (id == rowMilestone["Program ID"].ToString())
{
// calculate space in days from todays date and the milestone date
msDate = Convert.ToDateTime(rowMilestone["Milestone Date"]);
msTimespan = msDate - calDate;
msIntDate = (int)msTimespan.TotalDays + 1;
tTimespan = tDate - calDate;
tIntDate = (int)tTimespan.TotalDays + 1;
ganttPlotSpace = msIntDate - tIntDate;
// Draw each milestone or gateway which is not yet complete
if (rowMilestone["% Complete"].ToString() != "100")
{
taskname = rowMilestone["Task Name"].ToString();
if (taskname == "Gateway 1" || taskname == "Gateway 2" || taskname == "Gateway 3" || taskname == "Gateway 4" || taskname == "Gateway 5")
{
Rectangle gw = new Rectangle(startx + ganttPlotSpace, starty - 4, 2, 11);
ganttGraphics.DrawRectangle(gwPen, gw);
ganttGraphics.FillRectangle(gwBrush, gw);
}
else
{
Rectangle ms = new Rectangle(startx + ganttPlotSpace + 1, starty, 2, 2);
ganttGraphics.DrawRectangle(msPen, ms);
ganttGraphics.FillRectangle(msBrush, ms);
}
ganttGraphics.DrawLine(linePen, startx - 10, starty - 11, pnlGantt.Right, starty - 11);
}
}
}
starty = starty + 22;
}
ganttGraphics.DrawLine(linePen, startx - 10, starty + 11, pnlGantt.Right, starty + 11);
IMG1
码2
private void dataCapPlan_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
int startx = 10;
int starty = 78;
pnlGantt.Controls.Clear();
Graphics ganttGraphics = pnlGantt.CreateGraphics();
// Draw Gantt Chart
foreach (DataGridViewRow rowCapPlan in dataCapPlan.Rows)
{
id = rowCapPlan.Cells[0].ToString();
foreach (DataGridViewRow rowMilestone in dataMilestones.Rows)
{
if (id == rowMilestone.Cells[0].ToString())
{
// calculate space in days from todays date and the milestone date
msDate = Convert.ToDateTime(rowMilestone.Cells[2].Value);
msTimespan = msDate - calDate;
msIntDate = (int)msTimespan.TotalDays + 1;
tTimespan = tDate - calDate;
tIntDate = (int)tTimespan.TotalDays + 1;
ganttPlotSpace = msIntDate - tIntDate;
// Draw each milestone or gateway which is not yet complete
if (rowMilestone.Cells[3].ToString() != "100")
{
taskname = rowMilestone.Cells[1].ToString();
if (taskname == "Gateway 1" || taskname == "Gateway 2" || taskname == "Gateway 3" || taskname == "Gateway 4" || taskname == "Gateway 5")
{
Rectangle gw = new Rectangle(startx + ganttPlotSpace, starty - 4, 2, 11);
ganttGraphics.DrawRectangle(gwPen, gw);
ganttGraphics.FillRectangle(gwBrush, gw);
}
else
{
Rectangle ms = new Rectangle(startx + ganttPlotSpace + 1, starty, 2, 2);
ganttGraphics.DrawRectangle(msPen, ms);
ganttGraphics.FillRectangle(msBrush, ms);
}
ganttGraphics.DrawLine(linePen, startx - 10, starty - 11, pnlGantt.Right, starty - 11);
}
}
}
starty = starty + 22;
}
ganttGraphics.DrawLine(linePen, startx - 10, starty + 11, pnlGantt.Right, starty + 11);
}
IMG2