Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a scenario in which I need to create a table that has only 1 row. I am not supposed to use database for this. I have a long string of bytes which i should substring and put to each column of this row.This table should be resizable in terms of length.
That is its columns should increase or decrease based on my input. Has anyone got any idea.

What I have tried:

I preferred using datagrid view without database. Both my data column and its title should vary based on my input length.
C#
DataTable dt1 = new DataTable();
//i add columns likethis
dt1.Columns.AddRange(new DataColumn[4]{new DataColumn("byte1",typeof(string)),new DataColumn("byte2",typeof(string)),new DataColumn("byte3",typeof(string)),new DataColumn("byte4",typeof(string))});

int j=0;
byte0=hexstring.Substring(j,2);
.
.
.
//after i create columns i add data to rows like the following
dt1.Rows.Add(byte0,byte1,byte2,byte3);


These values inside Add keep on changing based on my input say i have 10 bytes, it inreases to byte9. How can I make i generic. Is there any solution ither than datagrid view? Any help appreciated.
Posted
Updated 18-Jun-18 22:34pm
v2

1 solution

I'm not sure i understand you well, but...

Take a look at below method:
C#
public string[] ToHexString(string input)
{
	return input.Select(c=>((int)c).ToString("X2")).ToArray();
}

It converts string into array of "hex-representation" of each char in string. Now, you can simply create DataTable. See:
C#
string s = "not very long string";
DataTable dt = new DataTable();
string[] hexstring = ToHexString(s);
for(int i=0; i<hexstring.Length; i++)
{
	dt.Columns.Add(new DataColumn(string.Format("byte{0}", i), typeof(string)));
}
dt.Rows.Add(hexstring);

//bind data to DataGridView
dataGridView1.Datasource = dt;


Good luck!
 
Share this answer
 
Comments
Member 13688117 19-Jun-18 6:50am    
Thank you so much its working!!!
Maciej Los 19-Jun-18 7:00am    
Glad i could help!
Maciej Los 19-Jun-18 7:47am    
If it works, why are you unmarked my answer as a solution?

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