Ignore inexistent column in dataset ( bound field )
Asked Answered
J

1

6

I'm working on a legacy windows forms project which I'm migrating to webforms.

There is a dataset which I bind to a gridview .

I have made all boundfields, so the gridview won't automatically generate the columns. Whenever I bind this dataset to the grid, some columns are missing in the dataset, so it's throwing errors about inexistent columns.

Is there any way to ignore missing columns in bound fields? Like, remove the bound column if it doesn't exist... or just ignore it?

Japha answered 3/8, 2016 at 11:44 Comment(8)
Why are you binding them, if they dont exist? Also it would help if you'd share some codeMiracle
It's because I have three different datasets. Some columns are present in all of them, but other columns are not. I wanted to do that so I don't have to make three separate gridviewsJapha
Hmmm okay. Is autogenerating-columns on these Grids an option? Is object-Binding an option? Did you do the binding in designer or in code?Miracle
I could auto-generate them but I'd have to reorder them and rename the headers. Actually I have found an alternative in which I create the gridview with no columns, then create them in run time (similar to auto-generate columns, but I have imeddiate control over header text and the column order)Japha
Tbh, save your bounty and do the binding in Code. There is no such option like Ignore-if-not-existsMiracle
Is it to late to switch to MVC? Aside from the obvious reasons, you could just pass data to a specific model and then pass a collection of it to a grid. It would make more sense for the specific task. Not to be that jerk, but I almost spit coffee onto my keyboard when I read the whole "legacy winforms apps" and how you're migrating it to a "webforms" app. (This last sentence is sarcasm and meant to be whimsical).Sempstress
I wish I could make it MVC. But the functions are too windows forms specific, web forms is the only one that looks similar. Also it wasn't up to me, I'm just an intern, gotta do what the folks ask me, right? XDJapha
@KevinBBurns looking at this question again, I just remembered that during the internship, after this project, I made another one using MVC (I never had made anything in aspnet mvc before) and I wanted to switch this project to MVC so badly, but then I had only 2 weeks left on my internship. But MVC would be waaaaaaay better than this shitty webforms thingJapha
J
0

I had to use a different approach:

I set the gridview to have no columns and autogeneratecolumns to false.

Then I created a XML with the list of all possible columns (this is a XML, not asp.net markup)

<Grid ID="grdSenha">
      <BoundField HeaderText="Status" />
      <BoundField DataField="Flg_Imprimiu" HeaderText="Imprimiu?" Visible="True" />
      <BoundField DataField="Nom_Localdest" HeaderText="Local Descarga" Visible="True" />
      <BoundField DataField="Dsc_Localdest" HeaderText="Descrição" Visible="True" />
      <BoundField DataField="Cod_Produto" HeaderText="Cod Prod" Visible="False" />
      <BoundField DataField="Dsc_Produto" HeaderText="Descrição Produto" Visible="True" />
      <BoundField DataField="Qtd_Transport" HeaderText="Qtde" Visible="True" />
      <BoundField DataField="Cod_Transport" HeaderText="Cod Trans" Visible="False" />
      [...]
</Grid>

Then, in my code, I'd select from the XML only the columns present in my datasource (using the DataField as key), then create the bound fields accordingly.

It works perfectly.

Japha answered 11/8, 2016 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.