Reputation: 1589
I have a datatable with this format
ID X-coordinate Y-coordinate
And I want to create a grid that has at the nodes the IDs and has lines connecting to adjacent IDs based off their X,Y location. Any suggestions on doing this, I have never done any drawing before. Thanks
Upvotes: 1
Views: 258
Reputation: 81655
Place a panel on a form:
private DataTable dt;
public Form1() {
InitializeComponent();
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("XCord", typeof(int));
dt.Columns.Add("YCord", typeof(int));
DataRow dr1 = dt.NewRow();
dr1["ID"] = 1;
dr1["XCord"] = 10;
dr1["YCord"] = 10;
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["ID"] = 2;
dr2["XCord"] = 10;
dr2["YCord"] = 50;
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["ID"] = 3;
dr3["XCord"] = 25;
dr3["YCord"] = 50;
dt.Rows.Add(dr3);
panel1.Paint += panel1_Paint;
}
Here is the paint method:
private void panel1_Paint(object sender, PaintEventArgs e) {
e.Graphics.Clear(Color.White);
for (int i = 1; i < dt.Rows.Count; i++) {
e.Graphics.DrawLine(Pens.Red,
new Point(Convert.ToInt32(dt.Rows[i - 1]["XCord"]), Convert.ToInt32(dt.Rows[i - 1]["YCord"])),
new Point(Convert.ToInt32(dt.Rows[i]["XCord"]), Convert.ToInt32(dt.Rows[i]["YCord"])));
}
}
and the results:
Upvotes: 2