2013-01-05 5 views
5

में डेटाग्रिडव्यू सेल मानों के लिए मान्यताओं में मेरे पास एक विंडोज़ फॉर्म एप्लिकेशन है जिसमें एक डाटाग्रिडव्यू है। मुझे डेटाग्रिडव्यू कोशिकाओं पर सेल सत्यापन लागू करने की आवश्यकता है ताकि यह नकारात्मक मानों को स्वीकार न करे। मुझे msdn लाइब्रेरी से इसके लिए एक उपयुक्त कोड मिला।सी #

private void dataGridView1_CellValidating(object sender, 
DataGridViewCellValidatingEventArgs e) 
{ 
dataGridView1.Rows[e.RowIndex].ErrorText = ""; 
int newInteger; 

// Don't try to validate the 'new row' until finished 
// editing since there 
// is not any point in validating its initial value. 
if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; } 
if (!int.TryParse(e.FormattedValue.ToString(), 
    out newInteger) || newInteger < 0) 
{ 
    e.Cancel = true; 
    dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a Positive integer"; 
} 
} 

दुर्भाग्य कोड केवल अनुमति देता है पूर्णांकों DataGridView में दर्ज किए जाने की। चूंकि मेरे पास "आइटम नाम" के रूप में कॉलम नाम है, जिसे टेक्स्ट के रूप में दर्ज किया जाना चाहिए, कोड में कोई समस्या है। जब मैं आइटम के नाम पर टाइप करता हूं तो यह एक त्रुटि संदेश उत्पन्न करता है। शेष सेल सत्यापन पूरी तरह से काम कर रहे हैं! मैं कोड को कैसे संपादित करना चाहता हूं ताकि यह इस त्रुटि को उत्पन्न न करे?

अग्रिम

Mirfath

उत्तर

4

DataGridViewCellValidatingEventArgse धन्यवाद एक .ColIndex संपत्ति जो आप वाकई मान्यता केवल स्तंभ आप चाहते हैं पर किया जाता है करने के लिए जाँच कर सकते हैं।

private void dataGridView1_CellValidating(object sender, 
DataGridViewCellValidatingEventArgs e) { 
    if (e.ColIndex == dataGridView1.Columns["MyNumericColumnName"].Index) { 
     dataGridView1.Rows[e.RowIndex].ErrorText = ""; 
     int newInteger; 

     // Don't try to validate the 'new row' until finished 
     // editing since there 
     // is not any point in validating its initial value. 
     if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; } 
     if (!int.TryParse(e.FormattedValue.ToString(), 
      out newInteger) || newInteger < 0) 
     { 
      e.Cancel = true; 
      dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a Positive integer"; 
     } 
    } 
} 
0

इसे आजमाएं और इसे समझ लिया।

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
    { 
     if (dataGridView1.IsCurrentCellDirty) 
     { 
      if (e.ColumnIndex == 0) //<-Column for String 
      { 
       Console.WriteLine(dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString()); 
       //Your Logic here 
      } 
      if (e.ColumnIndex == 1) //<-Column for Integer 
      { 
       Console.WriteLine(dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString()); 
       //Your Logic here 
      } 
     } 
    }