2012-09-28 14 views
31

में डेटाग्रिडव्यू में संख्यात्मक मान स्वीकार करें मुझे डेटाग्रिडव्यू बनाने की आवश्यकता है जो केवल कुंजीपटल ईवेंट में विशिष्ट कॉलम के लिए संख्यात्मक मान स्वीकार करे। क्या ऐसा करने का कोई अच्छा तरीका है?एक विशिष्ट कॉलम केवल कुंजीपटल ईवेंट

उत्तर

53
  • EditingControlShowing
  • की एक घटना जोड़ें EditingControlShowing में, जाँच करें कि यदि वर्तमान सेल वांछित स्तंभ में निहित है।
  • संपादन नियंत्रण में कुंजीपटल की एक नई घटना पंजीकृत करें (यदि ऊपर की स्थिति सही है)।
  • EditingControlShowing में पहले जोड़े गए किसी भी कुंजीपटल ईवेंट को हटाएं।
  • कुंजीपटल ईवेंट में, जांचें कि कुंजी कुंजी नहीं है तो इनपुट रद्द करें।

उदाहरण:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress); 
    if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column 
    { 
     TextBox tb = e.Control as TextBox; 
     if (tb != null) 
     { 
      tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress); 
     } 
    } 
} 

private void Column1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) 
    { 
     e.Handled = true; 
    } 
} 
+3

मैं की आलोचना नहीं कर रहा हूँ यह उत्तर, लेकिन एक सामान्य टिप्पणी बताते हुए: क्या यह अनुमति नहीं देनी चाहिए कि कॉलम पर किसी प्रकार का फॉर्मेटर सेट करें, जैसे कि 'AllowNumericOnly' या तो'। एक संपत्ति की तरह। –

+4

उत्कृष्ट जवाब! यह उन्हें अमान्य डेटा टाइप करने के रूप में इतना से बचाता है। हालांकि मैं जोड़ना चाहता हूं कि हार्ड कोडित कॉलम इंडेक्स का उपयोग करना एक अच्छा विचार नहीं है। मैं 'if (dataGridView1.CurrentCell.ColumnIndex == DataGridView1.Columns [" name "] का उपयोग करने की अनुशंसा करता हूं। इंडेक्स)' –

+0

@druciferre भी बेहतर: 'dataGridView1.CurrentCell.ColumnIndex == DataGridView.Columns.IndexOf (DataGridViewColumn1);' –

25

आप DataGridView.CellValidating Event इस तरह का उपयोग करना चाहिए:

private void dataGridView1_CellValidating(object sender, 
              DataGridViewCellValidatingEventArgs e) 
    { 
     if (e.ColumnIndex == 1) // 1 should be your column index 
     { 
      int i; 

      if (!int.TryParse(Convert.ToString(e.FormattedValue), out i)) 
      { 
       e.Cancel = true; 
       label1.Text ="please enter numeric"; 
      } 
      else 
      { 
       // the input is numeric 
      } 
     } 
    } 
3
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress); 
     if (dataGridView1.CurrentCell.ColumnIndex == 4) //Desired Column 
     { 
      TextBox tb = e.Control as TextBox; 
      if (tb != null) 
      { 
       tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress); 
      } 
     } 

    } 
    private void Column1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
      // allowed only numeric value ex.10 
     //if (!char.IsControl(e.KeyChar) 
     // && !char.IsDigit(e.KeyChar)) 
     //{ 
     // e.Handled = true; 
     //} 

       // allowed numeric and one dot ex. 10.23 
     if (!char.IsControl(e.KeyChar)&& !char.IsDigit(e.KeyChar) 
      && e.KeyChar != '.') 
     { 
      e.Handled = true; 
     } 

     // only allow one decimal point 
     if (e.KeyChar == '.' 
      && (sender as TextBox).Text.IndexOf('.') > -1) 
     { 
      e.Handled = true; 
     } 
    } 
1
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl 

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing 

txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl) 
End Sub 

Private Sub txtNumeric_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumeric.KeyPress 
    If (DataGridView1.CurrentCell.ColumnIndex > 0) Then 
     If (Not Char.IsControl(e.KeyChar) And Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = ".") Then 
      e.Handled = True 
     Else 
      'only allow one decimal point 
      If (e.KeyChar = "." And txtNumeric.Text.Contains(".")) Then 
       e.Handled = True 
      End If 
     End If 
    End If 
End Sub 
+0

अपने उत्तर को सही तरीके से प्रारूपित करें – HaveNoDisplayName

2

जवाब दिया उत्कृष्ट जब तक आप दशमलव स्थानों की आवश्यकता के रूप में अन्य लोगों ने बताया है। इस घटना में आप और, सत्यापन का विस्तार का उपयोग कर जोड़ने की जरूरत के लिए मान्यता का विस्तार दशमलव विभाजक

using System.Globalization; 

NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; 
char decSeperator; 

decSeperator = nfi.CurrencyDecimalSeparator[0]; 

के लिए एक संस्कृति चर मूल्य प्राप्त करने के लिए नीचे दिए गए वार्स:

if (!char.IsControl(e.KeyChar) && !(char.IsDigit(e.KeyChar) 
| e.KeyChar == decSeperator)) 
{ 
    e.Handled = true; 
} 
// only allow one decimal point 
if (e.KeyChar == decSeperator 
    && (sender as TextBox).Text.IndexOf(decSeperator) > -1) 
{ 
    e.Handled = true; 
} 
+0

मुझे विस्तारित सत्यापन पसंद है। मैंने देखा कि यद्यपि दशमलव वर्ण को वैध कुंजीचर के रूप में कभी स्वीकार नहीं किया गया था क्योंकि KeyPressEventArgs टेक्स्टबॉक्स में पहले दशमलव को दर्ज करने पर भी सत्य लौटा। मैंने और कहा, अगर (ई। केकर == decSeparator) {e.Handled = false; } 'इंडेक्सऑफ स्थिति के हिस्से के रूप में। धन्यवाद – voidmain

+0

ऑब्जेक्ट संदर्भ किसी ऑब्जेक्ट के उदाहरण पर सेट नहीं है। –

संबंधित मुद्दे