[WinForm]dataGridView动态加载以本地图片显示列

增加一个图片列:

C#

        private void btnQuery_Click(object sender, EventArgs e)
        {
            StringBuilder sb=new StringBuilder ();
            sb.Append(" SELECT  序号, 品号,品名,客户品号,客户编码,客户名称,数量,币种,汇率,原币单价,原币总价,本币单价,本币总价,创建时间");
            sb.Append(" FROM  UF_Quotation");
            sb.Append(" where 1=1 ");
            if (txtItem.Text.Trim() != "") 
            {
                sb.Append(" and 品号 like '%" + txtItem.Text.Trim() + "%'");
            }
            if (txtItemName.Text.Trim() != "")
            {
                sb.Append(" and 品名 like '%" + txtItemName.Text.Trim() + "%'");
            }
        
            string dateStart= Convert.ToDateTime(dateTimePicker1.Text.Trim()).ToString("yyyy-MM-dd");
            string dateEnd = Convert.ToDateTime(dateTimePicker2.Text.Trim()).ToString("yyyy-MM-dd");
            sb.Append(" and convert(char(10),创建时间,120)>='"+dateStart+"'");
            sb.Append(" and convert(char(10),创建时间,120)<='" + dateEnd + "'");

            DataTable dt = new DataTable();
            dt= JB_QuotationDAL.QuotationList(sb.ToString());
            dt.Columns.Add("img", typeof(Image));
            foreach (DataRow row in dt.Rows)
            {
                if (File.Exists(@"D:\产品图片\" + row["品号"].ToString().Trim() + ".jpg"))
                {
                    //需要判断是否存在图片
                    Image img = Image.FromFile(@"D:\产品图片\" + row["品号"].ToString().Trim() + ".jpg");//双引号里是图片的路径
                    row["img"] = img;
                }
                else
                {
                    //需要判断是否存在图片
                    Image img = Image.FromFile(@"D:\产品图片\LOGO.jpg");//双引号里是图片的路径
                    row["img"] = img;
                }
                
            }
            dgvQuotation.DataSource = dt;
        }

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 WinForms 的 DataGridView动态加载显示图片,您可以按照以下步骤进行操作: 1. 在 DataGridView 中添加一个显示图片。可以使用 DataGridViewImageColumn 类型的显示图片。 2. 在代码中动态加载图片,并将其赋值给 DataGridView 中对应的单元格。 以下是一个示例,展示了如何动态加载显示图片: ```csharp // 创建一个 DataGridViewImageColumn 显示图片 DataGridViewImageColumn imageColumn = new DataGridViewImageColumn(); imageColumn.HeaderText = "图片"; imageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom; // 设置图片布局方式 // 将添加到 DataGridView 控件中 dataGridView1.Columns.Add(imageColumn); // 加载图片并设置到单元格中 string imagePath = "path_to_image.png"; // 图片的路径 Image image = Image.FromFile(imagePath); // 加载图片 int rowIndex = 0; // 要设置图片的行索引 // 创建一个 DataGridViewImageCell,并将图片赋值给它 DataGridViewImageCell imageCell = new DataGridViewImageCell(); imageCell.Value = image; // 将 DataGridViewImageCell 设置到指定的单元格中 dataGridView1.Rows[rowIndex].Cells["图片"].Value = imageCell; ``` 在上述示例中,我们首先创建了一个 DataGridViewImageColumn ,并设置其显示图片的布局方式为 Zoom。然后将该添加到 DataGridView 控件中。 接下来,我们加载显示图片,并将其赋值给 DataGridView 中的指定单元格。通过创建一个 DataGridViewImageCell 对象,并将图片赋值给它,然后将该 DataGridViewImageCell 设置到指定的单元格中。 请注意,上述示例中的 "图片" 是 DataGridView图片的名称,请根据实际情况修改。同时,也可以在代码中使用循环来动态加载多张图片到多个单元格中。 这样,您就可以在 WinForms 的 DataGridView动态加载显示图片了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门德仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值