Binding LinqDataSource from code-behind to Gridview - asp.net

i have a grdidview control on the .aspx page and i am trying to connect dynamically from code behind and bind the gridview but somehow it throwing me an error... what is wrong with this code? any help?
LinqDataSource LDS_POReport = new LinqDataSource();
LDS_POReport.ContextTypeName = "DataContextDataContext";
LDS_POReport.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSourcePO_Selecting);
this.gvReport.DataSource = "LDS_POReport";
//this.gvReport.DataBind();
Update:
after i update the code to
this.gvReport.DataSource = LDS_POReport;
it works fine but when i try to sort i get this error:
The GridView 'gvReport' fired event Sorting
which wasn't handled.
i added this but no effect.
LDS_POReport.AutoPage = true;
LDS_POReport.AutoSort = true;

I guess that your problem is here:
this.gvReport.DataSource = "LDS_POReport";
The above code line attempts to assign a string to a property that expects some sort of data source. I assume that you really intended to assign the LinqDataSource object itself:
this.gvReport.DataSource = LDS_POReport;

First thing, the DataSource should get a reference to the object containing the data, not the name of the object containing the data. GridViews can work reflectively, but not THAT reflectively.

Related

ASP.NET DropDownList OnItemDataBound

Is there a method on a <asp:DropDownList> that is equivalent to the OnItemDataBound on Repeaters?
The reason is I want to check each value before putting it in the drop down list.
Nothing similar to OnItemDataBound which will give you an item context.There are events like OnDataBound which will get triggered when it reaches binding.
Your reason for asking this event is for enriching the row instead to filterout, which would not have helped you even with this event.
The best option to solve your question
The reason is I want to check each value before putting it in the drop
down list.
is to do some thing like the following pseudo code
this.YourDropDownList.Items.Clear();
var lst = new List<ListItem>();
var yourCollection= <YOURCollection after Apply Linq Conditions check>
foreach (var obj in yourCollection)
{ //loop and add
this.YourDropDownList.Items.Add(new ListItem {Text = obj.Prop1, Value = obj.prop2});
}

In Google AppMaker, How to filter using a single DateBox?

I am trying to filter a table using a DateBox. The problem I have is that it doesn't display the records of that day when binding value is set to #datasource.query.filters.date._equals. However, it does work when the filter is _greaterThanOrEquals, but it also includes later records.
I am using SQL tables with date field type is DATE.
It's a bug. We're looking into it.
For now please use workaround:
Remove the binding and set 2 filters in onValueEdit event with code like:
widget.datasource.query.filters.FIELD_NAME._greaterThanOrEquals = newValue;
widget.datasource.query.filters.FIELD_NAME._lessThanOrEquals = newValue ? new Date(newValue.getTime() + 24*60*60*1000) : null;
widget.datasource.load();
bind DateBox value to
#datasource.query.filters.NameOfDateFiled._equals
and don't forget reload datasource in "onValueChange" event.

Unable to bind list to gridview after sorted using linq ASP.NET

I am sorting a list of object and binding the sorted list to a gridview that supports paging.
I tried:
var selectedNew = selected.AsQueryable<Customer>().OrderBy(sortExpression);
selectedNew.ToList<Customer>();
gdvEmployees.DataSource = selectedNew;
gdvEmployees.DataBind();
I am getting the following error:
The data source does not support server-side data paging
What is causing the above error to be thrown?
ToList produces an output, you cannot use it by itself. try this:
var selectedNew = selected.AsQueryable<Customer>().OrderBy(sortExpression);
var selectedNewList = selectedNew.ToList<Customer>();
gdvEmployees.DataSource = selectedNewList;
gdvEmployees.DataBind();

set label text to total row count of gridview

I'm using a stored procedure in a sql database as the data source for a SqlDataSourceControl on my .aspx page. I'm then using the SqlDataSourceControl as the data source for a gridview on my page. Paging is set to true on the gridview. What i would like to do is set the text of a label to the total number of rows in the gridview. I can use this code
'labelRowCount.Text = GridView2.Rows.Count & " layers found"
to return the number of results per page, but it doesn't give me the total. I've looked in several places and haven't been successful in finding a solution.
You should use the underlying datasource that the gridview is bound to (grid.DataSource). For instance if you have bound the grid to a datatable then just cast the grids datasource into the datatable and the use the rows.count property to get the total record count. Another alternative would be to get a reference to the the grids datasource object before you set it to the grid so you can get the record count directly.
So for example (assuming you are bound to a DataTable)
int count = ((DataTable)grid.DataSource).Rows.Count;
Enjoy!
Put an event handler on "selected" for the SQL DataSource. That event handler has an argument of type SqlDataSourceStatusEventArgs. In there AffectedRows is the row count of the entire data set (not just that shown on the current page). So catch that and write it out to your label:
protected void SqlDataSource_Selected(object sender,SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
// do something useful, then...
e.ExceptionHandled = true;
}
else
labelRowCount.Text = String.Format("{0} layers found", e.AffectedRows);
}
GridView2.Rows saves only the rows that are visible, so when page-size is 5 you get only 5 records. As Doug suggested you have to set the labelRowCount.Text ondatabound and not on every postback, because on postback - when the datasource is not binded again - the datasource will be nothing. So a good place could be where you bind the grid to the datasource.

ASP.NET EnqityDataSource WhereParameters, creates new property

I am trying to populate GridView, using EntityDataSource(code behind), I need to able to sort GridView. However when I sort i get error:
A property with name 'aspnet_Users.UserId1' does not exist in metadata for entity type
So I beleive it is because I generate where parameter in code behind:
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Full code is :
ActiveEnqDataSource.ConnectionString = db.Connection.ConnectionString;
ActiveEnqDataSource.DefaultContainerName = "Entities";
ActiveEnqDataSource.EntitySetName = "Enquiries";
ActiveEnqDataSource.Include = "UserCars.CarModel.CarMake, Category, aspnet_Users";
ActiveEnqDataSource.EnableUpdate = true;
ActiveEnqDataSource.EnableInsert = true;
ActiveEnqDataSource.EnableDelete = true;
ActiveEnqDataSource.AutoGenerateWhereClause = true;
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Any suggestions? Thank you very much! The gridview itself renders perfectly, only thing I cannot sort it, any "whereParameters" I add, Add 1 to the property e.g UserId1,EnquiryStatus1, ProdauctName1. etc...
I got a similar error because I was adding the where clause each time the page was posted back. Dropping my code that generated the where clause inside an IsPostback statement fixed the problem:
if (!IsPostBack) {
// code to add where parameters
}
I got the same error when I used markup to define a Where parameter AND then I added the same parameter in code. Somewhere along the line, the 1 at the end of the parameter name was added.

Resources