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
Pobiega2y 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
GijsOP2y 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
Pobiega2y 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
GijsOP2y ago
it's the 0 at the end
Pobiega
Pobiega2y ago
how can you sell shoes with 0 in storage? 😛
Gijs
GijsOP2y ago
I should change that ye xD
many things
many things2y ago
they call that a promise, sometimes a future
Gijs
GijsOP2y ago
I changed it to 10 for now
Pobiega
Pobiega2y ago
well, thats honestly not your biggest problem. Your problem is that string is a poor datatype for numbers
Gijs
GijsOP2y ago
On my other menu the manger can add shoes by adding them I can send that part of the code aswell?
Pobiega
Pobiega2y 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
GijsOP2y ago
things like int and float?
Pobiega
Pobiega2y ago
yep!
Gijs
GijsOP2y ago
oh ye
Pobiega
Pobiega2y 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
GijsOP2y ago
so I would use int and not string
Pobiega
Pobiega2y 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
GijsOP2y ago
nope you pharse the string to an int right
Pobiega
Pobiega2y ago
no have you learned about classes yet?
Gijs
GijsOP2y ago
not really I asked my teacher but he just made one for me without really explaining saying "we teach that later"
Pobiega
Pobiega2y 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
GijsOP2y ago
I know you can make them to use them for making "databases" without having a real database or smth like that
Pobiega
Pobiega2y ago
one ShoeListing or ShoeInventoryRow or something unrelated. lets leave the shoe world for a while
Gijs
GijsOP2y ago
alright
Pobiega
Pobiega2y 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
GijsOP2y ago
could you use a list or array for that? or am I just completely miss
Pobiega
Pobiega2y 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
GijsOP2y ago
ah I see, so just for my understanding a type is the public class person part?
Pobiega
Pobiega2y ago
Person is the type
Gijs
GijsOP2y ago
I see
Pobiega
Pobiega2y 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
GijsOP2y ago
alright
Pobiega
Pobiega2y ago
so, lets make a type that represents your shoe-inventory can you try and show me what you come up with?
Gijs
GijsOP2y 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
Pobiega2y ago
Oh, I meant each individual row 🙂
Gijs
GijsOP2y ago
ah
Pobiega
Pobiega2y ago
Like, make a type that represents { "Nike ", "Airmax ", 32, "Zwart ", 200, 10};
Gijs
GijsOP2y 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
Pobiega2y ago
Almost! Can you think of a better data type for a price?
Gijs
GijsOP2y ago
int!
Pobiega
Pobiega2y ago
Sure! Or maybe even decimal in case we want to be able to say that the price is 199.99
Gijs
GijsOP2y ago
oh ye true because you work with number behind the .
Pobiega
Pobiega2y ago
Also, C# naming convention says that classes and properties should be PascalCased so begin with a capital letter
Gijs
GijsOP2y ago
aha got it so would I make a new class for this or do I add it to my existing code?
Pobiega
Pobiega2y ago
are those not the same thing? 🙂
Gijs
GijsOP2y ago
oh they are? 😅
Pobiega
Pobiega2y ago
sounds like it to me 😄
Gijs
GijsOP2y ago
aha good to know, so this is added to the first piece of code I sended?
Pobiega
Pobiega2y ago
Oh, it goes in its own file ideally
Gijs
GijsOP2y ago
so I creat another one of these? If I understand correctly
No description
Pobiega
Pobiega2y ago
Yeah thats a code file
Gijs
GijsOP2y ago
Alright I have done that
Pobiega
Pobiega2y ago
Cool so, now lets replace that string array we had to keep track of our inventory
Gijs
GijsOP2y 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
Pobiega2y 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
GijsOP2y ago
so I replace them with the public class or?
Pobiega
Pobiega2y ago
no, we replace the types 🙂 public static List<string[]> schoenenvoorraad is a list of string arrays we want this to be a... ?
Gijs
GijsOP2y ago
class?
Pobiega
Pobiega2y ago
no, we already have the class you made it before, ye? this
Gijs
GijsOP2y ago
yes
No description
Pobiega
Pobiega2y ago
thats the one fix the class name would you? AantalVooraad if nothing else
Gijs
GijsOP2y ago
should I just change it to Voorraad
Pobiega
Pobiega2y ago
Doesnt that mean "Storage"?
Gijs
GijsOP2y ago
Yes
Pobiega
Pobiega2y ago
I think thats a bad name to be honest How about ShoeListing, or ShoeInventoryRow? you can translate as needed
Gijs
GijsOP2y ago
Alright that does sound abit more clear indeed
Pobiega
Pobiega2y ago
we want our names to accurately represent their content
Gijs
GijsOP2y ago
I have changed it to ShoeListing
Pobiega
Pobiega2y 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
GijsOP2y ago
string?
Pobiega
Pobiega2y ago
really? I think you already know the type for a list, no?
Gijs
GijsOP2y ago
I have not heared of that I have only heared of just list
Pobiega
Pobiega2y ago
Look here
Gijs
GijsOP2y ago
class?
Pobiega
Pobiega2y ago
What do you think List<string[]> is?
Gijs
GijsOP2y ago
ph oh array
Pobiega
Pobiega2y ago
no its a... ?
Gijs
GijsOP2y ago
list array?
Pobiega
Pobiega2y ago
its a list of...? insert the last words 😛
Gijs
GijsOP2y ago
I'm sorry I'm not really concentrated my normal lesson just started 😅
Pobiega
Pobiega2y ago
Pay attention to your class then 🙂 this thread isnt going anywhere
Gijs
GijsOP2y ago
Alright, I think I can mess around from here myself! Still thanks tho and for having the patience to help me!
Pobiega
Pobiega2y 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
GijsOP2y ago
Alright I'll mess around when my lesson is done thank you so much for the help
Pobiega
Pobiega2y ago
np The answer I was looking for was A list of string arrays btw
Gijs
GijsOP2y ago
ah thanks
Pobiega
Pobiega2y ago
List<T> where T = string[]
Accord
Accord2y 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?