Fill Kendo UI Grid - user-interface

DataSourceResult does not contain a definition for ToDataSourceResult and the best extension method overload QueryableExtensions.ToDataSourceResult(DataTable, DataSourceRequest) requires a receiver of type DataTable
var results = db.Accounts.ToDataSourceResult(
request,
o => new {
ID = o.ID,
AccCode = o.AccCode,
AccName = o.AccName,
Phone = o.Phone,
Email = o.Email,
DateBirth = o.DateBirth,
Address = o.Address,
Type = o.Type
}
);
return Json(results.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

Your results var is already a DataSourceResult so you don't have to do it again in the Json().

Related

How to call Expression Func with two input parameters

I have following Expression Func which is receiving two input paramters, first is Person Object, second is bool and returning another type of Object PersonProfile
private Exression<Func<Person, bool, PersonProfile>> PersonProfileProjection => (person, isValid) =>
new PersonProfile
{
FirstName = person.FirstName,
HasAddress = isValid ? person.Address1 : null
};
And I am trying to call this while fetching Person table from dbContext.
_dbContext.Persons.Select(PersonProfileProjection);
I am confused how to send boolean parameter inside PersonProfileProjection. It works when I only put one input and one output parameter like this. But I want extra boolean input as well.
Any help would be highly appreciated.
You can follow Microsoft documentation for this : Expression Class
One sample created for SQLite that show above function usage.
public void GetData()
{
var connection = new SQLiteConnection(#"Data Source=database.sqlite;Version=3;");
var context = new DataContext(connection);
connection.Open();
var createtableQuery = #"
drop table Company;
CREATE TABLE[Company]
(
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
[Seats] INTEGER NOT NULL
);
";
var command = new SQLiteCommand(createtableQuery, connection);
command.ExecuteNonQuery();
Company com = new Company()
{
Id = 6,
Seats = 7
};
context.GetTable<Company>().InsertOnSubmit(com);
context.SubmitChanges();
var companies = context.GetTable<Company>();
foreach (var company in companies)
{
Console.WriteLine("Company: {0} {1}",
company.Id, company.Seats);
}
//compile Expression using Compile method to invoke it as Delegate
Func<int,int, Company> PersonProfileProjectionComp = PersonProfileProjection.Compile();
var dd = companies.Select(p => PersonProfileProjectionComp(p.Id,p.Seats));
//// Below line inline use. Both works.
//var dd = companies.Select(p => PersonProfileProjection.Compile().Invoke(p.Id,p.Seats));
}
private System.Linq.Expressions.Expression<Func<int, int, Company>> PersonProfileProjection => (person, seats) =>
new Company
{
Id = person,
Seats = seats
};
or in one line use this :
PersonProfileProjection.Compile().Invoke(person, isValid)
You could declare it as a Func instead of an expression:
private Func<Person, bool, PersonProfile> PersonProfileProjection => (person, isValid) =>
new PersonProfile
{
FirstName = person.FirstName,
HasAddress = isValid // do what you meant to do
};
... and call it as:
_dbContext.Persons.Select(p => PersonProfileProjection(p, true));
You could as well write an ordinary method:
private PersonProfile PersonProfileProjection(Person person, bool isValid)
{
return new PersonProfile
{
FirstName = person.FirstName,
HasAddress = isValid // do what you want to do
};
}
...and call it the same way:
_dbContext.Persons.Select(p => PersonProfileProjection(p, true));

Bind List to GridviewComboboxcolumn in telerik radgrindview (winform)

I have a generic List like
List<return> returnlist
class return
{
public string returnid {get; set;
...
public List<string> Vouchernumbers
}
I bind the returnlist to the telerik radgridview.
How can i bind the voucherlist to the GridviewComboboxcolumn for each row ?
I have bind the voucherlist to the combobox after radgridview_complete_binding.
You need to create the grid with columns and data
You need to add the combobox column, initialize it.. Please check you need to have a dataeditor in here
Assign the string to datasource
comboColumn.DataSource = new String[] { "Test1", "Test2"};
You can bind the collections too:
Binding list BindingList<ComboBoxDataSourceObject> list = new BindingList<ComboBoxDataSourceObject>();
ComboBoxDataSourceObject object1 = new ComboBoxDataSourceObject();
object1.Id = 1;
object1.MyString = "Test 1";
list.Add(object1);
ComboBoxDataSourceObject object2 = new ComboBoxDataSourceObject();
object2.Id = 2;
object2.MyString = "Test 2";
list.Add(object2);
colboCol2.DataSource = list;
radGridView1.Columns.Add(colboCol2);
create radcombobox and set datasource and add it to rad grid
eg :
GridViewComboBoxColumn col = new GridViewComboBoxColumn();
col.DataSource = DAL.ActiveDb.GetList<SalesRep>().ToList().OrderBy(x => x.RepName).Select(x => new { Id = x.Id, x.RepName });
col.DropDownStyle = RadDropDownStyle.DropDown;
col.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
col.DisplayMember = "RepName";
col.ValueMember = "Id";
col.FieldName = "RepId";
col.HeaderText = "Rep Name";
col.Width = 200;
//var t = gridColInfo.Where(x => x.ColumnName.ToLower() == "repid").FirstOrDefault();
//if (t != null)
//{
// col.Width = t.ColumnWidth;
//}
this.radGridBillwiseOpening.Columns.Add(col);

list inside another list select

what is the correct syntax to write the second list? bookid and other fields are not recognizing
var bookssublist = from bookdetails in bookslist
join bookcategories in _context.BookCategories
on bookdetails.BookId equals bookcategories.BookId
where bookcategories.CategoryId==CategoryId
select new BookBasicInfo {
count = bookcount,
BookInfo = new List<BookInfo>()
{
BookId = bookdetails.BookId,
BookTitle = bookdetails.Title,
Images = bookdetails.ThumbnailImagePath,
PublishDate = bookdetails.PublishedDate,
AuthorList = bookdetails.BookAuthors.Select(q => q.Author.Author1).ToList(),
CategoryList =bookdetails.BookCategories.Select(q=>q.Category.CategoryName).ToList(),
}
};
You are using collection initializer in a wrong way. Actually, you forgot to pass an object of type BookInfo to the initializer.
BookInfo = new List<BookInfo>()
{
new BookInfo()
{
BookId = bookdetails.BookId,
BookTitle = bookdetails.Title,
Images = bookdetails.ThumbnailImagePath,
PublishDate = bookdetails.PublishedDate,
AuthorList = bookdetails.BookAuthors.Select(q => q.Author.Author1).ToList(),
CategoryList =bookdetails.BookCategories.Select(q=>q.Category.CategoryName).ToList()
}
}

Devexpress Gridview Hide Editform in asp.net mvc

If my CustomerName FieldName equals to "MyCustomer" , i want to hide editing for that row.
If customername == "MyCustomer , hide column editing.
How can i hide column editing according to "MyCustomer"?
settings.Columns.Add(s =>
{
s.FieldName = "CustomerName";
s.Caption = "Customer";
s.Name = "CustomerColumn";
s.ColumnType = MVCxGridViewColumnType.ComboBox;
var comboBoxProperties = s.PropertiesEdit as ComboBoxProperties;
comboBoxProperties.DataSource =Model.CustomerList;
comboBoxProperties.TextField = "Customer_Name";
comboBoxProperties.ValueField = "Customer_Id";
comboBoxProperties.ValueType = typeof(int);
comboBoxProperties.ClientInstanceName = "CustomerColumn";
});
Any help will be appreaciated with points.
settings.CommandButtonInitialize = (s, e) => {
if (e.ButtonType == ColumnCommandButtonType.Edit) {
MVCxGridView g = s as MVCxGridView;
var value = (int)g.GetRowValues(e.VisibleIndex, "RowFieldName"); //use a correct field name and cast a resultant value to a correct value type
e.Visible = value > 10; // for example, only
}
};
Fortunately , i have myself.I found a solution.
It works.Hope this helps who has the same problem in the future.

Select top N with record from DataTable with some sorting using Linq

I have created a DataTable. I have populated the rows of the DataTable after some operations.
I am very new to Linq I want to Get the Top "N" Records from the DataTable implementing also some paging.
Let dataTable is the DataTable having some data.
I am need something like this
var Query = from d in dataTable
Order by columnName
skip( some records pageSize * pageNumber)
Select top N from dataTable
The column Name, Page size ,pageNumber and the N will passed as arguments
Try this:
var query = dataTable.AsEnumerable()
.OrderBy(c => c.columnName)
.Select(r => new {...})
.Skip(10)
.Take(5)
Try this,
int numberOfObjectsPerPage = 20;
var queryResultPage = dataTable.OrderBy(c => c.columnName).Select(r => r).Skip(numberOfObjectsPerPage * pageNumber).Take(numberOfObjectsPerPage);
Try this
var Query = dataTable.Select(o=>o).OrderBy(o=>o.columnName).Skip(pageSize * pageNumber).Take(N);
EDIT
For pass column name you should to add this code
public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string SortField, bool Ascending)
{
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = Ascending ? "OrderBy" : "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
And then you could call it in this way
var values = dataTable.OrderByField("columnName");

Resources