DataGridView单元格索引问题
好的,我的 Windows 应用程序有问题。我的 DataGridView 由以下列组成:ProductName、Qty、Price、Subtotal。所以我假设这些单元格索引分别如下:0、1、2、3。但是,每当我尝试执行下面的代码时:
txtSubtotalProducts.Text = "Php " + (Convert.ToDouble(dgvProducts .行[0].单元格[3].值) + Convert.ToDouble(dgvExpenses.Rows[1].Cells[3].Value) + Convert.ToDouble(dgvExpenses.Rows[2].Cells[3].Value)).ToString();
但是,每当我运行此代码块时,我都会遇到以下错误消息: 索引超出范围。
我在这里遗漏了什么吗?预先非常感谢。
编辑:抱歉,索引 4 实际上是我尝试过的,但最初,我尝试将 2 和 3 作为索引,但没有任何运气。但是,当我尝试 1(即“数量”列)时,它工作得很好。
Okay I have a problem with my Windows Application. My DataGridView consists of the following columns: ProductName, Qty, Price, Subtotal. So I am assuming that the Cell Indexes of these are respectively as follows: 0, 1, 2, 3. However, whenever I try executing the code below:
txtSubtotalProducts.Text = "Php " + (Convert.ToDouble(dgvProducts.Rows[0].Cells[3].Value) +
Convert.ToDouble(dgvExpenses.Rows[1].Cells[3].Value) +
Convert.ToDouble(dgvExpenses.Rows[2].Cells[3].Value)).ToString();
However, whenever I run this code block, I bump into this error message:
Index Out of Range.
Am I missing anything in here? Thanks alot in advance.
EDITED: Sorry, index 4 was actually something I tried, but originally, I am trying both 2 and 3 as indexes without any luck. However, when I try 1 (which is the Qty column) it works perfectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
史蒂芬是对的!
如果您有产品名称、数量、价格、小计,即 4 列。
因此,由于数组是从 0 开始的,因此您可以访问的可能列是 0、1、2 和 3。
因此,当您尝试访问
由于没有第 5 列(第 4 个索引)时,它将抛出“索引超出范围”异常:)
Stephan is correct!
If you have ProductName, Qty, Price, Subtotal that is 4 columns.
So as the arrays are 0 based, it the possible columns you can access is 0,1,2 and 3.
So when you try to access
As there is no 5th column (4th index), it will throw a Index Out of Range exception :)