Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I built an application that would scrape data from a web source, process that data to a jagged array and then finally display the data in individual labels.

The data is grouped by fields of interest. Say:

group A consists of a string[3][3]. I gave the labels names in a way so that I could iterate through the array and then do a (Label)Controls.Find("lb_DataA_" + index1 + "_" + index2) and so on.

I feel like this is a really inefficient and troublesome way of displaying the data - especially since I also have textChangedEvents on the labels to have the background colour interpolate fade.I have about 50 labels to manage that way.

I am not very familiar with the built in functionality of Visual Studio. Is there a way to bind a jagged array to a data grid (using it as a datasource somehow?) and have the same OnTextChangedEvent for all "Cells" of that data grid ?

Thanks for your help
Cheers

Daniel
Posted
Comments
Sergey Alexandrovich Kryukov 2-Sep-14 14:12pm    
You see, the problem is: you are trying to arrange data on screen regardless of the intent and its semantic used in the design of the site being scrapped. So, some ugliness is unavoidable. All your activity looks strange to me. Why doing all that? Why jagged array? Why do you think the jagged array will always be of rank 2?

From the other hand, there is no any technical problems of showing in DataGridView whatever you want. It all depends on how you want to arrange your data...

—SA
KergalBerlin 2-Sep-14 15:10pm    
I read the data as a string, break it down to a jagged array - which actually isn't really jagged at all, I could display all the data in a rectangular array as well - however, I am using linq to extract,sort and filter said array and to my best knowledge only jagged arrays work with linq.

The jagged array has exactly the form that I would like to see on screen and will always be 3x3. I don't need any headers just those 9 - 3 x 3 - cells.
I tried the obvious datagridview1.DataSource = myArray; - I get data in the gridview, but only metadata of the array not the actual data content.

Besides that I have several of these 3x3 arrays - they are being created by instances of my "scraping" class - they will update indepentently from each other and they are displayed at different places in my windows form.
Is there an inbuild way of mapping/binding the array content to the datagridview ? Or is there even a control object better suited for my case ?

I think you're approaching this from the wrong direction.

First, get your underlying data structure working the way you want it. Use INotifyPropertyChanged, check if a value is changed in the setter of a property.
Then, use the events provided to you by the BindingSource to respond to changes (ListChanged, ItemChanged etc.)
Based on those events, update your grid.

Or, just rebind the data to the grid like you described, and don't use CellValueChanged but something like CellFormatting events to apply the conditional formatting.
 
Share this answer
 
Comments
KergalBerlin 2-Sep-14 17:50pm    
I might give that a shot - I have not worked with INotifyPropertyChanged yet.
Thanks.
DataSource will make every property of the array a column . I took the existing string[][] and performend the following linq on it:

SQL
dataGridView1.DataSource = (from arr in test 
select new 
{ 
   Data = arr[0], Dog = arr[1] +
}
).ToArray();


The result was a datagridview with 2 columns (Data and Dog) with the respective values originating from the string[][].

Regarding my second question - I will open a new thread for that.
 
Share this answer
 
You can always feel in an instance of DataGridView with data in any form you want, cell by cell. If you need to fill in the jagged array, by concern would be: jagged array does not assume that there is a column-to-column one-to-one correspondence of the element of the inner array, so it's not clear what's the point of arranging the data in a grid view, where this correspondence is visually pronounced: the user reasonably tends to think that the column represents identical attributes of different data element arranges horizontally. Besides, the jagged nature of the array assumes that you will have any number of empty cells anyway.

If you still want to deal with your data as is and thing that my concerns are not important to you (well, I still don't know your goals), and you don't want to populate the control with data on cell-to-cell basis, one of the possible ideas is to fill in the arrays with null values to have an NxM matrix of data. Then you could simply bind your data as the data source with the control.

But then, a jagged array is not suitable structure anyway, mostly because the arrays don't allow adding elements. You can bind the data like list of lists. Even though you can populate list of list using your array as a data source, it's unlikely the best solution. It would be much better if you scribe the Web sites into an list of lists in first place. I hope it's easy for you.

—SA
 
Share this answer
 
Comments
KergalBerlin 2-Sep-14 15:44pm    
Hi Sergey,
I appreciate your fast responses, but apparently I am not expressing myself well enough.
The jagged arrays will always have the same form no empty values at all no changes in dimensions, length or anything alike. The entire array can be seen as rock solid. I am not taking the data as is from a website and I believe I got them in the most efficient and pragmatic way possible ( I do not leverage the power of jagged arrays in terms of "jagged endings" - the only reason I use them is to use Linq to Objects on them ).

I managed to get the first part of my problem done by doing the following:

dataGridView1.DataSource = (from arr in test select new { Data = arr[0], Dog = arr[1] }).ToArray();

(the above is of course only dummy data for testing purposes).
Using this approach I am always updating the entire DataSource - and cannot use the inbuilt CellValueChanged event (which would be awesome, because the EventArgs contain both row and column index which I need for the highlighting I want to do).

Do you have any ideas for that ?
Sergey Alexandrovich Kryukov 2-Sep-14 15:48pm    
I don't understand. Do you mean that the data structure is always NxM, all cells are filled in? Then why the array is technically jagged? Could be the array of the type like SomeType[,]...
In all cases, if you provide list of lists, you can use this structure as the data source, which was your question.
—SA
KergalBerlin 2-Sep-14 16:21pm    
the data is not only always NxM it will always be 3x3 . As mentioned the only reason the array is of type string[][] and not string[,] is the LINQ compatability.

Thanks for the list of lists idea - got that part down already as mentioned in the previous post.

Can you help me with one more thing?
Every 15 minutes I expect to receive new data (again I will transform that data to 3x3) and simply update my DataSource . I would like to track changes in the cells of the datagridview though.

Example :
old gridview Foo0, Moo0, Baa0
Foo1, Moo1, Baa1
Foo2, Moo2, Baa2

newgridview Foo0, Moo0, Baa0
Foo1, CHANGE, Baa1
Foo2, Moo2, Baa2

--> now I want to highlight and fade the background color of the cell with the changed text. Without the datasource I could simply use the CellValueChanged event - that does not work with the datasource though.

Any idea how I can acquire the row and column index of the changed cell ?

Sergey Alexandrovich Kryukov 2-Sep-14 17:11pm    
What are you talking about? "3x3" is the special case of "NxM", not visa versa.
—SA
KergalBerlin 2-Sep-14 17:31pm    
Please don't take this the wrong way, but what is the purpose of your responses?

The phrase "not only always NxM it will always be 3x3" cleary implies that 3x3 is the less general case. With the sentence before I am clearly going one step further . I do not only say " it is a rectangular array" i explicitly state that is not only a rectangular one,but even a quadratic one typed to a jagged array - AND AGAIN -to my best knowledge- LINQ TO OBJECT cannot (or at least not easily) be used with rectangular arrays of type string[,]. THIS is a -> jagged array string[][] and in my case it consists of 3 arrays with 3 fields.

Please do not reply unless you have something valuable to add.
In case I missunderstand your responses and you are not trolling, please accept my apoligies.

-DK

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