2011-02-07 25 views
35

DataGridView में किसी भी निर्दिष्ट सेल पर फ़ोकस कैसे सेट करें? मैं फोकस (rowindex, columnindex) जैसे एक साधारण तरीके की उम्मीद कर रहा था लेकिन यह इतना आसान नहीं है।डेटाग्रिड व्यू - एक विशिष्ट सेल पर ध्यान केंद्रित करें

+0

इस एक बाहर की कोशिश करो ..... http://stackoverflow.com/questions/20822270/canceledit-does-not-keep-focus-on-edited-cell-in-datagridview-c -शर्प –

उत्तर

64

काम नहीं करेगा सेट की तरह वर्तमान सेल:

DataGridView1.CurrentCell = DataGridView1.Rows[rowindex].Cells[columnindex] 

या

DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5) 

और आप सीधे द्वारा संपादन के साथ ध्यान केंद्रित कर सकते हैं:

dataGridView1.BeginEdit(true) 
+9

CurrentCell काम किया, लेकिन DataGridView1.Item (1, 5) नहीं किया था। मुझे करना था: dataGridView1.CurrentCell = dataGridView1.Rows [rowindex] .Cells [columnindex]। –

11

आप एकाधिक चयन से बचने के लिए सही

dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true; 

को Selected संपत्ति की स्थापना करके एक विशिष्ट Cell को Focus सेट कर सकते हैं बस

dataGridView1.MultiSelect = false; 
5

सेट DataGridView के साथ समस्या यह है कि यह पहली पंक्ति को स्वतः चयन है तो आप

grvPackingList.ClearSelection(); 
dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true; 
द्वारा चयन को साफ़ करना चाहते हैं

अन्य बुद्धिमान यह

-1
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
    { 
     int row = e.RowIndex; 
     int col = e.ColumnIndex; 
     if (row < 0 || col != 3) 
      return; 
     if (e.FormattedValue.ToString().Equals(String.Empty)) 
     { 
     } 

     else 
     { 
      double quantity = 0; 
      try 
      { 
       quantity = Convert.ToDouble(e.FormattedValue.ToString()); 
       if (quantity == 0) 
       { 
        MessageBox.Show("The quantity can not be Zero", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        e.Cancel = true; 
        return; 
       } 
      } 
      catch 
      { 
       MessageBox.Show("The quantity should be decimal value.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       e.Cancel = true; 
       return; 
      } 
     } 
    } 
+1

यह प्रश्न के लिए एक वैध समाधान नहीं है। – Tizz

0

DataGridView1.CurrentCell = DataGridView1.Item ("ColumnName", 5)

3

मुझे एक ही समस्या थी। मैंने कुछ कॉलम छुपाए हैं और बाद में मैंने पहली पंक्ति का चयन करने की कोशिश की। यह वास्तव में ऐसा नहीं किया काम:

datagridview1.Rows[0].Selected = true; 

तो मैं cell[0,0] का चयन करने की कोशिश की, लेकिन यह भी काम नहीं किया, क्योंकि इस सेल प्रदर्शित नहीं किया गया था। अब मेरा अंतिम समाधान बहुत अच्छी तरह से काम कर रहा है:

datagridview1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
datagridview1.CurrentCell = datagridview1.FirstDisplayedCell; 

तो यह पूरी पहली पंक्ति का चयन करता है।

1
public void M(){ 
    dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0]; 
    dataGridView1.CurrentCell.Selected = true; 
    dataGridView1.BeginEdit(true); 
} 
+2

व्याख्या करने की देखभाल? – SMUsamaShah

0
घटना Form_Load में

(वस्तु प्रेषक, EventArgs ई) की कोशिश इस

dataGridView1.CurrentCell = dataGridView1.Rows [dataGridView1.Rows.Count1] .Cells [0];

इस कोड को अंतिम पंक्ति और 1 सेल

-1

बस सरल पेस्ट और दर्रा Gridcolor() किसी भी जहां आप चाहते हैं पर ध्यान केंद्रित कर सकते हैं।

Private Sub Gridcolor() 
    With Me.GridListAll 
     .SelectionMode = DataGridViewSelectionMode.FullRowSelect 
     .MultiSelect = False 
     '.DefaultCellStyle.SelectionBackColor = Color.MediumOrchid 
    End With 
End Sub 
+2

स्टैक ओवरफ़्लो में आपका स्वागत है! हालांकि यह कोड स्निपेट प्रश्न हल कर सकता है, [एक स्पष्टीकरण सहित] (// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) वास्तव में आपकी पोस्ट की गुणवत्ता में सुधार करने में मदद करता है।याद रखें कि आप भविष्य में पाठकों के लिए प्रश्न का उत्तर दे रहे हैं, और वे लोग आपके कोड सुझाव के कारणों को नहीं जानते हैं। स्पष्टीकरण टिप्पणियों के साथ अपने कोड को भीड़ न करने का प्रयास करें, इससे कोड और स्पष्टीकरण दोनों की पठनीयता कम हो जाती है! – kayess

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