C
C#β€’2y ago
Gijs

❔ winforms storage system (beginner)

I'm new to winforms and for school I have to make a storage system. I'm completly done the only thing that doesn't work is the selling of shoes, I have no idea how to implement this. I use a datagridview to show the storage, the person selling has a textbox and a button where they can first put the number in the textbox and then click the button to remove x amount of shoes this is my code:
Public mwVoorraad()
{

InitializeComponent();
FillDataGrid();

}
public void FillDataGrid()
{
dt.Rows.Clear();
dt.Columns.Clear();
dt.Columns.Add("Merk");
dt.Columns.Add("Type");
dt.Columns.Add("Maat");
dt.Columns.Add("Kleur");
dt.Columns.Add("Prijs");
dt.Columns.Add("Aantal");

foreach (string[] schoen in voorraad.schoenenvoorraad)
{
dt.Rows.Add(schoen);
}
dgvAdmin.DataSource = dt;
}

private void btVerkoop_Click(object sender, EventArgs e)
{

}

private void btBackToMenu_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Public mwVoorraad()
{

InitializeComponent();
FillDataGrid();

}
public void FillDataGrid()
{
dt.Rows.Clear();
dt.Columns.Clear();
dt.Columns.Add("Merk");
dt.Columns.Add("Type");
dt.Columns.Add("Maat");
dt.Columns.Add("Kleur");
dt.Columns.Add("Prijs");
dt.Columns.Add("Aantal");

foreach (string[] schoen in voorraad.schoenenvoorraad)
{
dt.Rows.Add(schoen);
}
dgvAdmin.DataSource = dt;
}

private void btVerkoop_Click(object sender, EventArgs e)
{

}

private void btBackToMenu_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
if there is anything else you spot wrong with the code please let me know!
88 Replies
Pobiega
Pobiegaβ€’2y ago
thats literally just the code that sets up the view you are not showing your vooraad and its declaration, which is where you actually store your data
Gijs
GijsOPβ€’2y ago
This is the vooraad class
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
and this is the program I hard coded a shoe in here just for testing
static void Main()
{

string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new inlogMenu());
}
static void Main()
{

string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new inlogMenu());
}
sorry I forgot
Pobiega
Pobiegaβ€’2y ago
okay idk dutch, so i don't know if 32 or 200is your "number of shoes in storage" but I see an issue here your data is all strings its hard to do math on strings, as they are... well, not numbers
Gijs
GijsOPβ€’2y ago
it's the 0 at the end
Pobiega
Pobiegaβ€’2y ago
how can you sell shoes with 0 in storage? πŸ˜›
Gijs
GijsOPβ€’2y ago
I should change that ye xD
ꜲΓ₯ąɐȁặβ±₯ᴀᴬ
they call that a promise, sometimes a future
Gijs
GijsOPβ€’2y ago
I changed it to 10 for now
Pobiega
Pobiegaβ€’2y ago
well, thats honestly not your biggest problem. Your problem is that string is a poor datatype for numbers
Gijs
GijsOPβ€’2y ago
On my other menu the manger can add shoes by adding them I can send that part of the code aswell?
Pobiega
Pobiegaβ€’2y ago
that can wait we need to fix your data types do you know any numeric data types, of the top of your head?
Gijs
GijsOPβ€’2y ago
things like int and float?
Pobiega
Pobiegaβ€’2y ago
yep!
Gijs
GijsOPβ€’2y ago
oh ye
Pobiega
Pobiegaβ€’2y ago
int being whole numbers, and float being numbers with decimals ie 5 vs 7.1234 so which one would be good for storing the amount of shoes?
Gijs
GijsOPβ€’2y ago
so I would use int and not string
Pobiega
Pobiegaβ€’2y ago
sounds good! but
string[] schoen1 = { "Nike ", "Airmax ", 32, "Zwart ", 200, 10};
string[] schoen1 = { "Nike ", "Airmax ", 32, "Zwart ", 200, 10};
isnt going to work is it?
Gijs
GijsOPβ€’2y ago
nope you pharse the string to an int right
Pobiega
Pobiegaβ€’2y ago
no have you learned about classes yet?
Gijs
GijsOPβ€’2y ago
not really I asked my teacher but he just made one for me without really explaining saying "we teach that later"
Pobiega
Pobiegaβ€’2y ago
thats weird you sorta need that to solve this problem nicely the problem we have that a line in your datagrid should represent a single item
Gijs
GijsOPβ€’2y ago
I know you can make them to use them for making "databases" without having a real database or smth like that
Pobiega
Pobiegaβ€’2y ago
one ShoeListing or ShoeInventoryRow or something unrelated. lets leave the shoe world for a while
Gijs
GijsOPβ€’2y ago
alright
Pobiega
Pobiegaβ€’2y ago
lets say that you want to represent a person, with name and date of birth a single person has ONE name and ONE date of birth. They must always have both. we want these values to be linked somehow can you think of a way to do so right now?
Gijs
GijsOPβ€’2y ago
could you use a list or array for that? or am I just completely miss
Pobiega
Pobiegaβ€’2y ago
not really both lists and arrays are for storing multiple things of the same type the real answer here is to make a new type we MAKE a new type that represents out Person
public class Person
{
public string Name { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
}
public class Person
{
public string Name { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
}
Gijs
GijsOPβ€’2y ago
ah I see, so just for my understanding a type is the public class person part?
Pobiega
Pobiegaβ€’2y ago
Person is the type
Gijs
GijsOPβ€’2y ago
I see
Pobiega
Pobiegaβ€’2y ago
the class is the keyword that declares that this is a class (there are other things that are also types, such as structs, enums, records etc) but for now lets keep it simple and care only about classes
Gijs
GijsOPβ€’2y ago
alright
Pobiega
Pobiegaβ€’2y ago
so, lets make a type that represents your shoe-inventory can you try and show me what you come up with?
Gijs
GijsOPβ€’2y ago
public class voorraad
{
public int ShoeListing { get; set; }
pubic ShoeInventoryRow { get; set; }
}
public class voorraad
{
public int ShoeListing { get; set; }
pubic ShoeInventoryRow { get; set; }
}
` would it be something like this? or do I miss understand
Pobiega
Pobiegaβ€’2y ago
Oh, I meant each individual row πŸ™‚
Gijs
GijsOPβ€’2y ago
ah
Pobiega
Pobiegaβ€’2y ago
Like, make a type that represents { "Nike ", "Airmax ", 32, "Zwart ", 200, 10};
Gijs
GijsOPβ€’2y ago
public class voorraad
{
public string brand { get; set; }
pubic string type { get; set; }
public string size { get; set; }
public string color { get; set; }
public decimal price { get; set; }
public int amount { get; set; }
}
public class voorraad
{
public string brand { get; set; }
pubic string type { get; set; }
public string size { get; set; }
public string color { get; set; }
public decimal price { get; set; }
public int amount { get; set; }
}
like this?
Pobiega
Pobiegaβ€’2y ago
Almost! Can you think of a better data type for a price?
Gijs
GijsOPβ€’2y ago
int!
Pobiega
Pobiegaβ€’2y ago
Sure! Or maybe even decimal in case we want to be able to say that the price is 199.99
Gijs
GijsOPβ€’2y ago
oh ye true because you work with number behind the .
Pobiega
Pobiegaβ€’2y ago
Also, C# naming convention says that classes and properties should be PascalCased so begin with a capital letter
Gijs
GijsOPβ€’2y ago
aha got it so would I make a new class for this or do I add it to my existing code?
Pobiega
Pobiegaβ€’2y ago
are those not the same thing? πŸ™‚
Gijs
GijsOPβ€’2y ago
oh they are? πŸ˜…
Pobiega
Pobiegaβ€’2y ago
sounds like it to me πŸ˜„
Gijs
GijsOPβ€’2y ago
aha good to know, so this is added to the first piece of code I sended?
Pobiega
Pobiegaβ€’2y ago
Oh, it goes in its own file ideally
Gijs
GijsOPβ€’2y ago
so I creat another one of these? If I understand correctly
No description
Pobiega
Pobiegaβ€’2y ago
Yeah thats a code file
Gijs
GijsOPβ€’2y ago
Alright I have done that
Pobiega
Pobiegaβ€’2y ago
Cool so, now lets replace that string array we had to keep track of our inventory
Gijs
GijsOPβ€’2y ago
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
this one right?
Pobiega
Pobiegaβ€’2y ago
uh yeah, that too I was thinking about
string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
but they both need to be changed
Gijs
GijsOPβ€’2y ago
so I replace them with the public class or?
Pobiega
Pobiegaβ€’2y ago
no, we replace the types πŸ™‚ public static List<string[]> schoenenvoorraad is a list of string arrays we want this to be a... ?
Gijs
GijsOPβ€’2y ago
class?
Pobiega
Pobiegaβ€’2y ago
no, we already have the class you made it before, ye? this
Gijs
GijsOPβ€’2y ago
yes
No description
Pobiega
Pobiegaβ€’2y ago
thats the one fix the class name would you? AantalVooraad if nothing else
Gijs
GijsOPβ€’2y ago
should I just change it to Voorraad
Pobiega
Pobiegaβ€’2y ago
Doesnt that mean "Storage"?
Gijs
GijsOPβ€’2y ago
Yes
Pobiega
Pobiegaβ€’2y ago
I think thats a bad name to be honest How about ShoeListing, or ShoeInventoryRow? you can translate as needed
Gijs
GijsOPβ€’2y ago
Alright that does sound abit more clear indeed
Pobiega
Pobiegaβ€’2y ago
we want our names to accurately represent their content
Gijs
GijsOPβ€’2y ago
I have changed it to ShoeListing
Pobiega
Pobiegaβ€’2y ago
ok that is the name of your type our datagrid is a list of shoe listings what is a type that would accurately represent that?
Gijs
GijsOPβ€’2y ago
string?
Pobiega
Pobiegaβ€’2y ago
really? I think you already know the type for a list, no?
Gijs
GijsOPβ€’2y ago
I have not heared of that I have only heared of just list
Pobiega
Pobiegaβ€’2y ago
Look here
Gijs
GijsOPβ€’2y ago
class?
Pobiega
Pobiegaβ€’2y ago
What do you think List<string[]> is?
Gijs
GijsOPβ€’2y ago
ph oh array
Pobiega
Pobiegaβ€’2y ago
no its a... ?
Gijs
GijsOPβ€’2y ago
list array?
Pobiega
Pobiegaβ€’2y ago
its a list of...? insert the last words πŸ˜›
Gijs
GijsOPβ€’2y ago
I'm sorry I'm not really concentrated my normal lesson just started πŸ˜…
Pobiega
Pobiegaβ€’2y ago
Pay attention to your class then πŸ™‚ this thread isnt going anywhere
Gijs
GijsOPβ€’2y ago
Alright, I think I can mess around from here myself! Still thanks tho and for having the patience to help me!
Pobiega
Pobiegaβ€’2y ago
I'm still here if you need help. And there are a few steps we skipped that I think you need to do
Gijs
GijsOPβ€’2y ago
Alright I'll mess around when my lesson is done thank you so much for the help
Pobiega
Pobiegaβ€’2y ago
np The answer I was looking for was A list of string arrays btw
Gijs
GijsOPβ€’2y ago
ah thanks
Pobiega
Pobiegaβ€’2y ago
List<T> where T = string[]
Accord
Accordβ€’2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?