2016-05-15 37 views
-2
private void btnpass_Click(object sender, EventArgs e) 
{ 
    Inventory coo = new Inventory(
     dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), 
     dataGridView1.SelectedRows[0].Cells[1].Value.ToString(), 
     dataGridView1.SelectedRows[0].Cells[2].Value.ToString(), 
     dataGridView1.SelectedRows[0].Cells[3].Value.ToString(), 
     dataGridView1.SelectedRows[0].Cells[4].Value.ToString(), 
     dataGridView1.SelectedRows[0].Cells[5].Value.ToString(), 
     dataGridView1.SelectedRows[0].Cells[6].Value.ToString()); 
    coo.Show(); 
} 

上我試圖所選行GridView到另一個GridView通過另一種形式的上,但我得到一個錯誤:通行證所選行一個gridview的另一個gridview的另一種形式

 
Index was out of range. Must be non-negative and less than the size of the collection. 
Parameter name: index 

我使用了不同的方法,但它並沒有解決我的問題。

回答

0

您的錯誤信息表明,在電網無行或沒有選擇

首先檢查的選定行的任何行的,如果它是大於零,然後做你的工作 這樣的:

if(dataGridView1.SelectedRows.Count>0) 
{ 
Inventory coo = new Inventory(
dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), 
dataGridView1.SelectedRows[0].Cells[1].Value.ToString(), 
dataGridView1.SelectedRows[0].Cells[2].Value.ToString(), 
dataGridView1.SelectedRows[0].Cells[3].Value.ToString(), 
dataGridView1.SelectedRows[0].Cells[4].Value.ToString(), 
dataGridView1.SelectedRows[0].Cells[5].Value.ToString(), 
dataGridView1.SelectedRows[0].Cells[6].Value.ToString() 
); 
coo.Show(); 
} 
相關問題