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>
Related
I want to save student ID which is being set in a GridView. I want to save each row student ID into table through model whose object name is "am".
for (int i = 0; i < GridView1.Rows.Count; i++)
{
am.Std_ID= GridView1.Rows[i]["Std_ID"];
}
By using above method it says you cannot use indexing with GridView. What should I do.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="FullName" HeaderText="Student Names"/>
<asp:TemplateField ItemStyle-HorizontalAlign = "Center">
<ItemTemplate>
<asp:TextBox runat="server" ID="marks" Width="30px" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
My second question is in ItemTemplate I am using textbox. When I use its ID in function then Visual Studio says "marks doesn't exist in the current context". What should I do?
This will not exist: -
GridView1.Rows[i]["Std_ID"];
You don't access the data bound to a grid in this way; you need to reference the DataItem property.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem(v=vs.110).aspx
However your requirement is quite common, you could add a HiddenField control to the itemtemplate to store your ID, and retrieve the control (and therefore its value) in a server-side method.
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.
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 have a database table TravelRequest that contains, amongst other things, the fields SignOffName and SignOffDate. I have a table adapter TravelRequestTable based on this table. I have a DetailsView control which uses this via an ObjectDataSource. Should be all fairly standard stuff.
What I want to do is add a property called SignOffNameDate to the table adapter that combines the two fields and be able to bind to it in the DetailsView control. I want to do it programmatically rather than adding another column in the SQL because dealing with null values is tricky and depends on some other business rules.
This is what I tried:
public partial class TravelRequestDS
{
public partial class TravelRequestRow
{
public string SignOffNameDate
{
get { return CombineNameDate(SignOffName, SignOffDate); }
}
}
}
This property works fine when I access it programmatically, but when I try bind to it in my aspx page:
<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
<Fields>
<asp:BoundField DataField="SignOffNameDate"
HeaderText="Sign Off" />
...
I get an System.Web.HttpException exception, "A field or property with the name 'SignOffNameDate' was not found on the selected data source."
Any ideas how I can do this? Is there an attribute I need to add to the property?
If your objective is to display the combined result in a single non-editable field you can do it like this:
<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
<Fields>
<asp:TemplateField HeaderText="Sign Off" SortExpression="SignOffDate">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SignOffName") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("SignOffDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
...
If you don't want to change your table structure, change the default method that loads data into your adapter (usually called Fill), most likely you are doing a Select * From [TravelRequest] or selecting the individual fields, so what you could do is change your TableAdapter query select statement to something like
Select [YourCol1],[YourCol2], SignOffNameDate as Null From [TravelRequest]
Modifying the default query, and selecting SignOffNameDate as null will give you access to set this value
If I have this <asp:ButtonField runat="server" DataTextField="Name" CommandName="GetName"></asp:ButonField> Within a GridView. Is there any way to retrieve the DataTextField (Name) from the OnRowCommand method?
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource_Names"
DataKeyNames="ID,ModuleId" OnRowCommand="ChangeName">
Or alternatively, is there any way to make CommandName into an attribute where the command is dynamically entered based on given data, similar to the difference between DataTextField vs TextField.
I added <asp:BoundField DataField="Name" /> Then you can retrieve it using the same method here: GridView.onRowCommand Method
All I need to do now is find out a way to hide the field. If anyone knows how to, please let me know. Visible="false" gets rid of the field all together...
Not really sure if this is the best way, but it works:
Make your column a templatefield and bind the name value to a asp:hiddenfield control:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfName" runat="server" Value='<%# Eval("Name") %>'>
</asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>