2013-08-01 53 views
9

Khi tôi chạy mã của mình, dataGridView TopLeftHeaderCell cũng có một combobox. Làm thế nào tôi có thể thay đổi điều đó?Thêm Hộp tổ hợp vào Tiêu đề DataGridView

Dưới đây là mã của tôi:

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; 

     } 
} 

Cảm ơn bạn

Trả lời

1

tại mã của bạn,

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

sẽ luôn luôn trả 0,0, và do bạn đặt ComboBox tại địa điểm 0,0 trong DataGridView của bạn, và đó là lý do chúng tôi thấy điều này

enter image description here

bạn có thể sử dụng dataGridView1[i,0].size cho kích thước cần thiết

tôi đang tìm vị trí

tôi không thể thấy, nhưng những gì bạn có thể làm được bằng cách sử dụng dataGridView1.Width - dataGridView1[1,0].Size.Width bạn có thể sử dụng chiều rộng, và loại bỏ kích thước của tất cả các chiều rộng tiêu đề, và sau đó thêm chúng từng cái một.

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

Và giải pháp? OP dường như muốn thêm từng combobox vào mỗi columnheader, không đơn giản giải thích tại sao mã của anh ta không hoạt động. –

+0

tôi không có giải pháp vì tôi vẫn không biết tại sao anh ấy sẽ làm điều đó. nếu anh ta sẽ giải thích những gì anh ta muốn làm tôi sẽ có thể giúp –

+0

Tôi cần một combobox cho mỗi tiêu đề trong cột, exept the 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; 


    } 
} 

thử này nó sẽ giải quyết vấn đề của bạn, tôi loại bỏ những dòng họ không bắt buộc phải giữ như theo mặc định nó sẽ được hiển thị ... và theo mặc định nó sẽ mất kích thước tế bào ...

+0

Tôi có lỗi nói rằng chỉ mục DataGridViewCell.Visible, DataGridViewCell.Size, DataGridViewCell.Test, không được chỉ định, nó chỉ đọc – user2576562

Các vấn đề liên quan