【问题标题】:Adding label with image in datagridview在datagridview中添加带有图像的标签
【发布时间】:2022-09-27 09:34:03
【问题描述】:

我正在尝试在 c# 中使用 datagridview 制作事件查看器

This is my target

我已经阅读了许多源代码,但仍然对实现它感到困惑。

资源add custom DataGridViewColumn with label and button per cell How to add a Label to a DataGridView cell

非常感谢您的帮助

    标签: c# datagridview


    【解决方案1】:

    下面展示了如何从文件中读取图像,将它们放入 Dictionary 然后将它们加载到 DataTable 中的行中,当然对于真正的应用程序,有更多的图像和逻辑来分配取决于您的逻辑的图像。

    后端样机

    using System.Data;
    
    namespace DataGridViewImages.Classes
    {
        internal class Operations
        {
            public static Dictionary<int, byte[]> SmallImages()
            {
                Dictionary<int, byte[]> dictionary = new Dictionary<int, byte[]>
                {
                    { 1, File.ReadAllBytes("blueInformation_16.png") },
                    { 2, File.ReadAllBytes("radiobutton16.png") }
                };
    
                return dictionary;
            }
    
            public static DataTable Table()
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("image", typeof(byte[]));
                dt.Columns.Add("text", typeof(string));
    
                var images = SmallImages();
    
                dt.Rows.Add(images[1], "Some text");
                dt.Rows.Add(images[2], "More text");
    
                return dt;
            }
        }
    }
    

    表格代码

    using DataGridViewImages.Classes;
    
    namespace DataGridViewImages
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                Shown += OnShown;
                dataGridView1.SelectionChanged += DataGridView1OnSelectionChanged;
                dataGridView1.RowHeadersVisible = false;
            }
    
            private void DataGridView1OnSelectionChanged(object sender, EventArgs e)
            {
                if (dataGridView1.CurrentCell.ColumnIndex == 0)
                {
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex]
                        .Cells[0].Selected = false;
                }
            }
    
            private void OnShown(object sender, EventArgs e)
            {
                dataGridView1.DataSource = Operations.Table();
                dataGridView1.Columns[0].HeaderText = "";
                dataGridView1.Columns[0].Width = 25;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多