[英]C# Datagridview Image Column only displaying one image
我已经在c#winform中的数据网格中添加了一个“图像列”,并且尝试根据数据库值是否为“ 1”来显示图像。 但是我得到的是else语句设置的所有行中的相同图像,这是列信息
dgvPatList.Columns[8].Name = "NPO";
dgvPatList.Columns[8].HeaderText = "NPO";
dgvPatList.Columns[8].DataPropertyName = "NPO"
dgvPatList.Columns[8].Width = 50;
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.HeaderText = "NPO";
imageColumn.Name = "NPOIMG";
dgvPatList.Columns.Add(imageColumn);
这是添加图像的代码
private void dgvPatList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
int number_of_rows = dgvPatList.RowCount;
for (int i = 0; i < number_of_rows; i++)
{
if (dgvPatList.Rows[i].Cells[0].Value.ToString() == "1")
{
Icon image = Properties.Resources.Tick_Green;
this.dgvPatList.Rows[i].Cells["NPOIMG"].Value = image;
}
else
{
Icon image = Properties.Resources.no_results;
this.dgvPatList.Rows[i].Cells["NPOIMG"].Value = image;
//((DataGridViewImageCell)this.dgvPatList.Rows[i].Cells["NPOIMG"]).Value = Properties.Resources.no_results;
}
}
}
也许您的标准永远是正确的或总是错误的。
但是我使用正确的标准检查了这种方式,并且可以正常工作:
foreach (DataGridViewRow row in myDataGridView.Rows)
{
if (row.IsNewRow)
continue;
if (row.Cells[0].Value.ToString() == "1")
row.Cells["ImageColumn"].Value = Properties.Resources.Image1;
else
row.Cells["ImageColumn"].Value = Properties.Resources.Image2;
}
假设您的条件1实际被击中,这应该可以工作。它还将处理空值(如果有)。
foreach (DataGridViewRow dgRow in dgvPatList.Rows)
{
if (dgRow.Cells[0].Value == null) continue; //Change if you wish no_results to be shown
dgRow.Cells["NPOIMG"].Value = dgRow.Cells[0].Value.ToString() == "1"
? Properties.Resources.Tick_Green
: Properties.Resources.no_results;
}
示例如下所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.