2013-08-01 23 views
9

Kodumu çalıştırdığımda dataGridView TopLeftHeaderCell'in de bir combobox'ı var. Bunu nasıl değiştirebilirim?DataGridView Üstbilgileri'ne Combobox Ekleme

İşte benim kod:

comboBoxHeaderCell.Location = dataGridView.GetCellDisplayRectangle(i, -1, true).Location; 

hep 0,0 dönecektir,

public void AddHeaders(DataGridView dataGridView) 
{ 

     for (int i = 0; i < 4; i++) 
     { 
      // Create a ComboBox which will be host a column's cell 
      ComboBox comboBoxHeaderCell = new ComboBox(); 
      comboBoxHeaderCell.DropDownStyle = ComboBoxStyle.DropDownList; 
      comboBoxHeaderCell.Visible = true; 

      foreach (KeyValuePair<string, string> label in _labels) 
      { 
       comboBoxHeaderCell.Items.Add(label.Key); 
      } 

      // Add the ComboBox to the header cell of the column 
      dataGridView.Controls.Add(comboBoxHeaderCell); 
      comboBoxHeaderCell.Location = dataGridView.GetCellDisplayRectangle(i, -1, true).Location; 
      comboBoxHeaderCell.Size = dataGridView.Columns[0].HeaderCell.Size; 
      comboBoxHeaderCell.Text = _labels[i].Key; 

     } 
} 

size

Kodunuza

cevap

1

ederiz ve bunun sen koymak senin DataGridView konumu 0,0 de ComboBox, ve bu yüzden bu

'u görüyoruz

enter image description here

size gereken boyut için dataGridView1[i,0].size kullanabilirsiniz

i o bulamadık konumu

arıyorum ama ne yapabilirim kullanabilirsiniz dataGridView1.Width - dataGridView1[1,0].Size.Width kullanıyor genişliğini genişletin ve tüm üstbilgi genişliklerinin boyutunu kaldırın ve ardından bunları tek tek ekleyin.

int xPos = dataGridView1.Width; 

for (int i = 0; i < 4; i++) 
{ 
    xPos -= dataGridView1[i, 0].Size.Width; 
} 
... 
comboBoxHeaderCell.Size = dataGridView.Columns[0].HeaderCell.Size; 
comboBoxHeaderCell.Location = new Point(xPos, 0); 
xPos += comboBoxHeaderCell.Size.Width; 
+0

Ve çözüm? OP, her bir sütun başlığına her bir combobox'ı eklemek istemektedir, sadece onun kodunun neden çalışmadığını açıklamamaktadır. –

+0

Hiç bir çözümüm yok çünkü hala neden bunu yapacağını bilmiyorum. ne yapmak istediğini açıklarsa, –

+0

yardım edebilirim Her başlık için bir combobox'a ihtiyacım var, topLeftHeaderCell – user2576562

0
public void AddHeaders(DataGridView dataGridView) 
{ 

    for (int i = 0; i < 4; i++) 
    { 
     // Create a ComboBox which will be host a column's cell 
     DataGridViewComboBoxCell comboBoxHeaderCell = new DataGridViewComboBoxCell();   


     foreach (KeyValuePair<string, string> label in _labels) 
     { 
      comboBoxHeaderCell.Items.Add(label.Key); 
     } 

     // Add the ComboBox to the header cell of the column 
     dataGridView[i, 0] = comboBoxHeaderCell; 
     comboBoxHeaderCell.Value =_labels[i].Key; 


    } 
} 

o sorununuzu çözecek bu deneyin, ben onlar görünür olacak varsayılan olarak olarak tutmak için zorunlu olmayan bu satırları kaldırıldı ... ve varsayılan olarak hücre boyutunu ...

alacak
+0

Bu indeks DataGridViewCell.Visible olduğunu söyleyen bir hata var, DataGridViewCell.Size, DataGridViewCell.Test, atanmamış, sadece okunabilir – user2576562