Welcome back.
All right.
And at this point, we should be able to run this application and put data into the database.
So enter amount.
And click save.
Are you sure you want to save data saved And you see it's there.
Okay.
And now we have what we wanted.
We have the capability of adding data to a database and displaying it in a data grid view, which is
what we have here.
Okay, so now you know how to add data to a database.
You know how to get the data from a database.
And now what we want to have is the option to edit data from a database.
So let me just try that.
Let me save it.
You see, I didn't update it because none of those buttons actually work.
We still need to give them functionality, but you can see I can add values in here, which is very
bad.
Now I have to guess which one is it?
Now that's very confusing.
And now I definitely need the capability of editing or deleting something.
So let's now first of all, test if our currency converter is still going to work.
So I'm going to just add random values here.
Okay, So this one will be USD, let's save it and this one will be Florin, even though Florin don't
exist anymore.
Okay, so.
Let's just make the comparison here.
111 and we use USD and fluorine.
You can see they appear here.
Why do they appear here?
Well, because we set the itemssource of those boxes to be the data that we have in the data table,
which is amazing.
And now we can convert and you can see it actually works.
So now our two fully works already, but of course it's not as convenient as we would like it to be.
We cannot add it and we cannot delete.
And that's what we're going to cover now.
And by the way, if you made it this far, then definitely hit the like button, please, because I
put a lot of work into this.
When I built this video, I even thought, man, this could be a complete course that I could sell.
And now I'm offering it on YouTube and I'm like, Oh man, this is so much time that I'm investing into
this.
So if you appreciate the time and effort I put into this, then please hit that like button and also
subscribe.
And if you want to know more about databases and C-sharp and WPF and yeah, game development, for example,
definitely consider buying my complete C-sharp masterclass.
You can find the link in the description below.
Okay, so we're over an hour in one hour 20 or something into this video.
So now let's go ahead and add the functionality to actually do editing and.
To do selection change and sales change and all of the good stuff.
First of all though, let's add functionality for the cancel button, because if we look at it, we
have this cancel button and it doesn't do anything yet.
So let me quickly add that.
So this button cancel.
It's very simple and I need to stop the execution of the program for all of those green lines to disappear.
So we have button cancel, click well, a basic click event, right with the root event and object sender.
So we try to clear the master, which means we use this method we have here where we reset everything
and we get the data.
So we don't delete the data from the database, but we're just clearing the text fields and we make
sure that the button save content button is back to save.
And if an error occurs, we just display a message box to the user so he knows, okay, there's something
wrong.
Okay.
So now that that is out of the way.
Let's take care of those methods that we have not implemented.
So now I have two button clicks.
Let's or button cancel clicks.
Let's get rid of those.
So we need to implement this currency.
Selected cells changed.
Okay.
And what I want to execute in here, this is going to be a little more code, so let me paste it in
here and talk about it.
So we have.
This event that will be triggered once we clicked on one of those cells.
Okay.
Once we change the cell, so to speak, and what we are going to try because things could go wrong.
We, first of all, create a data grid and what do we create it from?
Well, we create it from the sender.
So you can see the object sender object is very convenient because in C-sharp everything is an object.
But what we are trying to do is we're trying to make an object of type data grid out of the sender.
And as we are in an event method, the selected cells changed method that will be executed once we click
on those on the data grid itself.
So on this here, the sender will be in fact a data grid.
It will be specifically the grid that we are working with.
So now what we can do is we can go ahead and create a data row view out of the row that was selected.
We're going to call this row selected and it will be the grid, which is our data grid dot current item.
So the current item itself might not be of type data row view, it will be of type object, which everything
is an object, right?
But we want it to be a data row view because then we can use row selected in certain ways.
Okay.
Because now we can get the column name of it and all of the good stuff.
Okay, so now we know which row we selected.
And if the row selected is not null, which means we have a row that was selected because that's basically
what we get from here.
We get the row and if it wasn't empty, we check if the DVD or DVD or so data grid view currency items
count is greater than zero.
So we have some items inside of our data grid.
So here there are items in here.
If there are none, then this wouldn't work.
But as there are some, we get into this if statement and this code will be executed.
So then we check if the selected cell is greater than zero, which means we have selected one.
So these are just these if statements are just to make sure that everything is clean, that we actually
have something that we have an object that we can use and so forth.
This is just to make sure that we don't run into errors straight away and then we get the currency ID
and what do we get it from?
Well, we parse it from the selected row that we have selected and we get its ID and we make a string
out of it.
So this gives us the ID of the selected row.
So, for example, this here, this is a cell in the row which is at the very top here, for example.
But internally it could have an ID of 3 or 4 or whatever, because if you change it here, you see suddenly
this row is at the bottom.
So which one is the ID one now or ID zero because we don't display the ID in our UI.
So I'm getting the ID now with this approach and then I check if the selected cells at position zero
at the column of it is the display index of zero.
So then it means that it is the added cell.
So if we selected the edit cell, so if we clicked on the edit cell, then we want to execute some code.
What do we want to execute?
Well, we want to execute the.
Following code.
So we're saying that the row selected amount to string should be the text amount dot text.
So what does that mean?
Once I click on this thing here, you can see that this text is edited.
Okay, now it says 111 once I click on it.
And once I click on it, this currency name here, this text field, this text box is also populated.
And that has to do with this line here where it says text currency name, dot, text row, selected
currency name to string.
So we're taking from the row that we selected the currency name, which is from which we get from the
data table or actually the grid itself.
And because the grid, well, we get the row from the grid and the grid has the data from the database.
So we're not specifically getting it from the database, but we're getting it from the grid itself,
from the row of the grid.
And now we can populate our text fields or text boxes with that.
So we say the text should be whatever we get from the data grid and we make sure that the button save
button content is set to update.
You can see now it's update.
If I click cancel, it's back to save.
Once I click on this button here, you say or you see it goes back to update.
That's what this code here does.
And then in the next if statement so both of them can be executed.
This is not an else if then we check is the column display index one even though we could of course
do an else if well it's fine.
So if it's one then we know that the lead cell was clicked on.
So then we know that this button was clicked on because this is the column one.
This is the column zero.
This is column one, This is column to column three.
Okay.
So we know that the display index of that column is one.
Then it's the delete button.
So then we can just ask the user if he really wants to delete whatever he clicked on.
And if he says yes, which is this if statement here, we say, let me put that into a different line
or in two lines you can see better.
So we see hit the user press.
Yes.
If you did press yes, establish a connection to your SQLite, then create a data table, execute a
command, then do the command type text.
So execute the text and then do the command parameters here and.
Said the well, add the value with the ID, with the currency ID and then execute that value.
So basically we're saying delete it from our currency master table, which is this one here.
So delete it where the ID is the ID that we have currently selected, which is the currency ID, Where
do we get it from?
Well, it's the one that we have currently selected from the row that we have selected.
So here we're saying please delete the row that we have selected from the database and then we use this
message box to show deleting was successful.
And then we use Clear Master.
What does Clear Master do once again?
Well, if you recall, it says or it calls this get data and bind currency, which basically resets
everything and displays the data in the right format.
So basically after deleting, you want to make sure that the deleted row doesn't appear in your data
grid anymore because otherwise you might be confused because you think, okay, I have deleted it,
but why is it still there?
And that's why you need to make sure that you also update the UI after you have updated your database
because otherwise they are not in sync with each other and this could cause a well, all sorts of problems.
Okay, so.
Now that we have that out of the way, we clear the master.
And well, if an error occurs, we just display that there is an error.
We say what the message is and we say it's an error and that's it.
So now at this point, we should be able to delete rows.
Let's see.
Let's go to the currency master.
And you can see that nothing is in here.
Let me test it.
So one test.
Let's add that.
Okay, so you see that we are not displaying the data grid at the beginning.
This is great.
A little test for you.
How can you make sure that the data grid is loaded at the beginning when this application is loaded?
Okay.
Because before I go any further, I want to make sure that we do that so you can pause the video and
try it yourself.
So basically.
In the main window once we are establishing.
The execution here once we.
Start running the app and let me stop running the app.
We want to make sure that we run bind currency as well as get data.
So this will now get us the data and we'll bind the concurrency.
And now.
If you run it and we go to Currency Master, you can see all of them are there.
So now they are there.
Now it works.
Now let me delete this one.
Yes.
Test was not successful.
Let me add it.
This one.
And as you see Add, it doesn't do anything because we didn't actually implement any functionality for
added yet except for.
Of course, that we need to add it here.
So yes, we can edit it.
It's there.
So let me use a different one here.
OMG is the new currency name.
Let me update that.
And data was successfully updated.
And you can see the data is in fact updated and it's here.