Reputation: 21
I am working on a project in C# using Entity Framework. Using EF, I connect to database that has a dozen of tables, and none of the views. What I want to accomplish is create object in code that would represent sql view (i.e. three DB tables joined for presenting useful data). I need this so I could create binding data source for the datagirdview. Right now I manually wrote SQL query for joining three tables and creating a list which then I define as datagridview.DataSource
This troubles me because of hard design about presenting the data in the datagridview. I have to manually write everything in code, and also not sure how to accomplish everything I need.
I would be more satisfied if I could create new class that would represent above and then in Designer View add Data source as object and view it in Designer mode and edit columns right there.
My query which does the job is as follows:
using (var context = new csModelEntitites())
{
var listaVozila = (from voz in context.Vozilo
join var in context.Varijanta on voz.VarijantaID equals var.ID
join mod in context.Model on var.ModelID equals mod.ID
join mar in context.Marka on mod.MarkaID equals mar.ID
select new
{
voz.ID,
voz.VIN,
Vozilo = mar.Naziv + " " + mod.Naziv + " " + var.Motor,
voz.GodProizvodnje,
voz.RegOznaka,
voz.RegDo
}).ToList();
VozilaPrikaz.DataSource = null;
VozilaPrikaz.DataSource = listaVozila;
foreach (DataGridViewColumn c in VozilaPrikaz.Columns)
{
if (c.HeaderText == "GodProizvodnje")
c.HeaderText = "Godina proizvodnje";
if (c.HeaderText == "RegOznaka")
c.HeaderText = "Registarska oznaka";
if (c.HeaderText == "RegDo")
c.HeaderText = "Registriran do";
I want to create class that would do the similar. Any help appreciated.
Upvotes: 0
Views: 215
Reputation:
You can bind a user defined custom class to a datagridview and edit the column headers in the designer-view. The steps would be.
First create the class whose public properties you would want as the datagridview column name. For example I created the class Class1.cs in my project folder. Later on , you can edit the column header or other related property in the designer.
namespace WindowsFormsApp2
{
public class Class1
{
public string x1 { get; set; }
public string x2 { get; set; }
public Class1(string x1, string x2)
{
this.x1 = x1;
this.x2 = x2;
}
}
}
Next in the designer view, pull an empty DataGridView from the ToolBox. Right click on the DataGridView->Properties->DataSource->Add Project DataSource ->
Then in the dialog box, choose "Object" as "Data Source Type". Then under "Select the data Objects" if you expand the tree , you would see your class. In my case, I select "Class1" , the same class which I had earlier created.
Then using edit columns, you can change any header text or so. In the code file, the data needs to be filled in as below:
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
private BindingSource bindingSource1 = new BindingSource();
public Form1()
{
InitializeComponent();
bindingSource1.Add(new Class1("gdc1", "gdc2"));
dataGridView1.DataSource = bindingSource1;
}
}
}
That's the . The datagridView is all set.
Upvotes: 1