I have this class as you can see :
public class Document
{
public int Id { set; get; }
public string Number { set; get; }
public string Class { set; get; }
public string Discipline { set; get; }
public string Unit { set; get; }
public DateTime SubmitDateTime { set; get; }
}
It has a repository to get data from database ,in my form in UI in put a gridviewDevExpress so i pass my data using my repository to dev express gridview
private void frmDocument_Load(object sender, EventArgs e)
{
gridControlDocument.DataSource = _documentRepository.Get().ToList();
}
I want to bind my class to grid view,so i define my columns in gridview as you can see :
I want to bind my data base columns to grid view columns .I don't know where is the property ?
The result of my running is :
I have a record in database
There is a property on DevExpress gridview for columns that is called FieldName. So you can add database column name to FieldName.
You should either to create all the specific columns with the correctly assigned GridColumn.FieldName properties or do not create columns at all (in this case the GridControl will automatically populate columns itself).
For details, I suggest you use the following tutorial which describes the XtraGrid's design-time data binding specifics: Create and Manage Columns at Design Time.
Please, also read the following article that describes the data binding aspects related to columns: Creating Columns and Binding Them to Data Fields
Moreover, you can use the Data Source Configuration Wizard that allows you to choose the specific binding mode (direct, server-side, parallel or asynchronous) for various data sources (ADO, EF, SQL, Excel, WCF, XML, IEnumerable/IList and etc).
Related
There is out of the box support for exporting Kendo Grid data in Xlsx and Pdf formats, I'm looking for something similar for exporting in Csv format.
I have tried the server export described in: https://demos.telerik.com/aspnet-mvc/grid/server-export, which just sends the visible rows to the action; My unfiltered grid has over 250k entries. The output I'm looking for is the data in this table with the filters applied. I can get the data again in the same way the grid was populated and filter the data server-side, but how do we pass the selected filters to the action?
There appears to be a number of possible solutions, but I'm hoping to save time on going down a dead end. The documentation is often good with Kendo, but I'm struggling to find what I need with this one.
A possible solution is to get the filters applied on the Grid and send it to the server side and export the result as a expected format. To get the filter applied on the grid, you have to act over the dataSource.
var grid = $("#myGrid").data("kendoGrid");
var filter = grid.dataSource.filter();
Then, you have to send via jquery ajax, the filter.filters array to an action and bind it into a Type, maybe, something like this:
public class GridData
{
public GridFilter[] Filters { get; set; }
public string Logic { get; set; }
}
public class GridFilter
{
public string Operator { get; set; }
public string Field { get; set; }
public string Value { get; set; }
}
Then, get the data based on the GridData object and return provide the CSV information.
I'm new in MVC. I saw the most of article about dynamic form is just for client side interface like add form group dynamically not the new object.
If it is possible, how about the Model?
Can we make model has dynamic property based field on the database that has created/added dynamically and generate the View form layout from the controller for CRUD Operation?
namespace Web1.Models
{
public class Class1
{
public string field1 { get; set; }
public int field2 { get; set; }
// Create dynamic property based database field that has been added
}
}
Thanks in Advance
Suppose I have a model with 20 fields, and in my index page, I want to list all models that are stored in my database.
In index page, instead of listing all fields of the model, I only to list 3 fields.
So, I make two class:
class CompleteModel {
public int Id { get; set; }
public string Field01 { get; set; }
public string Field02 { get; set; }
public string Field03 { get; set; }
public string Field04 { get; set; }
public string Field05 { get; set; }
...
public string Field20 { get; set; }
}
now, in my Controller, I can use:
await _context.CompleteModel.ToListAsync();
but I feel that it does not seem to be the right way to do it, because I'm getting all fields and using only 3 fields.
So, I made this code:
class ViewModel {
public string Field02 { get; set; }
public string Field04 { get; set; }
public string Field08 { get; set; }
}
var result = _context.CompleteModel.Select(
x => new {
x.Field02,
x.Field04,
x.Field08
}).ToListAsync();
var listResults = new List<IndexViewModel>();
if (result != null)
{
listResults.AddRange(results.Select(x => new IndexViewModel
{
Field02 = x.Field02,
Field04 = x.Field04,
Field08 = x.Field08
}));
}
I think this is a lot of code to do this.
First, I selected all the fields that I want, then, copied everything to another object.
There's a "more directly" way to do the same thing?
Like:
_context.CompleteModel.Select(x => new IndexViewModel { Field02, Field04, Field08 });
You could use AutoMapper to reduce the boiler plate so you're not manually copying field values over.
If you include the AutoMapper NuGet package then you'd need to have the following in your startup somewhere to configure it for your classes:
Mapper.Initialize(cfg => cfg.CreateMap<CompleteModel, ViewModel>());
You could then do something like the following:
var results = await _context.CompleteModel.ToListAsync();
var viewModelResults = results.Select(Mapper.Map<ViewModel>).ToList();
There are a lot of configuration options for the package so do take a look at the documentation to see if it suits your needs and determine the best way to use it if it does.
In my view this is one of the weaknesses of over abstraction and layering. The VM contains the data that is valuable to your application within the context of use (screen, process etc). The data model contains all the data that could be stored that might be relevant. At some point you need to match the two.
Use EF Projection to fetch only the data you need from the database into projected data model classes (using the EF POCO layer to define the query, but not to store the resultant data).
Map the projected classes onto your VM, if there is a naieve mapping, using Automapper or similar. However unless you are just writing CRUD screens a simple field by field mapping is of little value; the data you fetch from your data store via EF is in its raw, probably relational form. The data required by your VM is probably not going to fit that form very neatly (again, unless you are doing a simple CRUD form), so you are going to need to add some value by coding the relationship between the data store and the View Model.
I think concentrating on the count of lines of code would lead to the wrong approach. I think you can look at that code and ask "is it adding any value". If you can delegate the task to Automapper, then great; but your VM isn't really pulling its weight other than adding some validation annotation if you can consistently delegate the task of data model to VM data copying.
I'm working to generate HTML Table Rows in a TagHelper class to be added to a DataTables table that displays a few buttons in a hidden child row (shown on clicking a given row). For one of these buttons, I need the value from a property within a different class (corresponding to a value in a separate database table). I'm not sure how to go about referencing the other class so that it has the correct value for the given survey. The class layout is something like this:
Relevant Snippet from the TagHelper class I'm working in:
public IEnumerable<Survey> listSurv { get; set; }
foreach(Survey surv in listSurv) {
//Row Construction Happening Before the Button
//I need to reference the Report class here to get the ReportID (Id in the Report class).
//Currently, I have this, but it always returns 0
Report r = new Report();
innerData.InnerHtml.Append(r.Id.ToString()); //innerData is a TagBuilder element that's previously constructed
}
The Report Class:
public class Report()
{
public int Id {get; set;}
public virtual Survey Survey {get; set;}
public string DLLink {get; set;}
public DateTime Date_Uploaded {get; set;}
}
Is there some way I can Instantiate the Report class within the TagHelper class in a way that lets me access the Id corresponding with that particular survey? My initial thought was a paremeterized constructor, but I'm not sure how to go about Implementing that in this particular case. I'm more of a PHP Developer, so I'm not as familiar with how this would work in a ASP.NET setting.
You need a link back from Survey to Report. Without Survey code, that one is hard to tell. What you can do is save the report as a reference in the survey, and call that from your code.
What you did now is creating a new Report, such that is has always a default id of zero.
So create a report reference in survey:
public class Survey {
public Report Report { get; set; }
}
Link the survey instance with a reference to the report instance:
var report = GetReport();
var survey = GetSurvey();
survey.Report = report;
Now you can retrieve report data from the report property:
foreach(Survey survey in listSurv) {
innerData.InnerHtml.Append(survey.Report.Id.ToString());
}
I want to display data in one control Vertically with a window form control in C#, such as:
Title: The Game
Name: Andrew Smith
Age: 44
ISBN: 123456123456
Costs: £39.99
I'm looking for a simple display with code to add and remove items..
I have a list of data which I want to display quite simply from a list which will be displayed as Title/Item as above, which won't need to be amended only viewed.
Is there a way to find out the dimensions of the text and create padding, or is there an automatically adjusting multiple column type control? Any thing with some code to stop me going mad regarding special list Views and grid type views.
You can use DataGridView. It is simple to use.
You mentioned List Views in your post, and I think that is the answer for you. Try a ListView control with View set to Details. Use the Designer to create a column for each field in your data, and then use listView1.Items.Add() to add each item. (There is a constructor for ListViewItem which takes an array of strings; the strings are the values for each column.)
Create a BookItem class which has properties shown below.
public string Title { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int ISBN { get; set; }
public string Cost { get; set; }
Generate a lstBookItems item List<> type and add BookItem items. But first, you must create bookItemInstance and fill data one by one.
List<BookItem> lstBookItems = new List<BookItem>();
lstBookItems.Add(bookItemInstance);
After this point, if you using ASP.Net
Create a Asp:ListView or Asp:GridView using Toolbox and give data source and bind data on code behind PageLoad() method
grdBooks.DataSource = lstBookItems;
grdBooks.DataBind();
And now, you can change appearence this gridView as you wish at Design screen(ex: vertical).