繁体   English   中英

如何从DataGridView中的列获取所有值

[英]How to get all values from a column in a DataGridView

您会看到,如果项目名称已经存在于DataGridView中,我正在尝试添加数量。 因此,我做了一个for循环来检查DataGridView中的每一行。 它所做的只是添加更多行。

for (int x = 0; x < dataGridView.Rows.Count; x++)
{
    if (dataGridView.Rows[x].Cells[1].Value.ToString() == dataTable.Rows[0][2].ToString())
    {
        dataGridView.Rows[x].Cells[0].Value = int.Parse(dataGridView.Rows[x].Cells[0].Value.ToString()) + 1;
        dataGridView.Rows[x].Cells[2].Value = Convert.ToDecimal(dataTable.Rows[0][4].ToString()) * Convert.ToDecimal(dataGridView.Rows[x].Cells[0].Value.ToString());
    }
    else
    {
        quantity = 1;
        dataGridView.Rows.Add(quantity, dataTable.Rows[0][2], dataTable.Rows[0][4]);
    }
}

我认为在字符串比较中使用.ToLower()或.ToUpper()更好。

我解决了! 我只是使用for循环做了一个检查器!

for (int x = 0; x < dataGridView.Rows.Count; x++)
{
     if (dataGridView.Rows[x].Cells[1].Value.ToString() == dataTable.Rows[0][2].ToString())
     {
          same = true;
          counter = x;
     }
}
if (same == true)
{
     dataGridView.Rows[counter].Cells[0].Value = int.Parse(dataGridView.Rows[counter].Cells[0].Value.ToString()) + 1;
     dataGridView.Rows[counter].Cells[2].Value = Convert.ToDecimal(dataTable.Rows[0][4].ToString()) * Convert.ToDecimal(dataGridView.Rows[counter].Cells[0].Value.ToString());
}
else
{
    quantity = 1;
    dataGridView.Rows.Add(quantity, dataTable.Rows[0][2], dataTable.Rows[0][4]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM