[英]No error but something is wrong with DataGridView
我为学校编码的简单POS系统将vb.net代码大致翻译为c#。 但是,当我尝试使用按钮单击事件将内容添加到datagrid时,datagrid中没有任何更改。 没有错误。 至少尚未将datagrid连接到数据库。
这是我将数据插入数据网格的代码:
private void txtAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtProduct.Text) & !string.IsNullOrEmpty(txtQuantity.Text) & !string.IsNullOrEmpty(txtPrice.Text))
{
var with = dataSales;
int ii = 0;
int orderproduct = 0;
int count = 0;
for (ii = 0; ii <= with.RowCount - 1; ii++)
{
if (txtProduct.Text == Convert.ToString(dataSales[0, ii].Value))
{
count = count + 1;
if (count != 0)
{
orderproduct = Convert.ToInt32(dataSales[2, ii].Value) + Convert.ToInt32(txtQuantity.Text);
dataSales[2, ii].Value = orderproduct;
dataSales[3, ii].Value = Convert.ToInt32(dataSales[2, ii].Value) * Convert.ToInt32(dataSales[1, ii].Value);
}
else
{
if (count == 0)
{
float sum = Convert.ToInt32(txtPrice.Text) * Convert.ToInt32(txtQuantity.Text);
dataSales.Rows.Add(txtProduct.Text, txtPrice.Text, txtQuantity.Text, sum);
count = 0;
}
txtProduct.Clear();
txtQuantity.Clear();
txtPrice.Clear();
}
}
else
{
MessageBox.Show("Nothing is Selected", "Error");
}
}
}
}
跟随您的柜台,您将了解为什么从未添加任何东西。 您从0开始,但是每当有文本要添加时就添加+1。 将其调试并观看。
编辑:由于您的评论,您不知道如何调试。 在Visual Studio中打开.cs文件,单击左边的空白行以测试字段中的文本(下面的行):
if (!string.IsNullOrEmpty(txtProduct.Text) & !string.IsNullOrEmpty(txtQuantity.Text) & !string.IsNullOrEmpty(txtPrice.Text))
现在启动程序,输入文本并运行它。 它将在该行停止。 然后,您可以按F11键,它将转到增加计数器的行。 这意味着您将始终运行此分支。
if (count != 0)
{
orderproduct = Convert.ToInt32(dataSales[2, ii].Value) + Convert.ToInt32(txtQuantity.Text);
dataSales[2, ii].Value = orderproduct;
dataSales[3, ii].Value = Convert.ToInt32(dataSales[2, ii].Value) * Convert.ToInt32(dataSales[1, ii].Value);
}
注意:您不需要此:
if (count == 0)
{
}
为什么? 您已经将非0的任何内容发送到了另一个分支。 在这一点上,else足够了,无需浪费时间来确定总为零的值是否为零
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.