![](/img/trans.png)
[英]how to retrive data from the database based on a value of the cell of the datagridview
[英]How to dynamically assign Datagridview cell values based on data from my database?
我在Winform项目上有一个Datagridview。 Datagridview由我数据库中的数据填充。
我的数据库表上有3列: id
, name
, status
。
我想要做的是,基于从数据库(“联机”或“脱机”)获得的状态字符串,我想在单元格中放置一个图像,而不仅仅是显示联机或脱机的字符串。
例如:如果状态为“在线”,我想在单元格上显示我指定的在线图像/图标。
任何人都有想法如何解决这个问题?
我将在DataBindingComplete事件上执行此操作:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (r.Cells["status"].Value.ToString() == "Online")
{
//add image here
}
}
}
您的数据网格也需要附加到DataBindingComplete
这个答案可能也很有用:
您可以使用任何datagridview事件,例如
private void dataGridView1_RowsAdded(object sender,
DataGridViewRowsAddedEventArgs e)
{
// write code to add image path in current cell of the row.
}
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// write code to add image path in current cell of the row.
}
下面的链接可能会让您在列中添加图片
http://csharp.net-informations.com/datagridview/csharp-datagridview-image.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.