简体   繁体   English

C#LoadData,将列转换为dataGridView中的图像

[英]C# LoadData , Convert column to image in dataGridView

I need when load dataGridView status[status] column convert to image and show image , The column status = 1 or 0 and i need if 1 show ok.png and if 0 show no.png 我需要在加载dataGridView status [status]列转换为image并显示image时,列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();
        }

anyone can help me ? 有人可以帮助我吗?

Thanks 谢谢

Use CellFormatting event of DataGridView as follow: 使用DataGridView CellFormatting事件,如下所示:

    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

            }
        }
    }

Edit: Instead of Auto-Generating columns, create columns in DataGridView as shown in image: 编辑:如图所示,而不是自动生成列,而是在DataGridView创建列:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM