Array in gridview - asp.net

I have a string array data[].I need to bind this string array as datasource in gridview.
I have values in data array.
I write the code
gridview_forecast.DataSource = data;
gridview_forecast.DataBind();
I got an error as "A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem."

You cannot directly assign an array to GridView DataSource when GridView.AutoGenerateColumns is False. One way to do this convert your array to ArrayList or DataTable,
then assign it to GridView DataSource.
For an example click here

Related

Gridview DataKeyNames subclass property

I have a gridview bound to a list of objects like so List<myObject>.
The object has property which is a child object - an Address object.
I want to add the PostCode property of the Address object to the gridview's DataKeyNames property like so: DataKeyNames="Id, Address.Postcode", but keep getting the error myObject does not contain a property with the name Address.Postcode (it does...)
Any ideas?
EDIT: To clarify, I cannot modify myObject so that it exposes the Postcode as a property like so: myObject.Postcode. The Postcode property needs to sit where it is in the Address object (myObject property).
No you can't do it like this, it should be like..
DataKeyNames="Id, Postcode"
You need to get the PostCode column in your datasource object which you are going to bind to your Gridview.

Binding Tuples to a GridView

private List<Tuple<int, string>> GetEmpDetails()
{
return (from r in dataContext.Emp
select new Tuple<int, string>(r.ID, r.Name)).ToList();
}
I am returing a list of Tuple from WCF Service & Binding to a gridview
But when i bind it to a GridView It shows header as Item1 & Item2. Is there any way I can change the Tuple Property Names
you can't it's a tuple, this is the meaning of tuple, some values attached.
And it wasn't created for this abuse you're doing with it, you shouldn't bind a tuple to ui.
create a presentation model and bind him.

Problem with getting selected OBJECT from dropdownlist

I have a problem with getting selected objectfrom my list.
I bind collection of users to dropdownlist by:
ddlContractors.DataSource = service.GetAllUsers();
ddlContractors.DataTextField = "Name";
ddlContractors.DataValueField = "Id";
ddlContractors.DataBind();
It's working.
But when I try getting selected object by:
var user = (User)ddlContractors.SelectedItem;
I get:
(User)ddlContractors.SelectedItem Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'GWDSite.GWDService.User'
How can I get object user from dropdownlist ? I can change type of my list control if it is necessary
The value field in the dropdown list is the field "Id" not the User object. so 'SelectedItem' is returning the "Id" value -- not the object. You can use that Id to lookup the User object (from session or cache or wherever you can keep it)
You cant.
The SelectedItem is of type ListItem and you cannot just typecast a listItem back into your custom class i.e user.
You can only get the text or the value associated with the item that was selected via SelectedItem / SelectedValue
What you need to do is use that text / value and maybe retrieve your corresponding "User" object based on that text / value from somewhere (depending on how you are doing your state management).
use SelectedValue instead of SelectedItem
WEB APPLICATION
As you are working with web app , approach i was describng will not work here.
you can get the ID of the selected item and use this ID as a parameter to retrive the object from the service layer but if the object is expensive to create than use caching to store your object and retrive it wheb needed.

how to retrieve the other values in the business entity

The method has a return type of collection<businessEntity>. I am calling the method from the aspx page in order to populate a drop down -> ddlDropDown. I am binding he DataTextField of the ddlDropDown with the BusinessEntity.Name and the DataValueField with BusinessEntity.Id, the business entity contains another id which is BusinessEntity.ProductId. I need to use the ProductId of the value selected in the drop down list in the code behind. Can you help suggest how I can do so?
One possible way could be to call the method in the page_Load on the code behind and save the collection in a hidden variable and when required do a loop through in the hidden variable and retrieve the selected value Product Id.
Your thoughts please.
You could certainly accomplish this using a hidden field.
Here's another idea: bind the DataValueField to a special derived string, containing the BusinessEntity.Id AND the BusinessEntity.ProductId.
In other words, concatenate the BusinessEntity.Id and BusinessEntity.ProductId into a single string, seperated by the pipe ("|") symbol for example.
E.g to bind:
ddlFoo.DataValueField = string.Format("{0}|{1}", "Id", "ProductId");
Then to retrieve selected item:
var id = ddlFoo.SelectedValue.Split("|")[0];
var productId = ddlFoo.SelectedValue.Split("|")[1];
Saves you looping/matching. Again, its not ideal, but then again neither is binding multiple values types to a dropdownlist.

How is empty string collection any different from other empty collections, which don’t cause an exception

If ObjectDataSource.SelectMethod = A and if A() returns a single object of type O, then this object is wrapped in a one element IEnumerable collection and returned by the ODS.Select method
Thus, assuming ODS.SelectMethod points to method A(), then if A() returns a null , when called by ODS, this null is ( I assume ) wrapped into one element IEnumerable collection and no exception is thrown. Similarly, if ODS.SelectMethod points to method B(), which returns a list of objects, and if B() returns an empty collection when called by ODS, then no exception is thrown.
But if a method would instead return ( when called by ODS.Select ) an empty string collection (string[]), then I’d get exception “The data source for GridView did not have any properties or attributes from which to generate columns”. How is empty string collection any different from other empty collections, which don’t cause an exception?
Thanx
If your GridView has AutoGenerateColumns set to true, it requires a sequence of objects which have bindable members, like a DataRow. The column autogenerator inspects the signature of the sequence passed in and knows how to handle a few various cases, such as a DataRow which has a collection of columns which can be inferred into a list of columns on the GridView control. A string has no such properties. Set AutoGenerateColumns to false, and define your own column, like so:
<Columns>
<asp:TemplateField>
<ItemTemplate><%# Container.DataItem %></ItemTemplate>
</asp:TemplateField>
</Columns>
A string is pretty limited as a datasource for a GridView - unless you plan to eval properties about the string like it's Length, you can really just print the string itself (DataItem).

Resources