Ignore inexistent column in dataset ( bound field ) - c#

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?

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.

Related

ASP.NET GridView using C#

I have a problem with gridview, I am using a gridview and listview on a page, when I select a row in the gridview I want to insert the data into the listview based on selection in the gridview. I am using a primary key in gridview.
PLease help me out
This is also called "Master-Detail".
You can find a great tutorial here.
In short, you need to make the row selectable.
<asp:GridView ID="ProductsGrid" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="AllProductsDataSource" EnableViewState="False">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ProductName"
HeaderText="Product" SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice"
DataFormatString="{0:c}" HeaderText="Unit Price"
HtmlEncode="False" SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
This is done with <asp:CommandField ShowSelectButton="True" /> in the code above.
The next step is to add you DetailsView to be binded to the selected element of the master gridview. This can be done because the selection return the primary key of the element, from there you can load the detail.
This do a PostBack, if you want you can use Ajax and remove the PostBack flickering.
I believe the select on the gridview should fire off the event SelectedIndexChanged(). You would use that event to create the datasource for your listview and then bind the datasource to your listview.

Binding DataTable to DetailsView control

I have a DetailsView control that I want to populate with a data table. I'm doing this in code behind. After checking that the data table is created successfully, the code is:
detailsView.DataSource = dataTable;
detailsView.DataBind();
I run the program and the detailsView doesn't show.Could the problem be that a data table potentially can have multiple rows and a details view only one? If that's the problem is there a way of getting around it? I made sure in creating the data table that it has only one row. I also tried binding only one row in the data table but a data table row isn't accepted as a data source. What's the problem?
You have to add in a Bound field or Set the DetailsView AutoGenerateRows="true" to display the field in the detailsView. I am sure you forget to do this..
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" >
<Fields>
<asp:BoundField DataField="FieldName" HeaderText="Field Title" />
.......................
.......................
</Fields>
</asp:DetailsView>
DetailView.DataSource = DataSetName;
DetailView.DataMember = DataTableName;
http://www.dotnetspider.com/forum/319762-Datasource-Datamember-In-GridView.aspx

Combobox Database value

I am making an ASPx and C# form with DevExpress comboboxes and an SQL database. I've pulled the data to one of the boxes that I need.
I need to assign a variable to one of the fields from the database that are pulled into the combobox.
<dx:ASPxComboBox ID="testNameBox" runat="server" CssClass="dropdown"
Font-Names="Tahoma" Font-Size="10px" ClientIDMode="AutoID"
DataSourceID="testSource" ValueType="System.String"
IncrementalFilteringMode="Contains">
<Columns>
<dx:ListBoxColumn FieldName="pkTestKey" Visible="False" />
<dx:ListBoxColumn FieldName="fkTestCode" Visible="False" />
<dx:ListBoxColumn Caption="Test Item Name" FieldName="Name" />
</Columns>
</dx:ASPxComboBox>
The point is that when I choose something in this combobox, another combobox displays related data from another table based on the fkCode value.
How would I pull that variable from this, and use it so that my query for the textbox displays the appropriate data based on that fkCode?
Take a look at this demo on DevExpress. You might want to search more about Cascading Comboboxes
<script>alert("demo for XSS")</script>

fluent nhibernate ... mapping 'lookup' value to DataGrid

To make this as simple as I can think to ask, if I have to classes:
Order, OrderType, where an Order has 1 to 1 relationship with OrderType, how can I Bind (List) to a datagrid, and see the desired column/field from OrderType?
When I bind a List to a datagrid, in the Order.OrderType 'field', I just get the name of the OrderType class with a guid. I need to be able to say something like 'use the 'Code' field from the OrderType class.
This isn't really an nhibernate question, as this problem could come up with any object, regardless of how's it's persisted. What I think you're asking is you want to have a list of orders in a grid, and you want to bind to a property of another property that is a class.
<asp:Gridview ID="gv1" runat="server">
<Columns>
<!-- regular properties -->
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="CreatedAt" HeaderText="DatePlaced" />
<!-- property of a property-->
<asp:TemplateField HeaderText="Order Code">
<ItemTemplate>
<asp:Label Text='<%#((Order)Container.DataItem).OrderType.Code %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
Hope that helps.
For WinForms, check out the solutions at Winforms DataGridView databind to complex type / nested property

Grid View with dictionary as datasource

Please help me out... I need to set a dictionary as datasource to a GridView. The dictionary contains objects with for example the attributes start and finish. I have only found information about connecting datasets, resultsets, sql adapters and so on to GridViews.
The GridView im trying to present my dictionary with, should also be editable, but I guess the NO1 issue is to just connect it.
If you just set it as the datasource and set autegenreate columns to true of the gridview it will work just fine. If you want to bind the columns explicitly then you bind them to the Key and Value properties. e.g.:
<asp:GridView runat="server" ID="MyGridView" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Key" />
<asp:BoundField DataField="Value" />
</Columns>
</asp:GridView>
Just set the Datasource property to your Dictionary object.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx#binding_to_data

Categories

Resources