Question

I have an array of bytes in C#:

 byte[] bmp = File.ReadAllBytes("D:\\x.bmp");

And I want to represent it in DataGridView as follows (three columns - first column offset, second column sizebyte, third column description, and rows are elements from byte array from x.bmp):

0          2               signature
2          4                size BMP
6          2                reserved
8          2                reserved
10         4                offset start image
14         4                must 40
18         4                 width
22         4                 height
26         2                 must 1

Can i represent byte array in this way using DataGridView in C# ?

Was it helpful?

Solution

How about this code ?

You don't need to read all bytes of BMP file.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        UpdateDataGridView();
    }

    private void UpdateDataGridView()
    {
        byte[] bmp = new byte[28];
        List<BMPInfo> InfoList = new List<BMPInfo>();

        using (var fs = new FileStream("D:\\x.bmp", FileMode.Open, FileAccess.Read))
        {
            fs.Read(bmp, 0, bmp.Length);
        }

        InfoList.Add(new BMPInfo
        {
            Offset = BitConverter.ToInt32(bmp, 10),
            Size = BitConverter.ToInt32(bmp, 2),
            Description = "Something"
        });
        dataGridView1.DataSource = InfoList;
    }
}

public class BMPInfo
{
    public long Offset { get; set; }
    public long Size { get; set; }
    public string Description { get; set;}
}

OTHER TIPS

I am not a winform guy so I dont know how to work with DataGridView. But I somehow able to do this:

This is just to give you an idea.

private void Form1_Load(object sender, EventArgs e)
        {
            byte[] offset = { 0, 2, 6, 8, 10, 14, 18, 22, 26 };
            byte[] sizebyte = { 4, 2, 2, 4, 4, 4, 4, 2 };
            string[] description = { "signature", "size BMP", "reserved", "reserved", "offset start image", "must 40", "width", "height" };

            this.dataGridView1.Columns.Add("offset", "offset");
            this.dataGridView1.Columns.Add("sizebyte", "sizebyte");
            this.dataGridView1.Columns.Add("description", "description");
            int i = offset.Length;


                for (int j = 0; j < i; j++)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(dataGridView1);
                    row.Cells[0].Value = offset.GetValue(j).ToString();
                    if(j<sizebyte.Length)
                    row.Cells[1].Value = sizebyte.GetValue(j).ToString();
                    if (j < description.Count())
                    row.Cells[2].Value = description.GetValue(j).ToString();
                    dataGridView1.Rows.Add(row);
                }



        }

I know code is not perfect.But I am able to fill my dataGridView with this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top