2014-11-04 13 views
6

DataGridView numaralı telefona veri girmeniz gereken basit bir C# uygulamasına sahibim. Sütunlar için null değerler veya sayısal olmayan giriş gibi bazı doğrulayıcılar uyguladık. Bir düğmeye basıldıktan sonra kontrolleri foreach (DataGridViewRow row in dataGridView1.Rows) {...}Bir döngü içinde DataGridView'e otomatik olarak eklenen boş satır nasıl atlanır?

tarafından basıldığında, bunun otomatik olarak eklendiği ve boş olduğu halde DataGridView'un son satırını da doğrulamaya çalıştığı sorunuyla karşılaşıyorum. Yani ben

private void button1_Click(object sender, EventArgs e) 
{ 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     string inputItemNr; 
     string inputMHD; 
     string inputCharge; 
     string inputSupplNr; 
     string inputPrnCnt; 
     UInt32 itemnr; 
     DateTime mhd; 
     string mhdFormat = "yyMMdd"; 
     string batch; 
     byte prncnt; 

     if (row.Cells[0].Value == null) 
     { 
      MessageBox.Show("Enter item number"); 
      return; 
     } 
     else 
     { 
      inputItemNr = row.Cells[0].Value.ToString(); 
     } 

     if (!UInt32.TryParse(inputItemNr, out itemnr)) 
     { 
      MessageBox.Show("Incorrect item number: " + inputItemNr); 
      return; 
     } 

     if (row.Cells[1].Value == null) 
     { 
      MessageBox.Show("Enter MHD"); 
      return; 
     } 
     else 
     { 
      inputMHD = row.Cells[1].Value.ToString(); 
     } 

     if (!DateTime.TryParseExact(inputMHD, mhdFormat, CultureInfo.InvariantCulture, 
      DateTimeStyles.None, out mhd)) 
     { 
      MessageBox.Show("Incorrect MHD: " + inputMHD); 
      return; 
     } 

     if (row.Cells[2].Value == null) 
     { 
      inputCharge = DateTime.Now.ToString("yyMMdd"); 
     } 
     else 
     { 
      inputCharge = row.Cells[2].Value.ToString(); 
     } 

     if (row.Cells[3].Value == null) 
     { 
      batch = inputCharge; 
     } 
     else 
     { 
      inputSupplNr = row.Cells[3].Value.ToString(); 
      batch = inputCharge + " " + inputSupplNr; 
     } 

     if (row.Cells[4].Value == null) 
     { 
      inputPrnCnt = "1"; 
     } 
     else 
     { 
      inputPrnCnt = row.Cells[4].Value.ToString(); 
     } 

     if (!byte.TryParse(inputPrnCnt, out prncnt)) 
     { 
      MessageBox.Show("Incorrect print count: " + inputPrnCnt); 
      return; 
     } 
    } 
} 

yardım edin ... Burada bir döngü içinde sıkışmış duyuyorum.

sayesinde

cevap

14

Sen sıranın IsNewRow özelliğini kullanabilirsiniz:

foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    if (row.IsNewRow) continue; 
    // rest of your loop body ... 
} 
+0

eser, teşekkür ederim! – Geo

+0

Bu bilgi, Google’da doğru bir şekilde ifade edilmedikçe şaşırtıcı bir şekilde zordu; Cevabın ilgimi çekene kadar bu mülkün hiçbir fikri yoktu. Bu açık olmayan ama şaşırtıcı derecede basit ipucu paylaştığınız için çok teşekkürler. –

İlgili konular