I'm pretty new to C#. I want to increment an int
variable id
by 1 and insert it into a datagridview. The problem is, it doesn't increment, it stays at 1.
Here's my code for adding the data to datagridview
class QuantityCtrl : Quantity
{
private ManageSale _manageSale;
public QuantityCtrl(ManageSale manageSale)
{
_manageSale = manageSale;
}
private void BtnOk_Click(object sender, EventArgs e)
{
_manageSale.dgvItemList.Rows.Add
(
GenerateId(),
_manageSale.lblName.Text,
_manageSale.lblPrice.Text,
_manageQuantity.txtDiscount.Text,
_manageQuantity.txtQuantity.Text,
Total
);
}
}
Here's my code for incrementing
class Quantity
{
public int OrderId = 1;
public int GenerateId()
{
return OrderId++;
}
}
I solved it thanks to Brendan. I hope it could be a referrence for some developers :) and If someone has a better way of incrementing you can answer it Thank you.
public static int OrderId = 1;
Another way of doing it would be creating a new class in a folder inside your solution which contains the list and the proprieties :
class IDS
{
#region Proprieties
public int Id { get; set; }
#endregion
#region Lists
public List<IDS> _ids = new List<IDS>();
#endregion
}
Now, you'll need to link the class to the main, for that, go in your main code and put at the top :
using SolutionName.Folder;
Next go to your button event and simply put this:
private void btnAutoGen_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
int iId = 0;
try
{
var req = (from value in _id
select value.Id).Max() + 1;
iId = req;
}
catch (InvalidOperationException)
{
iId = 1;
}
You now have a button that auto increment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.