2017-11-02 4 views
0

나는 열을 추가 한 Datagridview을 가지고 있으며 양식을 실행할 때 데이터를 추가하려고합니다. 문제는 행 다음에 null 행이 자동으로 추가 된 다음 오류가 표시되므로 null 행을 피하려면 어떻게해야합니까? 내 코드는 빈 행이 당신의 DataGridView를 하단에서데이터베이스에 삽입되지 않은 datagridview의 null 행

foreach (DataGridViewRow row in dataGridView1.Rows) 
     {   
     string batchid =row.Cells["batchid"].Value.ToString(); 
     string unitid = row.Cells["unitid"].Value.ToString(); 
     string unitmf = row.Cells["unitmf"].Value.ToString(); 
     string barcode = row.Cells["barcode"].Value.ToString(); 
     string expmonth = row.Cells["expmonth"].Value.ToString(); 
     string expyear = row.Cells["expyear"].Value.ToString(); 
     string expdays = row.Cells["expdays"].Value.ToString(); 
     string purchaserate=row.Cells["purchaserate"].Value.ToString();    
     string openingstock = row.Cells["openingstock"].Value.ToString(); 
     string rate1 = row.Cells["rate1"].Value.ToString(); 
     string rate2 = row.Cells["rate2"].Value.ToString(); 
     string rate3 = row.Cells["rate3"].Value.ToString(); 
     string rate4 = row.Cells["rate4"].Value.ToString(); 
     string dc ="INSERT INTO itemstock(main_id, comp_id, firm_id, trans_id, user_id, computer_id,item_id, batch_id, bar_code, unit_id, unit_mf, salesrate1, salesrate2, salesrate3, salesrate4, exp_days, exp_month, exp_year, purch_rate, current_stock, costbase, lastcost, opening_cost, opening_stock)VALUES(1,1,1,1,1,1,'" + itemid + "','" + batchid + "','"+barcode+ "','" + unitid + "','" + unitmf + "','" + rate1 + "','" + rate2 + "','" + rate3 + "','" + rate4 + "','" + expdays + "','" + expmonth + "','" + expyear + "','" + purchaserate + "','" + openingstock + "','" + txtcurrentcost.Text + "','" + txtlastcost.Text + "','" + txtopeningcost.Text + "','" + openingstock + "',)"; 
     } 
+0

어떤 오류는 어떻게해야합니까 ? null 객체에서 ToString()을 호출하려고했기 때문입니까? 그렇다면'ToString()'을 호출하기 전에 속성이 설정되었는지 (null이 아닌지) 확인하십시오. – Garre

답변

0

입니다 ...이 같은 ... 당신의 루프에서 해당 행을 건너 뛸 수 있습니다

foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    if (row.IsNewRow) continue; 
    ...