In this video we're going to start designing a screen to help us manage our vehicle listings so lets
us just start our application and refresh ourselves with what we've done so far.
And we have many listing here that allows us to add remove edit and view listings.
So I'm actually just going to do all of that from.
Well one window.
And you know.
Case by case you probably design something else.
So I'm going to modify this entire listing or this entire menu strip and just have mileage vehicle listing
someone to remove all of the child options of that menu and I can just click and delete.
So when I click that one I did select that I could just press delete and it will actually just remove
that menu item.
So that's so you can go about removing them.
Right.
So I want one that says manage vehicle listing now when I click it I'm going to launch a new form that's
a mean to see all the vehicles in the database and then perform the crowd activities from there.
So I'm going to do everything from one window.
So firstly I want to double click and do the Click event on that button.
So there are a number of activities that need to be done.
And I agree it takes and they don't need to be done in any particular order.
So I'm creating the Click event but I'm not going to put anything in the click event because well the
ultimate reason for that given would be to launch the window and I haven't yet created that farm right.
So yes I have the tick given what I need to create the form.
So in no particular order I have stuff to do I'm just saying that to say that you don't do one thing
that the of a strict order of or series of activities that have to be performed in a particular order.
Right so of the click event already we can come up to that but I'm went to regulate my project go to
add say new item.
And once again I'm creating a blank form and then this time I'm going to call it manage vehicle listing.
All right.
So my form is manage vehicle listing I can spend some time doing some cosmetic changes where I'm going
to meet the text in the title bar not look.
So you know programming will look like.
All right.
So manage vehicle listing and then I'm going to start looking at some new controls.
And the first new control would be the grid view control.
So in my tool box I'm going to go into the subcategory data and then I'd want to see one that is data
grid view so data grid view displays rows and columns of data in a grid that you can customize.
All right.
So that is what we're going to drive over.
So I drove over that data grid view and you notice that it's just this big green box.
So I'm just going to expand this grid box to a certain extent.
If I use that current I get by that menu and I can edit columns and columns you know enable it edit
delete and add addition functions to the grid if I want I can also dock in the containers if I'm not
going to Gunton and fills the entire container right.
That's not necessarily what I want because I'm going to put the entire label at the top to say manage
vehicle this.
And I'm also going to put in some other things in here.
So you know maybe it starts off with one idea of what you want to do and then you realize that oh but
you don't have to do it that way you could do it another way.
Sometimes that happens so let's start off by not having it docked and then going to bring over a label.
So I have a label in add our entire record already.
So instead of going and trying to design a brand new label I'm just going to copy this going back over
to my new form paste it make sure I position it and I'm going to change the text I go to properties
change it takes and this one is manage vehicle listing all right.
So this reposition it accordingly.
All right so our new window for managing the vehicle listing no most important the most important parts
of this is our grid view saw a grid view will give us like a tabular representation of whatever data
we tell it that we want to see.
Right.
So you can think of Excel or even in the database when we did a select star from a table that we saw.
We notice that we get that tabular representation.
That's exactly what that data grid view of those us to do.
So the first thing that we do with these what I call them more essential controls because a label is
essential but not that much.
But I want to know the name of my grid view right.
So I go to properties of the thinking grid view and you can take some time and look at the properties
and realize that these properties whatever control their different properties and of course each control
is unique in its own right.
But the one the one common property is going to be name.
And I don't want my data grid view to because it's a grid view someone to call this dv you know are
GV grid view.
Right.
And this is car these star vehicles which are row on so remember my prefix and then the name of whatever
it is that this control is for.
So a grid view for vehicle lists.
Then I'm going to set up this farm that when it loads I run a query for the vehicles and I put that
data are the data that's going back from that query inside of this grid view.
Right.
So remember we did something similar to that when we were looking at launching our car or was it our
ad or into the record form when we wanted to make sure that our combo box had all the data it needed
from the listing of cars.
Right.
So in this case you want the grid view to have that same data when the form loads.
So since I wanted to happen when the form loads the when this happens that is the events that I want.
So I'm going to click on the form go over to properties the lightning bolt and I'm seeing that when
the form loads I want this particular thing to happen.
So I'm just going to double click inside the load section and know it's saying okay this is the farm
name on the score load.
So when the farm loads What do I want to happen.
So the next thing I want to do of course is establish a database connection and then I can start earning
my query.
So if you forgot how that works you can go back to the order into the record code file where what we
did was declare an object that represented our connection to the database we initialize it inside of
for constructor and then wherever we needed to access the database we simply called on that object right.
So we're going to follow that same pattern.
So in our new form the first thing I want to do is in the class I'm defining our private and I'm making
it read only that's muscle memory because it doesn't it that's always happens.
So you know you can do it with all the read only.
That's fine but I wanted to be off type car or into entities though if you forgot where this name came
from.
Then in the e d m x file that you would have created for this project.
That's a car into a DB the idea makes in my case but if you have a different name it must be dot e DMX.
You can double click that's use C or D It's a model but when you go to properties you will see entity
country in your name and that is the class type that represents the connection to your database.
All right so if you have forget the name or you are doing one from scratch you just looking at entity
country in your name and whatever name is there that is your data type.
All right.
So back in our new code file for our new form we say a private read only date.
Sorry.
Car Into entities which is our entity contain a name and that I'm going to give it the object name so
visual studio is always making suggestions and that's really cool.
I took one of the additions in the previous form where I said add car and let me find that code quickly.
So in the previous one where I access the database I use the resource through those suggestions.
And that's fine but a lot of the times on the internet you probably see something like context being
used or underscored D.B. The fact is the name of the object is not it's not it's inconsequential to
the functionality.
All right.
So I can name my object anything.
So in this situation I'm going to name it underscored DB.
That being said over I do promote consistency so if you started on one path continued on that path.
If you want to change the path change a path right through your code so don't have underscored D.B.
in this form.
And then car into a record or into entities in another form and then another form a field another way
and you call its contact star X..
Don't don't do that doesn't have one consistent naming convention.
I'm doing it this way just to show you that whatever you call the object is inconsequential.
All right.
Someone to use D.B. in this situation however later on I will go through and normalize everything to
underscore D.B..
All right.
So private read only car into entities the B and then inside of my constructor on an I underneath initialized
component I'm going to see on the score d d equal to a new instance of car into entities.
So that's the initialization right.
So in our program runs are when this form is launched it knows all of the DV.
Then initialize is its components and the DB.
And then throughout the rest of the form any time we need to talk to our database we have or underscore
D.B. object.
So the first thing I want to do is run the query to get the car words to say var cars and I use cars
and vehicles interchangeably so whichever one you want to use that's fine but var cars is equal to database
connection Dot and I don't want the car into a record.
So I kind of scroll until I see the table that looks like what I want which is types of cars.
And then I can see a dot to list.
So I want the list or at least of the types of cars in my database and then I'm storing all of those
inside of a variable called cars.
All right.
And then the next step would be to let the data grid know that each should be displaying whatever is
going back instead of the list of cars.
So I can see GV.
All right.
So because I renamed it I know it's a grid view so I call it GV vehicle list dot data source is equal
to cars.
Right.
So the list of cars going back from the database will serve as the data source for my data grid.
So let us see how that looks.
I click start and then when the window comes up I'm clicking and nothing is happening otherwise nothing
happening and we see that we still have a street menu item but nothing is happening and that's because
I did not finish up my click event so I finished wiring up the form or at least for now I've wired the
form so I'm going to go back over to my main window design or to the code file or rather and then here
is that click events I have to go to design because I missed a menu item so I need to delete this one.
All right good.
So now that we've cleaned up the interface back in our code file what we need to do is let the button
launch the new form.
So I'm just going to say var and this one is going to be vehicle listing is equal to new and I call
it vehicle mileage vehicle listing.
All right.
And then I have to say that it's MDI appearance.
Is this so vehicle listing that Amber's a parent is equal to this which is mean window.
And then finally after all of that out of the way I want to show.
All right so let's try.
That's getting someone to launch our application.
And then when I click Vintage Vehicle listing now we're seeing our listing and look at this no.
So we're seeing the grid view and I mean it looks somewhat anemic or we see the big green box.
We see the grid filling out it does much of the box as possible.
We see that we're getting about the I.D. and the name and this seemingly random column from the database
called car into records.
But the fact is that we are seeing the car rental records in that grid views and all we can go back
to this window and maybe clean it up a bit.
Someone's a get out of this.
Someone to modify the width of this grid view because I really don't need to needed to fill that much
space with two columns.
So I'm seeing Aidid name and cover car rental records.
How do I explain what that seemingly random column is.
But I want to add the button so I did take it's the ability to edit to add and remove the cars from
the menu listing in the main window so what I'm going to do is put buttons inside of this window that
will be responsible for helping us do those actions right.
So I went to have four buttons or three buttons so I made one button that I'm just copying and pasting
and I have a spare one.
All right.
And then for my buttons for the properties one I want to take to see what I wanted to do so but no one
let's say we want to meet this add new car right.
The button three.
I want this and other spin properties so that I don't have to keep on going in and going Oh it's so
button three.
I want this one to be edited.
Car and then this one would be delete car.
And then another important step to having my buttons is that I don't want them to be beaten and button
one button to button three I want them to have sensible names that I can reference them in the code
behind.
Right.
So but no one wants renamed to be GM.
That's my prefix for a button car.
All right.
Edit car is going to be Beatty and edit car.
All right.
And then the final one would be Beatty and the lead car.
All right.
So then I can go ahead and create click events for them from no if I want.
So I'm going to double click the ad car and notice because I gave it a sensible name.
The click event is very clear.
Beatty and ad car on the score click.
Correct.
So I can just go ahead and create all those events accordingly.
So just go back and double click each until I have those click events and then this is why I don't like
pinning properties because in when writing code it's not useful.
Right.
So then I have three button click events with three buttons on my form.
So that's just some cosmetic stuff.
That's a start again and see what that looks like.
All right.
So now we see that a grid is a bit smaller and we have the data being displayed.
Sure.
But then to the right we see our buttons and well they're all dead buttons because they have click events
but the click events aren't doing anything just yet.
So no I want to clean up the grid a bit.
I don't want the I.D. and name columns to be displaying in come on letters like that.
So what I'm going to do is inside of my form load phone shun when I set the data source I noticed that
when the data grid is generating the columns relative to the data it is being fed.
So I'm feeding it the list of cars and the list of cars each object has I.T. it has name and some list
of car rental records.
Right.
Which I'll soon explain exactly what that is.
Well at least later on we'll see exactly what that is.
So what I'm going to do for my data grid is I'm going to say GV vehicle list dot and then I want to
specify that I want the first column which I know is going to be I.T. second C columns.
And if you have ever worked with Aries our collection.
So we are working with that list and that's our collection type of a work with an array you know that
the square brackets represent.
You can specify exactly which subscript of the Ari you want.
Right.
So if you haven't.
That's fine.
So let me explain.
So the columns here.
This is a grid view control and then I'm saying I want the columns.
And if it's columns that means it's off collection there's more than one columns.
I want the first column which I know as a programmer is going to be Ida anyway so I can see square brackets
zero because in computers you start counting from zero not one.
Right.
So the first subscript of 3 0.
So the first script is 0 and then I want the property of this which is the header text to be equal to
the text I.D. in all caps right.
So I'm seeing that after I get this stuff cars and then I get and then I said the data source at this
point all of the columns would have been generated.
I am seeing that the first column that was generated I want the heretics to be I.D. And then I want
the second column so I'm just going to duplicate that line and Jesus a subscript from 0 to 1.
And I want this one to be name in all caps.
All right.
So let's see.
Hold that look.
So if I click Start then I'm going to see that I'm getting Idi name and I'm still seeing Kyra into records.
But at least the column headers are changing accordingly.
So that's encouraging at least.
All right.
So the next thing I want to do is to eliminate the fact that I'm getting but that seemingly random car
into records column.
But before I do that let me just explain a bit to what that represents.
So I opened up my day it's a model which is the idea makes file and I'm seeing this yard.
If we look in the table types of car or types of cars we will see that we have the idea we have the
name and then below we have this navigation property called car and title records.
Right.
So just humor me for a bit and let me take you on this journey if you will have our intel system and
then you will have a car that is rented multiple times then that means that this car is associated with
multiple records.
Right.
So if I wanted to know how many times my I believe of a car called Lada or Honda how many times my Honda
was rented then I would have literally at least off the times this car was rented.
Right.
So that is exactly what this navigation property represents.
It says that for any car that you're going to look at I can show you the list off or into our records
that it is associated with because they are related.
All right.
And then in the same way the car into record knows the exact type of car that was rented in this record.
So this is whole link and an entity framework actually global it's helping you to form good relationships
between your tables and actually query data across multiple tables with much ease but without getting
too much into you know complicating exactly how they're used right.
No I don't need the reference to the car rental records just yet.
So back in my code file I'm going to tweak this query a bit to select exactly what I want.
So this is doing a select style.
And we did a select star in our adder into our record once again for our combo box where I said that
doing it this way is just like seeing select star in this case with OnStar.
We literally just want I.D. and name because when we're doing it this way we're getting but the random
seemingly random column of car into the records.
Right.
So I'm going to go back and I'm going to show you how we can treat this someone to come in this place
and I'm going to say that this is the way you do select star from types of cars right.
Just so we're clear.
But then I'm going to rewrite this story and I'm going to walk you through exactly what we're doing.
So I'm going to save our cars.
Once again is equal to the connection to the database not the table that I want.
Well then I'm not willing to say at least this time I'm going to say not silly.
All right.
And then when I do that I'm going to have to work a bit of I want to call it magic.
But as I said I'm here to guide you through.
So I want to select just the two columns that I want.
I'm sure if you do if you're used this school then you would think that you'd say Oh I.
Come on in.
Well it's unfortunately not that simply in C sharp.
We're going to have to do what we call a lot of lambda expression where we create an object.
All right.
So this is lambda.
So this.
That's just it's right.
There's nothing more to explain.
The only thing I can explain here is that this doesn't have to be a Q.
This could be a W.
This could be a word.
I mean you can use any expression you want but the Q or whatever expression you you use here is going
to represent every object that is inside of the collection.
Right.
So this is just how you start a lambda expression.
You have an expression what you want is a letter or a word or whatever and then you have equal and then
the greater than saying or I just called it the arrow.
Right.
So you have the expression then the arrow and then this allows you to see what columns you want from
the table.
So I am saying select then I initialize alumni expression and then using the lambda expression I can
see CU dot I.D..
Right.
But then I don't want just idea I want a new object.
So what I'm want to see is new and then I'm open up.
I open up my curly braces and then inside of this I'm going to specify that I want.
And Heidi being equal to Q Dot I.D. then I can use my comma and say I want a new a new property called
name and it should have the value Q Dot name.
All right.
So if you're you still askew and aliasing.
All right we're in escrow you would've said something like SELECT this and you give it an alias so that
selected the column I would say select I.D. as another name on select to name as something else or whatever.
This is how you go about doing aliasing inside of the link.
Right.
So once again let me just walk you through what we're doing or saying I want to connect to the database.
Give me the table and then I want to select.
And then we do a lambda expression and then after the lambda expression we are selecting a new object.
So we're literally just building an object on the fly.
This this is just a new object that we're creating for our own purpose and what I want in my object
is a property called I.D. which is going to get the value that's coming from the database with an I.D.
come on another property called name and it is going to get the value coming from the database with
the word name.
And once again these are aliases.
Right.
So I could have used anything here I didn't have to use idea name again I could have used car I.D..
Right.
And I could have used car name.
It doesn't really matter.
So it's up to me to know what I am naming these.
Once again these are aliases.
If you are used to school and then for as many properties as they're coming back on I want and I want
to create an alias I could just say Como and do the same alias e was lambda expression dot whichever
column I want.
All right lambda variable or other.
And then after doing all of that I notice all of that's happened instead of the curly braces.
Then I would end with a semicolon but then I would probably want to do a list also.
So I like lists so.
And just me it makes it much easier to just when you do a link query just make everything I list afterwards.
If you're selecting expecting a collection of results.
Right.
And then some people would actually break this into multiple lines so they would actually do something
like the query here and then they a dot select in another line and then so then they would do that.
Oh it's a bit more I won't do that one though.
So that is how you would go about building open aliasing system.
So this situation will be more like select and then I would say something like I.D. as car I.D..
Kamal name as car name from types of cars.
Right.
So I'm gonna show you that link is variable for that and it does create some wonderful obstruction from
the typical school while maintaining C sharp code.
It does take practice it does day getting used to.
So if you will have to kind of do it to be able to reap the benefits of what it allows you to do.
Right.
So if I do start again and then we look at our grid we see that we're getting bug eyed.
You are getting by name and we're only getting about those two we're not getting that third column again
and that's once again because I specified what I wanted to select on even though I changed the names
I still told the grid view that the IDA ideas should be the column name for the first one I name the
column name for the second one so I'm just going to put our breakpoint at the point where we're sitting
the data source and I want to close the window and I'm going to load it again and then I'm going to
look instead of my car's variable where you see it's coming back with five and if you look at the object
listing you're seeing car idea and car names.
So we created custom objects that we wanted to create at least and then that that list of custom objects
is being used as a data source for our grid and then we set the column names and then our grid is displayed.
So I'm removing the breakpoint clicking continue and that is the okay.
So in the next video we'll start looking at how we wire up these buttons for adding to the database
editing car record and removing a car record.