Confession: Horrible code I just wrote



  • I was having problems getting a Silverlight DataGrid to display some data from a web service. The data is a collection of collections. It will always be two-dimensional but property names and number of properties can vary depending on the results. Here's a helper class I wrote to support databinding on this data.

            private sealed class HugeTuple
            {
                public string Item1 { get; set; }
                public string Item2 { get; set; }
                public string Item3 { get; set; }
                public string Item4 { get; set; }
                public string Item5 { get; set; }
                public string Item6 { get; set; }
                public string Item7 { get; set; }
                public string Item8 { get; set; }
                public string Item9 { get; set; }
                public string Item10 { get; set; }
                public string Item11 { get; set; }
                public string Item12 { get; set; }
                public string Item13 { get; set; }
                public string Item14 { get; set; }
                public string Item15 { get; set; }
                public string Item16 { get; set; }
                public string Item17 { get; set; }
                public string Item18 { get; set; }
                public string Item19 { get; set; }
                public string Item20 { get; set; }
                public string Item21 { get; set; }
                public string Item22 { get; set; }
                public string Item23 { get; set; }
                public string Item24 { get; set; }
                public string Item25 { get; set; }
                public string Item26 { get; set; }
                public string Item27 { get; set; }
                public string Item28 { get; set; }
                public string Item29 { get; set; }
                public string Item30 { get; set; }
                public string Item31 { get; set; }
                public string Item32 { get; set; }
                public string Item33 { get; set; }
                public string Item34 { get; set; }
                public string Item35 { get; set; }
                public string Item36 { get; set; }
                public string Item37 { get; set; }
                public string Item38 { get; set; }
                public string Item39 { get; set; }
                public string Item40 { get; set; }
                public string Item41 { get; set; }
                public string Item42 { get; set; }
                public string Item43 { get; set; }
                public string Item44 { get; set; }
                public string Item45 { get; set; }
                public string Item46 { get; set; }
                public string Item47 { get; set; }
                public string Item48 { get; set; }
                public string Item49 { get; set; }
                public string Item50 { get; set; }
                public string Item51 { get; set; }
                public string Item52 { get; set; }
                public string Item53 { get; set; }
                public string Item54 { get; set; }
                public string Item55 { get; set; }
                public string Item56 { get; set; }
                public string Item57 { get; set; }
                public string Item58 { get; set; }
                public string Item59 { get; set; }
                public string Item60 { get; set; }
                public string Item61 { get; set; }
                public string Item62 { get; set; }
                public string Item63 { get; set; }
                public string Item64 { get; set; }
                public string Item65 { get; set; }
                public string Item66 { get; set; }
                public string Item67 { get; set; }
                public string Item68 { get; set; }
                public string Item69 { get; set; }
                public string Item70 { get; set; }
                public string Item71 { get; set; }
                public string Item72 { get; set; }
                public string Item73 { get; set; }
                public string Item74 { get; set; }
                public string Item75 { get; set; }
                public string Item76 { get; set; }
                public string Item77 { get; set; }
                public string Item78 { get; set; }
                public string Item79 { get; set; }
                public string Item80 { get; set; }
                public string Item81 { get; set; }
                public string Item82 { get; set; }
                public string Item83 { get; set; }
                public string Item84 { get; set; }
                public string Item85 { get; set; }
                public string Item86 { get; set; }
                public string Item87 { get; set; }
                public string Item88 { get; set; }
                public string Item89 { get; set; }
                public string Item90 { get; set; }
                public string Item91 { get; set; }
                public string Item92 { get; set; }
                public string Item93 { get; set; }
                public string Item94 { get; set; }
                public string Item95 { get; set; }
                public string Item96 { get; set; }
                public string Item97 { get; set; }
                public string Item98 { get; set; }
                public string Item99 { get; set; }
                public string Item100 { get; set; }
                public string Item101 { get; set; }
                public string Item102 { get; set; }
                public string Item103 { get; set; }
                public string Item104 { get; set; }
                public string Item105 { get; set; }
                public string Item106 { get; set; }
                public string Item107 { get; set; }
                public string Item108 { get; set; }
                public string Item109 { get; set; }
                public string Item110 { get; set; }
                public string Item111 { get; set; }
                public string Item112 { get; set; }
                public string Item113 { get; set; }
                public string Item114 { get; set; }
                public string Item115 { get; set; }
                public string Item116 { get; set; }
                public string Item117 { get; set; }
                public string Item118 { get; set; }
                public string Item119 { get; set; }
                public string Item120 { get; set; }
                public string Item121 { get; set; }
                public string Item122 { get; set; }
                public string Item123 { get; set; }
                public string Item124 { get; set; }
                public string Item125 { get; set; }
                public string Item126 { get; set; }
                public string Item127 { get; set; }
                public string Item128 { get; set; }
                public string Item129 { get; set; }
                public string Item130 { get; set; }
                public string Item131 { get; set; }
                public string Item132 { get; set; }
                public string Item133 { get; set; }
                public string Item134 { get; set; }
                public string Item135 { get; set; }
                public string Item136 { get; set; }
                public string Item137 { get; set; }
                public string Item138 { get; set; }
                public string Item139 { get; set; }
                public string Item140 { get; set; }
                public string Item141 { get; set; }
                public string Item142 { get; set; }
                public string Item143 { get; set; }
                public string Item144 { get; set; }
                public string Item145 { get; set; }
                public string Item146 { get; set; }
                public string Item147 { get; set; }
                public string Item148 { get; set; }
                public string Item149 { get; set; }
                public string Item150 { get; set; }
                public string Item151 { get; set; }
                public string Item152 { get; set; }
                public string Item153 { get; set; }
                public string Item154 { get; set; }
                public string Item155 { get; set; }
                public string Item156 { get; set; }
                public string Item157 { get; set; }
                public string Item158 { get; set; }
                public string Item159 { get; set; }
                public string Item160 { get; set; }
                public string Item161 { get; set; }
                public string Item162 { get; set; }
                public string Item163 { get; set; }
                public string Item164 { get; set; }
                public string Item165 { get; set; }
                public string Item166 { get; set; }
                public string Item167 { get; set; }
                public string Item168 { get; set; }
                public string Item169 { get; set; }
                public string Item170 { get; set; }
                public string Item171 { get; set; }
                public string Item172 { get; set; }
                public string Item173 { get; set; }
                public string Item174 { get; set; }
                public string Item175 { get; set; }
                public string Item176 { get; set; }
                public string Item177 { get; set; }
                public string Item178 { get; set; }
                public string Item179 { get; set; }
                public string Item180 { get; set; }
                public string Item181 { get; set; }
                public string Item182 { get; set; }
                public string Item183 { get; set; }
                public string Item184 { get; set; }
                public string Item185 { get; set; }
                public string Item186 { get; set; }
                public string Item187 { get; set; }
                public string Item188 { get; set; }
                public string Item189 { get; set; }
                public string Item190 { get; set; }
                public string Item191 { get; set; }
                public string Item192 { get; set; }
                public string Item193 { get; set; }
                public string Item194 { get; set; }
                public string Item195 { get; set; }
                public string Item196 { get; set; }
                public string Item197 { get; set; }
                public string Item198 { get; set; }
                public string Item199 { get; set; }
                public string Item200 { get; set; }
                public string Item201 { get; set; }
                public string Item202 { get; set; }
                public string Item203 { get; set; }
                public string Item204 { get; set; }
                public string Item205 { get; set; }
                public string Item206 { get; set; }
                public string Item207 { get; set; }
                public string Item208 { get; set; }
                public string Item209 { get; set; }
                public string Item210 { get; set; }
                public string Item211 { get; set; }
                public string Item212 { get; set; }
                public string Item213 { get; set; }
                public string Item214 { get; set; }
                public string Item215 { get; set; }
                public string Item216 { get; set; }
                public string Item217 { get; set; }
                public string Item218 { get; set; }
                public string Item219 { get; set; }
                public string Item220 { get; set; }
                public string Item221 { get; set; }
                public string Item222 { get; set; }
                public string Item223 { get; set; }
                public string Item224 { get; set; }
                public string Item225 { get; set; }
                public string Item226 { get; set; }
                public string Item227 { get; set; }
                public string Item228 { get; set; }
                public string Item229 { get; set; }
                public string Item230 { get; set; }
                public string Item231 { get; set; }
                public string Item232 { get; set; }
                public string Item233 { get; set; }
                public string Item234 { get; set; }
                public string Item235 { get; set; }
                public string Item236 { get; set; }
                public string Item237 { get; set; }
                public string Item238 { get; set; }
                public string Item239 { get; set; }
                public string Item240 { get; set; }
                public string Item241 { get; set; }
                public string Item242 { get; set; }
                public string Item243 { get; set; }
                public string Item244 { get; set; }
                public string Item245 { get; set; }
                public string Item246 { get; set; }
                public string Item247 { get; set; }
                public string Item248 { get; set; }
                public string Item249 { get; set; }
                public string Item250 { get; set; }
                public string Item251 { get; set; }
                public string Item252 { get; set; }
                public string Item253 { get; set; }
                public string Item254 { get; set; }
                public string Item255 { get; set; }
                public string Item256 { get; set; }
                public string Item257 { get; set; }
                public string Item258 { get; set; }
                public string Item259 { get; set; }
                public string Item260 { get; set; }
                public string Item261 { get; set; }
                public string Item262 { get; set; }
                public string Item263 { get; set; }
                public string Item264 { get; set; }
                public string Item265 { get; set; }
                public string Item266 { get; set; }
                public string Item267 { get; set; }
                public string Item268 { get; set; }
                public string Item269 { get; set; }
                public string Item270 { get; set; }
                public string Item271 { get; set; }
                public string Item272 { get; set; }
                public string Item273 { get; set; }
                public string Item274 { get; set; }
                public string Item275 { get; set; }
                public string Item276 { get; set; }
                public string Item277 { get; set; }
                public string Item278 { get; set; }
                public string Item279 { get; set; }
                public string Item280 { get; set; }
                public string Item281 { get; set; }
                public string Item282 { get; set; }
                public string Item283 { get; set; }
                public string Item284 { get; set; }
                public string Item285 { get; set; }
                public string Item286 { get; set; }
                public string Item287 { get; set; }
                public string Item288 { get; set; }
                public string Item289 { get; set; }
                public string Item290 { get; set; }
                public string Item291 { get; set; }
                public string Item292 { get; set; }
                public string Item293 { get; set; }
                public string Item294 { get; set; }
                public string Item295 { get; set; }
                public string Item296 { get; set; }
                public string Item297 { get; set; }
                public string Item298 { get; set; }
                public string Item299 { get; set; }
                public string Item300 { get; set; }
                public string Item301 { get; set; }
                public string Item302 { get; set; }
                public string Item303 { get; set; }
                public string Item304 { get; set; }
                public string Item305 { get; set; }
                public string Item306 { get; set; }
                public string Item307 { get; set; }
                public string Item308 { get; set; }
                public string Item309 { get; set; }
                public string Item310 { get; set; }
                public string Item311 { get; set; }
                public string Item312 { get; set; }
                public string Item313 { get; set; }
                public string Item314 { get; set; }
                public string Item315 { get; set; }
                public string Item316 { get; set; }
                public string Item317 { get; set; }
                public string Item318 { get; set; }
                public string Item319 { get; set; }
                public string Item320 { get; set; }
                public string Item321 { get; set; }
                public string Item322 { get; set; }
                public string Item323 { get; set; }
                public string Item324 { get; set; }
                public string Item325 { get; set; }
                public string Item326 { get; set; }
                public string Item327 { get; set; }
                public string Item328 { get; set; }
                public string Item329 { get; set; }
                public string Item330 { get; set; }
                public string Item331 { get; set; }
                public string Item332 { get; set; }
                public string Item333 { get; set; }
                public string Item334 { get; set; }
                public string Item335 { get; set; }
                public string Item336 { get; set; }
                public string Item337 { get; set; }
                public string Item338 { get; set; }
                public string Item339 { get; set; }
                public string Item340 { get; set; }
                public string Item341 { get; set; }
                public string Item342 { get; set; }
                public string Item343 { get; set; }
                public string Item344 { get; set; }
                public string Item345 { get; set; }
                public string Item346 { get; set; }
                public string Item347 { get; set; }
                public string Item348 { get; set; }
                public string Item349 { get; set; }
                public string Item350 { get; set; }
                public string Item351 { get; set; }
                public string Item352 { get; set; }
                public string Item353 { get; set; }
                public string Item354 { get; set; }
                public string Item355 { get; set; }
                public string Item356 { get; set; }
                public string Item357 { get; set; }
                public string Item358 { get; set; }
                public string Item359 { get; set; }
                public string Item360 { get; set; }
                public string Item361 { get; set; }
                public string Item362 { get; set; }
                public string Item363 { get; set; }
                public string Item364 { get; set; }
                public string Item365 { get; set; }
                public string Item366 { get; set; }
                public string Item367 { get; set; }
                public string Item368 { get; set; }
                public string Item369 { get; set; }
                public string Item370 { get; set; }
                public string Item371 { get; set; }
                public string Item372 { get; set; }
                public string Item373 { get; set; }
                public string Item374 { get; set; }
                public string Item375 { get; set; }
                public string Item376 { get; set; }
                public string Item377 { get; set; }
                public string Item378 { get; set; }
                public string Item379 { get; set; }
                public string Item380 { get; set; }
                public string Item381 { get; set; }
                public string Item382 { get; set; }
                public string Item383 { get; set; }
                public string Item384 { get; set; }
                public string Item385 { get; set; }
                public string Item386 { get; set; }
                public string Item387 { get; set; }
                public string Item388 { get; set; }
                public string Item389 { get; set; }
                public string Item390 { get; set; }
                public string Item391 { get; set; }
                public string Item392 { get; set; }
                public string Item393 { get; set; }
                public string Item394 { get; set; }
                public string Item395 { get; set; }
                public string Item396 { get; set; }
                public string Item397 { get; set; }
                public string Item398 { get; set; }
                public string Item399 { get; set; }
                public string Item400 { get; set; }
                public string Item401 { get; set; }
                public string Item402 { get; set; }
                public string Item403 { get; set; }
                public string Item404 { get; set; }
                public string Item405 { get; set; }
                public string Item406 { get; set; }
                public string Item407 { get; set; }
                public string Item408 { get; set; }
                public string Item409 { get; set; }
                public string Item410 { get; set; }
                public string Item411 { get; set; }
                public string Item412 { get; set; }
                public string Item413 { get; set; }
                public string Item414 { get; set; }
                public string Item415 { get; set; }
                public string Item416 { get; set; }
                public string Item417 { get; set; }
                public string Item418 { get; set; }
                public string Item419 { get; set; }
                public string Item420 { get; set; }
                public string Item421 { get; set; }
                public string Item422 { get; set; }
                public string Item423 { get; set; }
                public string Item424 { get; set; }
                public string Item425 { get; set; }
                public string Item426 { get; set; }
                public string Item427 { get; set; }
                public string Item428 { get; set; }
                public string Item429 { get; set; }
                public string Item430 { get; set; }
                public string Item431 { get; set; }
                public string Item432 { get; set; }
                public string Item433 { get; set; }
                public string Item434 { get; set; }
                public string Item435 { get; set; }
                public string Item436 { get; set; }
                public string Item437 { get; set; }
                public string Item438 { get; set; }
                public string Item439 { get; set; }
                public string Item440 { get; set; }
                public string Item441 { get; set; }
                public string Item442 { get; set; }
                public string Item443 { get; set; }
                public string Item444 { get; set; }
                public string Item445 { get; set; }
                public string Item446 { get; set; }
                public string Item447 { get; set; }
                public string Item448 { get; set; }
                public string Item449 { get; set; }
                public string Item450 { get; set; }
                public string Item451 { get; set; }
                public string Item452 { get; set; }
                public string Item453 { get; set; }
                public string Item454 { get; set; }
                public string Item455 { get; set; }
                public string Item456 { get; set; }
                public string Item457 { get; set; }
                public string Item458 { get; set; }
                public string Item459 { get; set; }
                public string Item460 { get; set; }
                public string Item461 { get; set; }
                public string Item462 { get; set; }
                public string Item463 { get; set; }
                public string Item464 { get; set; }
                public string Item465 { get; set; }
                public string Item466 { get; set; }
                public string Item467 { get; set; }
                public string Item468 { get; set; }
                public string Item469 { get; set; }
                public string Item470 { get; set; }
                public string Item471 { get; set; }
                public string Item472 { get; set; }
                public string Item473 { get; set; }
                public string Item474 { get; set; }
                public string Item475 { get; set; }
                public string Item476 { get; set; }
                public string Item477 { get; set; }
                public string Item478 { get; set; }
                public string Item479 { get; set; }
                public string Item480 { get; set; }
                public string Item481 { get; set; }
                public string Item482 { get; set; }
                public string Item483 { get; set; }
                public string Item484 { get; set; }
                public string Item485 { get; set; }
                public string Item486 { get; set; }
                public string Item487 { get; set; }
                public string Item488 { get; set; }
                public string Item489 { get; set; }
                public string Item490 { get; set; }
                public string Item491 { get; set; }
                public string Item492 { get; set; }
                public string Item493 { get; set; }
                public string Item494 { get; set; }
                public string Item495 { get; set; }
                public string Item496 { get; set; }
                public string Item497 { get; set; }
                public string Item498 { get; set; }
                public string Item499 { get; set; }
                public string Item500 { get; set; }
               
                public HugeTuple(List<string> properties)
                {
                    if (properties.Count > 500)
                        throw new Exception("HugeTuple only supports up to 500 properties!");
                    Type type = this.GetType();
                    for (int i = 0; i < properties.Count; i++)
                    {
                        type.GetProperty("Item" + (i + 1)).SetValue(this, properties[i], null);
                    }
                }
            }



  • As the saying goes:

     MY EYES!  THE GOGGLES DO NOTHING!


  • 🚽 Regular

    That'll be 500 bad code offsets, please. Thanks a lot.



  • @mott555 said:

            private sealed class HugeTuple
            {
                public string Item1 { get; set; }

    Why? What? How? And more importantly, are you mad?

    Please tell me you at least generated the code. But still, why?

    Oh, and you owe me a new screen. The previous one melted, thankyouverymuch.



  • Since you already started going down that route, I think you should also unroll that loop in the constructor and initialize all 500 properties by hand. That will both avoid reflection and serve as an appropiate punishment.



  • @b-redeker said:

    @mott555 said:

            private sealed class HugeTuple
            {
                public string Item1 { get; set; }

    Why? What? How? And more importantly, are you mad?

    Please tell me you at least generated the code. But still, why?

    Oh, and you owe me a new screen. The previous one melted, thankyouverymuch.

     

    Yeah, I generated the code. No way I was going to type all that by hand!

    Basically the problem is I had a two-dimensional array of data to display. The Silverlight DataGrid control only allows you to bind to properties to populate the columns, not to elements of a collection. And you can't manually create cells and rows and add data to the DataGrid in code, you HAVE to use data binding. So this is a workaround to convert a single collection (representing the properties or fields in a single row) into a type where each value gets its own property, which can be used for data binding. I picked 500 because that's far beyond anything we should encounter in production.

    So once I receive the data (a collection of collections of strings) I convert it to a collection of BigTuples. That collection can be used as the ItemsSource to the DataGrid.

    Ugly, yes. Maddening, yes. Is there something better? Probably, but I couldn't find it. Does it work? Yes.



  • @mott555 said:

    private sealed class HugeTuple
     

    You misspelled HugeTurdPile



  • TRWTF is you using an interface element which is obviously unsuited for your needs and creating a crappy workaround just so you can do something which isn't that hard to do in another way. E.g. something I just thought up in like 5 seconds: Use a standard Grid; find the maximum number of columns you need; create them; Use a double for(each) loop to create rows and fill the cells

    For those of you who don't understand: the DataGrid has columns which all are linked to a specific Property of a class. You then feed it a collection of your objects (all of the same class) and the grid is filled with a row for each object and fills that row with objects's Properties.

    It is by nature unsuitable to display a Collection which contains objects of different classes. It will then only be able to display Properties that every object in the collection has, i.e. none.



  • @dtech said:

    TRWTF is you using an interface element which is obviously unsuited for your needs and creating a crappy workaround just so you can do something which isn't that hard to do in another way. E.g. something I just thought up in like 5 seconds: Use a standard Grid; find the maximum number of columns you need; create them; Use a double for(each) loop to create rows and fill the cells

    For those of you who don't understand: the DataGrid has columns which all are linked to a specific Property of a class. You then feed it a collection of your objects (all of the same class) and the grid is filled with a row for each object and fills that row with objects's Properties.

    It is by nature unsuitable to display a Collection which contains objects of different classes. It will then only be able to display Properties that every object in the collection has, i.e. none.

     

    TRWTF is me posting that crap and then defending it. 

    DataGrid has some nice features like being able to click the headers to sort data by that field, resizing columns, etc. And it offers consistency. We use DataGrids elsewhere, if this wasn't some user will get confused and complain. And whatever the current dataset is, all objects are of the same class and have the same properties, it's never trying to display a dataset with mixed classes. The underlying type will just change depending on what site it is, what data the site works with, the user's login, etc.



  • @El_Heffe said:

    @mott555 said:

    private sealed class HugeTuple
     

    You misspelled HugeTurdPile

    You can't fool me.  It's turd-piles all the way down!

     



  • You do realize that all you needed was the StringCollection class right? Just saying



  • @mott555 said:

    Ugly, yes. Maddening, yes. Is there something better? Probably, but I couldn't find it. Does it work? Yes.

    If you ever find yourself merely contemplating writing code like this, you don't need to ask "is there a better way?" because it is obviously impossible to find a worse way than how you're doing it. Being a lazy programmer is good, but there is such a thing as too lazy and "I couldn't find a better solution" is exactly that. Ever heard of Stack Overflow?

    Hell's bells, if I ever found shit like this checked into the coedebases I work on, I would hunt down the perpetrator and kick his/her/its ass. Then I would get a written warning issued to that person, and then I would spend the rest of my career trying to get them fired for incompetence.



  • @The_Assimilator said:

    Hell's bells, if I ever found shit like this checked into the coedebases I work on, I would hunt down the perpetrator and kick his/her/its ass. Then I would get a written warning issued to that person, and then I would spend the rest of my career trying to get them fired for incompetence.

    Then: anger management class!



  • @blakeyrat said:

    @The_Assimilator said:
    Hell's bells, if I ever found shit like this checked into the coedebases I work on, I would hunt down the perpetrator and kick his/her/its ass. Then I would get a written warning issued to that person, and then I would spend the rest of my career trying to get them fired for incompetence.

    Then: anger management class!

    I've always though that I manage my anger very well - isn't it best to remove that which is making you angry in the first place? (Much like a zombie apocalypse; you manage that by shooting undead in the face, preferably with a shotgun.) Hell, I always thought being proactive was a desirable trait for an employee.



  • @ObiWayneKenobi said:

     MY EYES!  THE GOGGLES DO NOTHING!


    Ah—then Sir requires a pair of Joo Janta 200 Super-Chromatic Peril Sensitive Sunglasses!



  • reflection is fun



  • @mott555 said:

    Is there something better? Probably, but I couldn't find it.

    It annoys the crap out of me that I don't have an answer to this (geek).

    A collection of collections; are we talking like multiple groups of customers, or a list of orders with orderlines, or....? So is this something you can even show in a datagrid? Wouldn't a treeview make more sense?



  • Perhaps I'm not quite understanding the issue, but it seems to me that this will just give you a DataGrid with 500 columns titled "Item1"..."Item500"? Or are you explicitly setting the titles on the actual datagrid? And if you're explicitly setting the titles on the datagrid, then presumably you know what fields should be on the backing objects, right? In which case, why not create an object containing the exact fields you need and using a collection of those as the data source? It looks like you're trying to make something far more general than it has to be, but maybe I'm missing something.



  • @b-redeker said:

    @mott555 said:

    Is there something better? Probably, but I couldn't find it.

    It annoys the crap out of me that I don't have an answer to this (geek).

    A collection of collections; are we talking like multiple groups of customers, or a list of orders with orderlines, or....? So is this something you can even show in a datagrid? Wouldn't a treeview make more sense?

     

    It's for a custom  ArcGIS map viewer application and it can view any related data (1:M relationship in the geodatabase) for a feature class. For example, sales history on parcels, building info on parcels, inspection history for street signs or manholes. Pretty much anything. It varys from site to site or even within a site depending on what feature class you're looking at. The top-level collection is a collection of records, so each item (which happens to be another collection) represents a single record in the table. Then each item is a collection containing KeyValuePairs of the property name and the property value. It's a List<Dictionary<string, object>>.

    @toth said:

    Perhaps I'm not quite understanding the issue, but it seems to me that this will just give you a DataGrid with 500 columns titled "Item1"..."Item500"? Or are you explicitly setting the titles on the actual datagrid? And if you're explicitly setting the titles on the datagrid, then presumably you know what fields should be on the backing objects, right? In which case, why not create an object containing the exact fields you need and using a collection of those as the data source? It looks like you're trying to make something far more general than it has to be, but maybe I'm missing something.
    								    </p><p></blockquote> </p><p>Once I get the data back from the web service I loop over the results to find column names and explicitly add DataGridTextColumns to the DataGrid for each property/attribute, setting the header to the proper display value and setting the binding to "Item1", "Item2", etc, up to the number of attributes. And yes it's very general. On one site it might display data from a parcel sales history table with 20 - 25 columns, on another site it might display manhole inspection history with only 15 columns, etc. </p><p>I found a couple topics about this on the Silverlight.net forums about this apparent DataGrid limitation. It seems the "preferred" method of handling this is to create a type at runtime based on your dataset and then binding the DataGrid to it (yikes). </p>


  • @mott555 said:

    I found a couple topics about this on the Silverlight.net forums about this apparent DataGrid limitation. It seems the "preferred" method of handling this is to create a type at runtime based on your dataset and then binding the DataGrid to it (yikes).

    Why is that a "yikes?" Sounds like a good solution to me.



  • @blakeyrat said:

    @mott555 said:
    I found a couple topics about this on the Silverlight.net forums about this apparent DataGrid limitation. It seems the "preferred" method of handling this is to create a type at runtime based on your dataset and then binding the DataGrid to it (yikes).

    Why is that a "yikes?" Sounds like a good solution to me.

    The sample I found involved generating C# code at runtime and compiling it to an assembly which was then loaded via reflection. I also remember seeing the sample injecting CLR op-codes into the output as well. Clever, but too much work and it sounds like it would bump into Silverlight's security model.



  • @mott555 said:

    I found a couple topics about this on the Silverlight.net forums about this apparent DataGrid limitation. It seems the "preferred" method of handling this is to create a type at runtime based on your dataset and then binding the DataGrid to it (yikes).

    When I run into these situations, I simply create a wrapper class to fetch the collection elements and expose them as properties.  It shouldn't be necessary to create a general-purpose solution for this entire class of problems.


  • @Jaime said:

    When I run into these situations, I simply create a wrapper class to fetch the collection elements and expose them as properties.

     

    ...and thus, we've come full circle!



  • @PSWorx said:

    @Jaime said:

    When I run into these situations, I simply create a wrapper class to fetch the collection elements and expose them as properties.

     

    ...and thus, we've come full circle!

    Not full circle.  The OP created generic properties and mapped them to a collection using reflection.  I'm advocating creating a wrapper class for each case, naming the properties sanely, and writing proper implementation code for each.


  •  @Jaime said:

    Not full circle.  The OP created generic properties and mapped them to a collection using reflection.  I'm advocating creating a wrapper class for each case, naming the properties sanely, and writing proper implementation code for each.

    Ah, I misunderstood you. I agree, that would be a sensible solution.



  • Is this SL4? if So you could use an indexer

    public string this[string key]
    {
      get
      {
        return fromSomeDictionary[key];
      }
    }
    

    And bind using {Binding [key]}



    EDIT: Even in older version you could have just used {Binding [0]} instead of {Binding Item0}



  • @Jaime said:

    @PSWorx said:

    @Jaime said:

    When I run into these situations, I simply create a wrapper class to fetch the collection elements and expose them as properties.

     

    ...and thus, we've come full circle!

    Not full circle.  The OP created generic properties and mapped them to a collection using reflection.  I'm advocating creating a wrapper class for each case, naming the properties sanely, and writing proper implementation code for each.

    If I understand correctly, the issue is as follows:

    The clients can issue requests for certain data to the OP's application. So, for instance a client might say "Give me sales history, building info, oh, and throw in some inspection history on street signs." Another client might have an entirely different set of information that they want, which may or may not overlap with the aforementioned client.

    (if that's not correct, correct me).

    So you couldn't have separate wrapper classes for various configurations, or you'd have to have a separate class for each possible permutation. Alternatively, you could have a huge class with all the different possible pieces of information on it, and hide the columns that the client didn't request, but of course that would only be marginally better than the OP collection of strings (they would be typed and named appropriately, but there still might be several hundred such properties).



  • @toth said:

    So, for instance a client might say "Give me sales history, building info, oh, and throw in some inspection history on street signs."

    This is how the OP said it:

    @mot555 said:

    it can view any related data (1:M relationship in the geodatabase) for a feature class. For example, sales history on parcels, building info on parcels, inspection history for street signs or manholes. Pretty much anything. It varys from site to site or even within a site depending on what feature class you're looking at.

    Depending on the number of permutations, this soon becomes impossible to do statically/predefined, and you will have to do something dynamic/runtime. So far, so good. But can't you load the data dynamically in a dataset and bind that to the grid?



  •  @jaime said:

    Not full circle.  The OP created generic properties and mapped them to a collection using reflection.  I'm advocating creating a wrapper class for each case, naming the properties sanely, and writing proper implementation code for each.

    The only problem is we have dozens of deployments of this application and we're always getting more. We try to keep the code as generic as possible so we don't end up with any code specific to a single deployment because that makes maintenance a nightmare. It's really easy to handle when all deployments use the exact same code. All the various deployments actually get their data for this feature from a single web method which is why it returns such a generic type. It just returns different data depending on which site called it.

    @XIU said:

    Is this SL4? if So you could use an indexer

    public string this[string key]
    {
    get
    {
    return fromSomeDictionary[key];
    }

    }

    And bind using {Binding [key]}



    EDIT: Even in older version you could have just used {Binding [0]} instead of {Binding Item0}

    I'll have to try that when I get a chance. I wasn't aware you could bind to an indexed property like that.

    @toth said:

    If I understand correctly, the issue is as follows:

    The clients can issue requests for certain data to the OP's application.
    So, for instance a client might say "Give me sales history, building
    info, oh, and throw in some inspection history on street signs." Another
    client might have an entirely different set of information that they
    want, which may or may not overlap with the aforementioned client.

    (if that's not correct, correct me).

    So you couldn't have separate wrapper classes for various
    configurations, or you'd have to have a separate class for each possible
    permutation. Alternatively, you could have a huge class with all the
    different possible pieces of information on it, and hide the columns
    that the client didn't request, but of course that would only be
    marginally better than the OP collection of strings (they would be typed
    and named appropriately, but there still might be several hundred such
    properties).

    Pretty much, though there is never any overlap in information between clients. The City of St. Joseph doesn't care about manhole inspection history for the sewer system in Rolla.

    @b-redeker said:

    Depending on the number of permutations, this soon becomes impossible to
    do statically/predefined, and you will have to do something
    dynamic/runtime. So far, so good. But can't you load the data
    dynamically in a dataset and bind that to the grid?

    If you're talking about an ADO.NET DataSet, those aren't available in Silverlight. Silverlight has no idea what a database is.



  • Why make it sealed? I could have used that code to write the most awesome derived class ever! =(



  • @mott555 said:

    Silverlight has no idea what a database is.

    Ah, oh, uh. What?

    Clearly I know even less about Silverlight than about other topics, but does this example help you?

    http://forums.silverlight.net/forums/t/16733.aspx



  • @b-redeker said:

    @mott555 said:

    Silverlight has no idea what a database is.

    Ah, oh, uh. What?

    Silverlight's like Flash. (Which also doesn't know what a database is.) It's a whole 'nother level.



  • @b-redeker said:

    @mott555 said:
    Silverlight has no idea what a database is.
    Ah, oh, uh. What?

    Clearly I know even less about Silverlight than about other topics, but does this example help you?

    http://forums.silverlight.net/forums/t/16733.aspx

    I found that topic earlier. The TypeBuilder and CreateProperty and ILGenerator stuff scared me away.

    As far as database support in Silverlight, the Silverlight runtime itself cannot access any relational database. It can ask a web service to talk to the database for it though. That's why Silverlight and WCF are usually used together.


Log in to reply