DataGridView获取ArgumentOutOfRangeException

时间:2017-08-06 19:15:42

标签: c# winforms datagridview

我有一个包含dataGridView的表单,并将其值传递给文本框,但是当我点击任意列时,我不断收到ArgumentOutOfRangeException

如果只点击行,这些代码可以正常工作。

private void dataGridProd_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewRow row = this.dataGridProd.Rows[e.RowIndex];

        foreach (DataGridViewColumn column in dataGridProd.Columns)
        {
            column.SortMode = DataGridViewColumnSortMode.NotSortable;

            txBName.Text = row.Cells[1].Value.ToString();
            txBPrice.Text = row.Cells[2].Value.ToString();
        }
        btnAdd.Enabled = true;
        prodQuanUpDown.Enabled = true;
    }

非常感谢任何类型的回复。提前谢谢!

2 个答案:

答案 0 :(得分:0)

首先,如果你得到foreach,那么你不能在这里和你的帖子中看到为什么你需要一个ArgumentOutOfRangeException循环,那么它必须是因为以下两行。你真的有那些行的列吗?您可以查看Count收藏集Cells

上的row.Cells.Count媒体资源
txBName.Text = row.Cells[1].Value.ToString();
txBPrice.Text = row.Cells[2].Value.ToString();

答案 1 :(得分:0)

产品名称是否真的在表格的第二列中,如果它位于第一列,它应该是row.Cells[0].Value.toString();

row.Cells[1].Value.toString();< - 这是第二列的价格,如果它在第二列

希望这个答案有所帮助!

相关问题