How do I bind a GridView to a custom object? - asp.net

If I have the following ASP.NET code (it's not complete - obviously there's a lot missing, but none of it matters):
<asp:GridView>
<Columns>
<asp:TemplateField>
<ItemTemplate>
My Label: <asp:Label />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
My Text Box: <asp:TextBox />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And if I have something like this in the CodeBehind:
Private MyListOfObjects As List(Of MyObject)
...
Public Class MyObject
Public MyTextBoxString as String
Public MyLabelString as String
End Class
How can I bind the GridView so that one row is equivalent to one item in my MyListOfObjects list, and so that the data will populate and persist across page loads or postbacks? I've never done custom databinding like this before, so a full explanation would be very helpful. All the tutorials I've come across so far only talk about using GridViews directly with Database query results, and that's not what I need.
Thanks!

Just set the datasource of the gridview to your object.
MyGridView.DataSource = myList
MyGridView.DataBind()
Here's a very similiar post:
Binding a method that returns List<employee> to a gridview
Looks like you are using a list in vb.net. Remember lists can hold integers, strings, dates, objects (these include user defined types (your object)).
So you can bind a gridview to a list object by setting the datasource property to your list.
In the above example, myList, might hold a ton of employee objects, etc. So assign it to the datasource and .DataBind() and voila a gridview with each row containing your object.

You can do something like
My Label: <asp:Label id="myLabel" runat="server" Text='<%# Eval("MyTextBoxString") %>' />
in the markup and similar stuff for your textbox.
GridView1.DataSource = MyListOfObjects
GridView1.DataBind()

First remember any binding controls like GridView, DropdownList e.t.c bind to public properties, so first make your public members to public properties.
Then create objects of MyObject class and add them to your List<MyObject> object
Finally you can persist this list object by saving it in Session or ViewState to maintain it after postbacks.
I hope you can do it now!!! you can ask for more help if neccessary.

Related

ASP.Net WebForms Model Binding GridView Using Entity Framework 5.0.0.0

im binding List<T> to my GridView where T is the object created by EntityFramework from my database. There is a foreign key in my said table and i want to display its corresponding text value in the GridView.
<asp:TemplateField HeaderText="Foreign Key Type">
<ItemTemplate>
<asp:Label ID="LabelID" Visible="true" runat="server"
Text="<%# Item.<foreignKeyTable>.Text %>" >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
when i do that i get the following error
The operation cannot be completed because the DbContext has been disposed.
how can i get pass that
Most likely, you have query code that does the following:
using (var ctx = new SomeContext())
{
var data = ctx.Data.Where(..).ToList();
return data;
}
You may not have a using, but either way, the context has it's Dispose() method on it, and any unloaded navigation properties will always fail, because there is no context alive for it to attach to. Even though you may use the context within a method, and may not be storing it globally, as long as you aren't explicitly disposing the context, by calling the method or with a using statement, you will be fine.

ItemType when datasource is datareader

I want to set the ItemType in my repeater, but my datasource is a datareader, not a class.
How can I do thats?
Help me please.. Thank
If the DataSource is a DataReader, your ItemType needs to be either a DbDataRecord (which implements IDataRecord) or better, IDataRecord directly.
Example (assuming you are getting an Id column back):
<asp:Repeater ID="rpt" ItemType="System.Data.IDataRecord" runat="server" >
<ItemTemplate>
<%# Item.GetInt32(Item.GetOrdinal("Id")) %>
</ItemTemplate>
</asp:Repeater>
But I think the above is probably worse than simply doing Eval or Cast. I can see the benefit when you are binding to a custom business object but not when you are using a DataReader.

Getting the bound type of templated column controls in gridView

Hi I have following grid view
<asp:GridView ID="grdSettings" runat="server" Height="400px" Width="100%"
AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="XYZ" BoundFieldName="XYZTypeName">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>'>
</asp:Label>
<asp:Label ID="lblCostumerId" runat="server" Text='<%#Bind("CustomerId") %>'></asp:Label>
</asp:TemplateField>
</Columns>
</asp:GridView>
Which is bound by List of Customer ,class is as follows
Class Customer
{
public string CustomerName { get; set; }
public int CustomerId { get; set; }
}
Now on a method named GetGridStuff() , i need to iterate in every column and get the type that was bound to controls in template field. For example in case of the first control in the template field
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>' >
</asp:Label>
I need to know what type of property data it contains , in this case its CustomerName. I need to get this dynamically at run time as to write code in part of program which is not aware if this grid structure. I have the grid object with me and i can access all properties.
Let me try this, being away from coding for more than 5 years now and if I understand the problem correctly as you may want to handle it through server side code.
Look for an event called ItemBound (or similar forgive me for my memory), this gives you values of all the items in current row (Properties). You also need to declare temp control types (label,textbox etc) and assign corresponding value to these controls using e.FindControl with appropriate type casting, e.g. Label l = (Label)e.Findcontrol("Name")
downside of the approach you should avoid too much of process as it is going to execute on every row creating of the grid.
Let me know if you still want a precise code to handle the problem but otherwise this description should at least help you to look for Row Level Event to crack the prob and also encourage you to stick to tech forums :)

Using a FormView with LINQ

I am using a FormView control to edit a record fetched using a LinqDataSource. In essence, the markup for the FormView and the data source looks like this:
<asp:FormView ID="RuleInstancePropertiesFormView" runat="server" DataKeyNames="RuleInstanceId"
DataSourceID="RuleInstanceDataSource" DefaultMode="Edit" Visible="false"
CssClass="PropertiesTable">
<EditItemTemplate>
<asp:Label ID="RuleInstanceId" runat="server" Text='<%# Eval("RuleInstanceId") %>' />
<telerik:RadTextBox ID="RuleInstanceNameTextBox"
runat="server"
Text='<%# Bind("Rule.Name") %>' />
<telerik:RadTextBox ID="LimitIndexTextBox"
runat="server"
Text='<%# Bind("LimitIndex") %>' />
</EditItemTemplate>
</asp:FormView>
<asp:LinqDataSource ID="RuleInstanceDataSource" runat="server"
ContextTypeName="Questionnaire.QuestionnairesDataContext"
TableName="RuleInstances" EnableUpdate="true">
</asp:LinqDataSource>
The record I am editing has a foreign key reference to another table, namely the "Rule" table. In the FormView, I need to edit fields in this foreign key table (I have a binding to the Rule.Name field in the above code).
When I try to save my changes, the local field (LimitIndex) is saved correctly in the database, but the foreign field (Rule.Name) isn't. I'm a bit puzzled by this, as the FormView correctly fetches the current value of Rule.Name, but refuses to persist it back to the LINQ object.
I hope anyone can help, thanks :)
You're specifying which table to use as a datasource using the TableName property. This controls which table Linq2Sql will update and so is limiting the update to just the RulesInstances table.
Okay, I found an alternative (but not elegant) solution. I use an ObjectDataSource instead of a LinqDataSource. I have then defined an class
public class RuleInstanceProjection {
public RuleInstanceProjection(){}
// Bind properties from the LINQ object.
public RuleInstanceProjection(RuleInstance instance) { ... }
public int RuleInstanceId {get; set;}
public string Rule_Name {get;set;}
...
}
The object above takes a RuleInstance as constructor, and updates its own properties with all the values that I need in the FormView. I then configure the ObjectDataSource to use another custom class, RuleInstanceProjectionDS, as data source. This class handles updates and inserts of my custom RuleInstanceProjection class:
public class RuleInstanceProjectionDS
{
public RuleInstanceProjectionDS() { }
public RuleInstanceProjection GetRuleInstance(int ruleInstanceId)
{ /* Retrieve the RuleInstance, and instantiate
a RuleInstanceProjection object */ }
public void SaveInstanceProjection(InstanceProjection projection)
{ /* Map fields back to the LINQ objects and submit. */ }
}
I can therefore handle all mappings in these two classes. Now, I only have to decide if this is actually better than updating the GUI controls directly...
I must say it isn't the most elegant solution, and I hope there will be a better data binding framework in later versions of ASP.NET. The current one only seems to handle the most trivial cases :(.

Binding Gridview to IList<BusinessObject> that contains an IList<BusinessObject>

I'm having trouble figuring out how to bind a custom IList to a gridview. The IList contains another custom IList. I need to bind a property from this IList to the gridview.
public class Seminar : BusinessObject
{
private string _description = String.Empty;
private List<User> _attendees;
public string Description {get {return _description;} set {_description = value;}}
public List<User> Attendees {get {return _attendees;} set {_attendees = value;}}
}
public class User : BusinessObject
{
private string _name = String.Empty;
public string Name { get { return _name; } set { _name = value; } }
}
Backend page.aspx.cs:
List<Seminar> seminarList = SeminarManager.Get(id);
gridSeminars.DataSource = seminarList;
gridSeminars.DataBind();
Frontend page.aspx:
<asp:GridView ID="gridSeminars" runat="server">
<Columns>
<asp:BoundField DataField="Id" />
<asp:BoundField DataField="Description" />
<asp:BoundField DataField="Name" />
</Columns>
</asp:GridView>
The problem is with populating the "Name" field in the gridview. All suggestions are welcome.
Have you tried,
Attendees.Name
Edit:
Attendees is an IEnumerable itself. The above suggestion only works with Attendee.Name
If you wan´t to display all the attendees you need to make a templatefield and maybe use a repeater or something...something like:
<asp:TemplateField>
<ItemTemplate>
<asp:Repeater runat="server" DataSource='<%# Eval("Attendees") %>'>
<ItemTemplate>
<tr>
<td>
Name
</td>
<td>
<%# Eval("Name")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
Johan's answer is a good one, specifically the part about how you are trying to bind a collection to a column that is expecting something that can be converted to a string.
Another option would be to put a TemplateField that contains the bindable control (Repeater, another GridView, etc.) you choose to use and binding the DataSource property to the Attendees property of your business object.
Also, I don't if you're married to the idea of using a GridView, but in cases like these where you need more control of the layout, I would suggest the new ListView (.NET 3.5) control, or lately I've just been using nested repeaters so I can have more refined control of the layout I am trying to generate.
Hope this helps.
I'm assuming you want to flatten the hierarchy and display one row for every user. If you have access to Linq, you can use this:
List<Seminar> seminarList = SeminarManager.Get(id);
gridSeminars.DataSource = from seminar in seminarList // loop through all of the seminars in the list
from user in seminar.Attendees // loop through all of the users in the current seminar
select new // create a new, flattened object to bind to.
{
seminar.Id,
seminar.Description,
user.Name
};
gridSeminars.DataBind();
http://weblogs.asp.net/zeeshanhirani/archive/2008/03/26/select-many-operator-part-1.aspx

Resources