如何只传递选定的项目 [英] How to pass only selected item
问题描述
for (int i = 0; i < dgvPizza.Rows.Count; i++)
{
if (dgvPizza.Rows.Count != 0)
{
DataRow row = dt.NewRow();
string qty = dgvPizza.Rows[i].Cells[1].Value.ToString();
string size = dgvPizza.Rows[i].Cells[0].Value.ToString();
string Name = dgvPizza.Rows[i].Cells[3].Value.ToString();
row[0] = qty;
row[1] = size;
row[2] = Name;
dt.Rows.Add(row);
}
}
我正在使用此代码在ComboBox
上选择尺寸并在TextBox
上输入数量,所有数量均在DataGridView
上,现在我只想传递我选择的Row
,但这给了我错误:对象引用未设置为对象的实例.但是,当我选择整个3行时,它将传递它们.我该怎么办?
I was using this code to select size on ComboBox
and enter quantity on TextBox
which all of them are on a DataGridView
and now I want to only pass the Row
that I have selected, but it gives me this error: Object reference not set to an instance of an object. But when I select whole 3 rows it passes them. What shall I do?
推荐答案
if ( dgvPizza.Rows.Count!=0)
这是没有意义的.如果计数为0,则您的循环将无法运行.
您需要告诉我们哪一行会给您带来错误,然后尝试使用一些标点符号,以便您的帖子可读.如果您在控件上使用SelectedItem属性,则可以仅查看选定的行.仅当未选择任何内容时,它为null,您应该检查一下.
不要发布有效的代码,发布无效的代码,这样我们就可以发现问题出在哪里.
This is pointless. If the count is 0, your loop would not run.
You need to tell us what line gives you an error, and try using some punctuation so your post is readable. IF you use the SelectedItem property on the control, you can look at just the selected row. It is null only if nothing is selected, you should check for that.
Don''t post the code that works, post the code that does not work, so we can see what is going wrong.
这篇关于如何只传递选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!