我需要在加载dataGridView状态[status]列时将其转换为图像和显示图像, 列状态= 1或0,我需要1如果显示ok.png,如果0显示no.png
private void LoadData()
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "select id[code] ,status[status], url[url] from db";
DB = new SQLiteDataAdapter(CommandText, sql_con);
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0];
dataGridView.DataSource = DT;
dataGridView.Columns["code"].Visible = false;
sql_con.Close();
}
任何人都可以帮助我吗?
由于
答案 0 :(得分:2)
使用CellFormatting
的{{1}}事件如下:
DataGridView
修改强>
而不是自动生成列,在 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0)
{
var dt = dataGridView1.DataSource as DataTable;
if (dt != null && dt.Rows.Count > e.RowIndex && dt.Columns.Count > e.ColumnIndex)
{
var value = Convert.ToInt32(dt.Rows[e.RowIndex][e.ColumnIndex]);
if (value == 0)
e.Value = Image.FromFile(@"C:\No.png"); // Here you can provide your own path of No.png image
if (value == 1)
e.Value = Image.FromFile(@"C:\OK.png"); // Here you can provide your own path of Ok.png image
}
}
}
中创建列,如图所示: