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.
Related
I have a TableView that consists of a single row, which is implemented using an object with relevant String and Float properties. To make the object observable to the TableView, this single object is put into a list thus:
private ObservableList<MyObject> myObjectList = FXCollections.observableArrayList();
MyObject myObject= new MyObject ("myStringLiteral", myFloat, anotherParameter, etc);
myObjectList.add(myObject); //a single-element list
I swap objects in and out of this list, but there is only ever one element in the list at any one time. The corresponding TableView only ever has one row.
This seems inefficient to implement it using an observable list. Can a tableview observe an object directly, without having to put it in a list first?
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
For a data bound control it is common scenario where we provide data text field and data value field ( in simple controls like Dropdownlist) but more fields in controls like Gridview. Generally the datasource is of type IEnumerable.
How does the control internally process these values or rather how do they get the value from the data source without knowing what kind of datasource they are dealing with.
Can someone explain with code how the controls evaluate these fields from the data source.
Typically, the data-bound control (or concerned components such as
DataControlField in GridView) will handle DataBinding event.
Within event handler, the data item that is being currently bound (e.g. DataRowView or entity instance) is retrieved. This is done via DataBinder.GetDataItem passing the actual control or control's NamingContainer. For example, if you are implementing a lower level control such as DataControlField for higher level data-bound control such as GridView then it would handle data-binding of a cell control and hence it will use cell's naming container to pass to DataBinder.GetDataItem method which uses current data binding context to get the same.
Once the data item object is retrieved, one need to evaluate the given data-binding expression against it to get the actual value and apply any formatting as per different properties set to the control/component. The most simple way is to use DataBinder.Eval overload. However, one may use the more efficient ways - for example, say DataField string is going to be only property name then you may look and cache the property descriptor and then use the same against different data items.
I will suggest you to use tool such as Reflector to inspect relevant control's code to get the better idea.
I never knew i could find this information so easily and LLyod was in fact wrong on using reflection to find data from a datasource. None of the data controls use it when i inspected through Reflector ;(
link that solved the problem
http://msdn.microsoft.com/en-us/library/ms366540.aspx
how you do it is below
protected override void PerformDataBinding(IEnumerable retrievedData)
{
base.PerformDataBinding(retrievedData);
// Verify data exists.
if (retrievedData != null)
{
string dataStr = String.Empty;
foreach (object dataItem in retrievedData)
{
if (DataTextField.Length > 0)
{
dataStr = DataBinder.GetPropertyValue(dataItem,
DataTextField, null);
}
else
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(dataItem);
if (props.Count >= 1)
{
if (null != props[0].GetValue(dataItem))
{
dataStr = props[0].GetValue(dataItem).ToString();
}
}
}
}
}
}
If the above code seem Greek and Latin , you will have to have a course on asp.net controls development to understand what is being done.
I am developing a web application where in i have a WCF service which interacts with the database using entity framework. I want to get rid of creating Classes for each & every LINQ query
e.g
public class Emp
{
public int CD{get;set;}
public string Name{get;set;}
}
public List<Emp> GetServTypeForPromotionDue()
{
return (from a in Context.TableName
select new Emp{ a.CD, a.NAME });
}
for other table & LINQ i have to create a separate class every time. Alternative to this is to use Anonymous method which is not preferable solution. To avoid both the methods I am using Tuple Class where I return List> or List> depending on the return type. This works fine but the problem is I am binding the result of LINQ query directly to a Gridview By default Tuple has properties item1,item2,..& so on. So my griview shows these names are column names so, Is there any way I can change the property name to CD, Name instead of Tuple's Item1, Item2 before binding to grid?
when binding the list, you could use linq:
this.grid.DataSource = tupleList.Select(i => new
{
FirstName = i.Item1,
LastName = i.Item2,
CD = i.Item3
});
You would have to change the column names on the GridView and not the tuple. A tuple is a lightweight type that doesn't support much customization (MSDN). If you know what you are binding to the GridView, change the column names by handling the RowDataBound event and checking the RowType of Header, then changing the column names there.
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.