1

I have this code :

string strdata = Encoding.Unicode.GetString(buffer);
char[] splitchar = new Char[] { '\x00' };
string[] assetdata = strdata.Split(splitchar, strdata.Length);

Buffer is a text data which goes as one row and consists of 4 types of variables. You can see example of Encoded in Unicode buffer following this link : http://pastebin.com/ScdGX8it So there are 4 types of data here which needs to be filled into DataGridView rows separated by 4 columns , so it can me sorted and manipulated after. Assetdata is array with this data separated by each value as a single element , but i need to group them - that is the main problem. Thanks.

2
  • Can you show how you want the data organized then grouped? You say you want 4 columns of data, but your data dump shows more values than that in each section. Commented Jul 24, 2013 at 18:20
  • Actually it is 4 types of data separated by this \x00 character. , so now it looks like a1-b1-c1-d1-a2-b2-c2-d2 and etc where "-" is "\x00" character , and a b c d are types of data , and numbers represent just difference in it. And i want it to be grouped in DataGridView , in columns a b c d with data1 data2 data3 data4 inserted by rows accordingly. Hope that explains what i want. grab.by/oL4C - here you can see screenshot , in listbox you see rows with data separated. Now the 4-th type is selected. So i want that to insert to the DataGridView on the top accordingly. Commented Jul 24, 2013 at 18:30

2 Answers 2

1

Here's one way without LINQ, that uses a datatable as the datasource for the datagridview.

        DataTable dt = new DataTable("T1");
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("A"), new DataColumn("B"), new DataColumn("C"), new DataColumn("D")});
        for (int i = 0; i < assetdata.Length; i += 4)
        {
            dt.Rows.Add(new string[]{assetdata[i],assetdata[i+1],assetdata[i+2],assetdata[i+3]});
        }
        dataGridView1.DataSource = dt;

This way you can modify the datatable and update the datagridview, which will probably give you more options since this fits in more with how the datagridviewwas was designed.

Sign up to request clarification or add additional context in comments.

1 Comment

damn ! this works perfectly ! Thanks so much , i was not thinking about creating data table... Though you need to add -4 at assetdata.Length , or it will give index out of range error. One more thing - if i have columns already created , how can i force Datasource to use those columns for insertion ? Because it adds new columns now , even if i use the same name.
0

LINQ is your friend. Go through the tutorials on grouping. It should be pretty easy, since you already have a string[]. Once you're done with your query, call the ToIEnumerable() extension method to bind to a DataGridView.

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

2 Comments

Thing is that for now even c# is not my friend , as i am learning it for 2 months only... I am trying to avoid use of LINQ because i want to learn how it works as it is...
LINQ is an integral part of C#/.NET - using it will allow to learn about different pieces of the frameworks, such as: generics, lambda methods, anonymous types, collections/interfaces. If you wanted to do this without LINQ, you could manually create the strongly typed classes, and perform the grouping logic yourself, putting all items in a List<T> when you are done, to bind to a DataGridView.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.