2013-08-01 4 views
9

코드를 실행하면 dataGridView TopLeftHeaderCell에도 콤보 상자가 있습니다. 어떻게 바꿀 수 있습니까?DataGridView 머리글에 Combobox 추가

여기 내 코드입니다 :

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

항상 0,0 반환합니다

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; 

     } 
} 

당신에게

코드에서

답변

1

감사 및 그 당신이 넣어 당신의 DataGridView에 위치 0,0에서 ComboBox, 이것이 우리가 이것을 보는 이유입니다.

,451,515,

enter image description here

당신이 필요로하는 크기 dataGridView1[i,0].size을 사용할 수 있습니다

나는 그것을 찾을 수 없습니다 위치

찾고 있어요,하지만 당신이 할 수있는 것은 당신이를 사용할 수 dataGridView1.Width - dataGridView1[1,0].Size.Width 을 사용하고 있습니다 너비 및 모든 머리글 너비의 크기를 제거한 다음 하나씩 추가하십시오.

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

그리고 해결책은 무엇입니까? OP는 단순히 각 코드가 작동하지 않는 이유를 설명하는 것이 아니라 각 columnheader에 각 콤보 상자를 추가하려는 것 같습니다. –

+0

나는 왜 그럴 수 있을지 아직 모르니까 나는 해결책이 없다. 그는 그가 뭘 원하는지 설명 할게요 내가 도울 수있을거야 –

+0

난 각 열에있는 각 헤더에 대한 combobox가 필요합니다, 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; 


    } 
} 

이 문제를 해결할 것입니다 이것을 시도, 나는 그들이 그것을 볼 수 있습니다 기본적으로 유지하는 것이 필수 아닌 그 라인을 제거 ... 그리고 기본적으로는 셀 크기 ...

를 취할 것
+0

나는 그 색인 DataGridViewCell.Visible, DataGridViewCell.Size, DataGridViewCell.Test가 할당되지 않았습니다. 읽기 전용입니다. – user2576562