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
Related
I have done gridviews before, where I bind all SQL data from data safe reader or specify column names and binds at the javascript level. This is a bit different and I am a bit stumped on how to proceed.
I have a business class that handles all SQL data queries.
This class is called to Fetch a LIST. This list contains collections and child collections.
var _InfoList = BusinessObjectClass.Get(_CriteriaKey);
I can access the list as such:-
Txtbox1.Text = _InfoList.ID#.ToString();
Now I am trying to bind one of the collections in the LIST to a gridview.:-
C#:-
gvwMembers.DataSource = _InfoList.Members;
gvwMembers.DataBind();
Where Members is a collection...
But this syntax doesn't add any thing to the gridview...The gridview is empty.
Second methodology:-
I also tried doing something like this:-
List<BusinessObjectClass> Members = new List<BusinessObjectClass>();
Members = _InfoList.Members;
gvwVendors.DataSource = Members;
gvwVendors.DataBind();
But to no avail.. this is because the problem lies in the 2nd statement:-
Members = _InfoList.Members.... this is not a valid assignment...can anyone help with this?
After fleshing out some details in the comments, it is perfectly fine to have a simple GridView with no columns defined, but make sure AutoGenerateColumns is not false. The default is true. This will create a column for you, based on each property of the object being bound.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
And in order to pick and choose which properties to display, define them in the <Columns> section.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Property1" HeaderText="Property1" />
<asp:TemplateField HeaderText="Property2">
<ItemTemplate>
<asp:Label ID="lblProperty2" runat="server" Text='<%# Eval("Property2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I currently have a gridview that is set up and working properly but I ran into a little bit of an issue. My grid's datasource is being set from a DB table and one of the columns in the table is the numeric ID of another table. Rather than displaying this numeric ID in the grid, I need to display a description column off of the other table. Can I add an additional column to my current gridview just for display purposes?
Yes you can accomplish this by using a template on your ID field rather than creating a new field. This link has some examples. This way you can custom format your existing column.
You can use a data-set and bind this dataset to the gridview.
Using the data-set, you can add rows, columns. The below example is a good one to add rows/columns to the grid-view. You can also follow the same example with a little bit of tweaks.
http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx
Set AutoGenerateColumns to false, and define the columns that you want to display:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" CellSpacing="0" AutoGenerateColumns="false" Width="100%">
<RowStyle BackColor="#ffffff" />
<AlternatingRowStyle BackColor="#f5f5dc" />
<HeaderStyle BackColor="Beige" ForeColor="#333333" Font-Bold="true" Height="25px" />
<Columns>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<%# Eval("Address") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The change should be made in 3 locations:
The datasource should have the desc column brought back with it. Do the join in the database and bring it all back at once so the front end will not need to make an insane amount of calls.
On display, you can set the id column to invisible. This can be done on the code behind or on the aspx.
On edit, if it is a value that is based off of a look-up table, you will need to create a drop down list. You can query for more info if edit is needed, if not, the top two points should cover your needs.
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
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
I am creating a GridView/DetailsView page. I have a grid that displays a bunch of rows, when a row is selected it uses a DetailsView to allow for Insert/Update.
My question is what is the best way to link these? I do not want to reach out to the web service again, all the data i need is in the selected grid view row. I basically have 2 separate data sources that share the same "DataObjectTypeName", the first data source retrieves the data, and the other to do the CRUD.
What is the best way to transfer the Selected Grid View row to the Details View? Am I going to have to manualy handle the Insert/Update events and call the data source myself?
Is there no way to link these two so they use the same data source ?
<asp:GridView ID="gvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsSearchData" >
<Columns>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
....Code...
<asp:DetailsView ID="dvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsCRUD" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
Visible="false" Width="100%">
<Fields>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
...
The standard way to do it would be to have the selected item of the griview be a control parameter to the objectdatasource you have wired up to the detailsview. I would probably not worry too much about the overhead of retreiving data that you already have unless you are catering to users with such slow connections that you want to avoid roundtrips to the webserver at all costs.
If you really want to avoid that thenyou could pull the data out of the gridview using javascript/jquery and then do your inserts/updates via ajax calls. It would require lots more coding though.
This is a really old thread, but in case anyone came here looking for an answer like I did, a simple solution is to add this function to your code:
(Note that this only works if the rows in your GridView match the entries in your DetailsView.)
protected void GridView1_OnSelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.SetPageIndex(GridView1.SelectedIndex);
}
And modify the GridView and DetailsView to include these settings:
<asp:GridView ... OnSelectedIndexChanged="GridView1_OnSelectedIndexChanged" ... >
<asp:DetailsView ... AllowPaging="True" ... >
This will make the selected page in the DetailsView match the selected index in the GridView.
You can hide the paging options in the DetailsView properties if you dont want users to navigate using paging in the DetailsView.