可能重复:
带有图像的datagridview动态
他喜欢帮我动态创建一个datagridview。C# Windows 窗体
目标是 ping IP 或主机名的成功列表,以及是否插入网格(主机、日期和图像红色或绿色),具体取决于响应。
我有这个写网格的函数,但是如果一个 ping 不成功,他把所有其他的都放在Redball
.
private void TabelaDinamimca(bool sucesso,string host, string data)
{
GridView1.ColumnCount = 3;
GridView1.Columns[1].Name = "host";
GridView1.Columns[2].Name = "data";
DataGridViewImageColumn img = new DataGridViewImageColumn();
string path = (sucesso == true
? @"C:\TestFolder\greenBall.png"
: @"C:\TestFolder\redBall.png");
Image image = Image.FromFile(path);
img.Image = image;
GridView1.Columns.Add(img);
string[] row = new string[] { host, data };
GridView1.Rows.Add(row);
GridView1.Show();
}
注意:在调试模式下,bool sucesso caseErro
为 false
解决方案
private void TabelaDinamimcaSucess(bool sucesso, int index, string host, string data, string tempo,string status)
{
string[] row = new string[] { index.ToString(), host, data, tempo,status };
dataGridView1.Rows.Add(row);
int number_of_rows = dataGridView1.RowCount -1;
Bitmap b = new Bitmap((sucesso == true ? Properties.Resources.greenBall : Properties.Resources.redBall));
Icon icon = Icon.FromHandle(b.GetHicon());
dataGridView1.Rows[number_of_rows].Cells["img"].Value = icon;
dataGridView1.Show();
}