需要帮助以获取C#datagrid视图中“数量”列的默认值,并且该值可以更改

时间:2018-12-01 09:02:06

标签: c# datagridview

在Windows形式c#datagridvew中,我将其设置为销售发票形式的默认值。我将数量的默认值设置为1,但是如果要更改它,则不能;当我在editend之后单击时,所有行再次变为1:

//Only for Default Value
private void dgvitemDetail_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    foreach (DataGridViewRow row in this.dgvitemDetail.Rows)
    {
        if (row.IsNewRow) break;
        row.Cells[5].Value = "1";
    }
}

1 个答案:

答案 0 :(得分:0)

您不希望“休息”;您想要“继续;”

“继续”告诉代码跳过所有内容并转到列表中的下一项。 “中断”告诉switch语句这是本节代码的结尾。

几个例子:

for (var animal in animals)
{
  if (animal == "cat") continue;

  Console.WriteLine("Is not a cat!");
}

还有一个switch语句:

switch (animal)
{
    case "cat":
      Console.WriteLine("I'm a cat!!");
      break;
    default:
      Console.WriteLine("I wish I was a cat!");
      break;
}

我想你想要

void dgvitemDetail_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{ 
    foreach (DataGridViewRow row in this.dgvitemDetail.Rows) 
    { 
        if (row.IsNewRow) continue;

        row.Cells[5].Value = "1";
    }
}

甚至更好:

void dgvitemDetail_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{ 
    foreach (DataGridViewRow row in this.dgvitemDetail.Rows) 
    { 
        // 0 = if it's a new row and 1 = a preexisting row
        row.Cells[5].Value = row.IsNewRow ? 0 : 1;
    }
}