Click here to Skip to main content
15,885,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to do the following:

1. Retrieve the product information in the Gridview.
2. Reading the data from Gridview into 2D array "integer type".
3. Convert 2D array to be Byte[], or using Buffer.BlockCopy Method in order to transmitted over the network.
4. Receive the Byte array.
5. Convert it to 2D array and fill the Gridview.

What I have tried:

<pre>
This is my code 

<pre>, var select = "SELECT Pro_ID, Price, Category_ID FROM Products where Empl_ID = 1";
                var c = new SqlConnection(); 
                var dataAdapter = new SqlDataAdapter(select, c);
    
                var commandBuilder = new SqlCommandBuilder(dataAdapter);
                var ds = new DataSet();
                dataAdapter.Fill(ds);
                dataGridView.DataSource = ds.Tables[0];
                dataGridView.Columns[0].HeaderText = "Items";
                dataGridView.Columns[1].HeaderText = "Price";
                dataGridView.Columns[2].HeaderText = "Quantity";
    
                // reading data from gridview into 2D array
                string[,] DataValue = new string[dataGridView.Rows.Count, dataGridView.Columns.Count];
    
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    foreach (DataGridViewColumn col in dataGridView.Columns)
                    {
                        DataValue[row.Index, col.Index] = dataGridView.Rows[row.Index].Cells[col.Index].Value.ToString();
                    }
    
                }



I was able to implement the first and second points, but the rest, I need some guidance to implement.
Posted
Updated 30-Mar-17 14:07pm
Comments
Member 13036251 30-Mar-17 20:51pm    
You need to ensure the encoding is the same on the send and receive. Have you thought of using a session variable of type String[]. You can make it multidimensional and could bind it to your grid.

1 solution

Hi Member 12669478,

Basically what you are trying to do is send the grid values (2D string) over the network as byte[] and reconstruct it to the same 2D array. You are treating all the values as string.

This might be a bit tricky as the receiver end needs to know the dimensions of the array, and a mark for individual elements in the byte array. I suggest you can use pipe (|) as a delimiter (I am assuming there is no pipe in the texts; having pipe in texts is highly unlikely).

After you populate the DataValue you can add these lines:
C#
const int PIPEDELIMETER = 124;
List<byte> ListOfBytes = new List<byte>();

// Adding metadata - dimension of the first index. Plus the delimiter.
ListOfBytes.Add(Convert.ToByte(DataValue.GetUpperBound(0) + 1));
ListOfBytes.Add(PIPEDELIMETER);

// Adding metadata - dimension of the second index. Plus the delimiter.
ListOfBytes.Add(Convert.ToByte(DataValue.GetUpperBound(1) + 1));
ListOfBytes.Add(PIPEDELIMETER);

for (int i = 0; i <= DataValue.GetUpperBound(0); i++)
{
    for (int j = 0; j <= DataValue.GetUpperBound(1); j++)
    {
        // Add the string as bytes in ListOfBytes.
        ListOfBytes.AddRange(Encoding.ASCII.GetBytes(DataValue[i, j]));

        // Add the delimiter
        ListOfBytes.Add(PIPEDELIMETER);
    }
}

byte[] Bytes = ListOfBytes.ToArray();   // Convert 2D string array to byte array.
Say DataValue has dimensions [3, 2], and the first two elements are:
DataValue[0, 0] = "Issac";
DataValue[0, 1] = "Newton";
Then the Bytes array has the following signature:
[0]: 3
[1]: 124
[2]: 2
[3]: 124
[4]: 73
[5]: 115
[6]: 115
[7]: 97
[8]: 99
[9]: 124
[10]: 78
[11]: 101
[12]: 119
[13]: 116
[14]: 111
[15]: 110
[16]: 124
.............
.............
Code as follows at the receiver end:
/*Reconstruction*/
int FirstDimensionCount = Bytes[0]; // First dimension from metadata.
int SecondDimensionCount = Bytes[2]; // Second dimension from metadata.

// String array to hold the reconstructed data.
string[,] DataValueConvertedBack = new string[FirstDimensionCount, SecondDimensionCount];
int BytesCount = 4; // Data starts from the 4-th index.

// Get the entire bytes array into character array.
char[] CharsFromBytes = Encoding.UTF8.GetString(Bytes).ToCharArray();

StringBuilder SBuilder = new StringBuilder();
for (int i = 0; i < FirstDimensionCount; i++)
{
    for (int j = 0; j < SecondDimensionCount; j++)
    {
        while (CharsFromBytes[BytesCount] != '|')
            SBuilder.Append(CharsFromBytes[BytesCount++]); // Build the string character by character.
        DataValueConvertedBack[i, j] = SBuilder.ToString(); // Add the string to the specified string array index.
        BytesCount++;   // Increase the bytes pointer as it is currently pointing to the pipe
        SBuilder.Clear();   // Clear out for the next operation.
    }
}

// DataValueConvertedBack (receiver end) = DataValue (sending end) at this point.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900