2011-05-08 8 views
6
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex > 1) 
    { 
     int cellValue = Convert.ToInt32(((DataGridViewCell)sender).Value); 

     if (cellValue < 20) 
     { 
      ((DataGridViewCell)sender).Value = 21; 
     } 
    } 
} 

मैं उस सेल के मूल्य को प्राप्त करने की कोशिश कर रहा हूं जिस से ईवेंट निकाल दिया गया था।मैं सेल_लेव ईवेंट से DataGridViewCell का मान कैसे प्राप्त कर सकता हूं?

एक अपवाद निकाल दिया जाता है जब मैं एक DataGridViewCell को sender कास्ट करने के लिए प्रयास करें: 'System.Windows.Forms

प्रकार ' System.Windows.Forms.DataGridView 'की वस्तु कास्ट करने में असमर्थ के प्रकार .DataGridViewCell '।

आप मुझे क्या सलाह देते हैं?

मैं अगर मूल्य 20 से भी कम है जांच करने की आवश्यकता है, और अगर यह होता है, यह 21

उत्तर

4

को टक्कर अप theDataGrid[e.RowIndex, e.ColumnIndex].Value के साथ काम करने की कोशिश करो। मैं उम्मीद करता हूं कि प्रेषक सेल की बजाय डेटाग्रिड व्यू ऑब्जेक्ट होने की अधिक संभावना है।

int cellValue = Convert.ToInt32(((DataGridView)sender).SelectedCells[0].Value); 
2

प्रेषक के प्रकार के रूप में सेल का मान प्राप्त कर सकते हैं, DataGridView है। क्या यह आपकी मदद कर सकता है।

+0

यह एक बहुत अच्छा जवाब है क्योंकि कोई कभी-कभी एक सामान्य घटना हैंडलर बनाता है। अर्थात्; इवेंट हैंडलर कई डेटाग्रिड व्यू की सेवा के लिए है। डुलिनी अतापट्टू ने प्रेषक पैरामीटर को DataGridView ऑब्जेक्ट के रूप में पूछकर यहां चाल की। – netfed

4
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
     { 
      if (dataGridView1.Rows[e.RowIndex].Cells[1].Value != null) 
      { 
       int cellmarks = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); 
       if (cellmarks < 32) 
       { 
        dataGridView1.Rows[e.RowIndex].Cells[2].Value = "Fail"; 
       } 
       else 
       { 
        dataGridView1.Rows[e.RowIndex].Cells[2].Value = "Pass"; 
       } 

      } 
     } 

इस कोड currentcell मूल्य प्राप्त होगा: तो आप निम्न पंक्ति का उपयोग कर सकते

0

मैंने एक _CellClick घटना के लिए थोड़ा सा संस्करण किया।

private void Standard_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
    if (e.RowIndex >= 0) 
    { 
     int intHeaderId = 0; 
     switch (((DataGridView)sender).Columns[e.ColumnIndex].Name) 
     { 
      case "grcHeaderId": 
       intHeaderId = (int)grvEnteredRecords[grcHeaderId.Index, e.RowIndex].Value; 
       break; 
... 
संबंधित मुद्दे

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