S
S
Sechih2021-10-25 21:32:09
C++ / C#
Sechih, 2021-10-25 21:32:09

How to display number in hexadecimal format in DataGridView?

Good afternoon, I'm just starting to write in C #, I wrote an application that communicates with STM32 via USB-CDC, in fact CAN - Sniffer, reads a line, converts it to hex, sorts it by Id and saves it to a class (I immediately wanted to save it to a structure, but not came out due to access and initialization of the fields of the structure in the stream, they were reset to zero)

class CAN_RX_Class
        {   static public uint[] Id = new uint[max_message];
            static public byte[] DLC = new byte[max_message];
            static public byte[] Byte0 = new byte[max_message];
            static public byte[] Byte1 = new byte[max_message];
            static public byte[] Byte2 = new byte[max_message];
            static public byte[] Byte3 = new byte[max_message];
            static public byte[] Byte4 = new byte[max_message];
            static public byte[] Byte5 = new byte[max_message];
            static public byte[] Byte6 = new byte[max_message];
            static public byte[] Byte7 = new byte[max_message];
            static public ushort[] CAN_count = new ushort[max_message];
            static public ushort number_last_string;
            static public ushort current_line_number = 0;
        }

I read the buffer and put it on the shelves of the number class
uint ID_array = 0;
            for (byte j = 5, offset = 28; j <= 12; j++, offset -= 4)
                ID_array += (uint)((_bufer[j].MyAsciiToHex()) << offset);
              
  if (CAN_RX_Class.number_last_string == 0)
                {
                    //формируем ID
                    CAN_RX_Class.Id[i] = ID_array;
                    //Формируем DLC
                    CAN_RX_Class.DLC[i] = _bufer[13].MyAsciiToHex();
                    //Формируем BYTS
                    CAN_RX_Class.Byte0[0] = (byte)(((_bufer[14].MyAsciiToHex()) << 4) + (_bufer[15].MyAsciiToHex()));

In the debug, you can see how all the numbers fit correctly into the class and form a complete message, now I want to display all the fields of the class arrays (numbers) in the DataGridView. I add the
required number of rows and I want to update the data in them every time in real time, I think now how to do it optimally, brought out on crutches.
It is inconveniently done that you need to add a line every time, but I think it will work out like this, if there is a new Id in the class, we create a new line for it, no, we update the already created one.
//добавляем пустые строки >>
            for (; CAN_RX_Class.current_line_number <= CAN_RX_Class.number_last_string; ++CAN_RX_Class.current_line_number)
                Invoke(new Action(() =>
                {
                    dataGrid_CAN1.Rows[dataGrid_CAN1.Rows.Add()].Cells[0].Value = CAN_RX_Class.current_line_number + 1;
                }));
            //<<

When outputting, the number in the field is displayed in decimal format, how to make it output in hexadecimal form, do you need to write a method or is there some kind of ready-made implementation?
Of course, if I sorted by Id in an array of type string and not ushort and compared Id strings, and not "real numbers", then it would be no problem to display FFs in the GridView.
You need to output in this format, but it outputs in decimal
6176f634ef09f234898326.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sechih, 2021-10-26
@Sechih

This is how I solved the problem with the extension

public static string MyConvertToHEX(this int mean) => (Convert.ToString(mean, 16));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question