2010-01-14 5 views
2

System.Windows.Forms.Button 컨트롤을 보유하기 위해 사용자 지정 열 (DataGridViewButtonControlColumn) 및 셀 (ButtonControlCell) 클래스를 만들었습니다. 단추가 열에 추가되고 올바르게 표시됩니다. 단추를 ButtonControlCell의 값으로 설정하기 전에 "Click"에 대한 이벤트 처리기를 연결합니다. 그러나이 핸들러는 버튼을 클릭 할 때 호출되지 않습니다.단추 클릭 이벤트가 사용자 지정 DataGridViewCell에서 실행되지 않음

재정의 된 페인트 기능의 DataGridView 컨트롤에 단추를 추가합니다.

DataGridView에 Button을 등록하기 위해 따라야하는 특정 단계가 있습니까?

코드 : 구현에서

public class ButtonControlCell : DataGridViewTextBoxCell 
    { 
. 
. 
. 
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) 
     { 
      base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); 

      if (btnVal != null) 
      { 
       btnVal.Size = new System.Drawing.Size(80, 20); 
       btnVal.Location = cellBounds.Location; 
       this.DataGridView.Controls.Add(btnVal); 
      }    
     } 
. 
. 
. 
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) 
     { 
      // This is not called when the button is clicked (which is correct I guess) 
      base.OnMouseClick(e); 

      if (btnVal != null) 
      { 
       btnVal.PerformClick(); 
      } 
     } 
. 
. 
} 

:

private void AddButtonCell(string sText, EventHandler oEh, DataGridViewButtonColumn oClm, DataGridView dgvParent, int iRow, int iColumn) 
     { 
      Button btnTemp = new Button(); 
      btnTemp.Height = 20; 
      btnTemp.Width = 60; 
      btnTemp.Anchor = AnchorStyles.Top; 
      btnTemp.Text = sText; 
      btnTemp.Click += new EventHandler(btnTemp_Click); 
      btnTemp.Tag = new Point(iRow, iColumn); 

      Controls.Add(btnTemp); 

      dgvParent.Rows[iRow].Cells[iColumn].Value = btnTemp; 
     } 

     void btnTemp_Click(object sender, EventArgs e) 
     { 
      Button btnSender = (Button)sender; 

      DataGridViewRow r = dgvResults.Rows[((Point)btnSender.Tag).X]; 

      TagInfo oRet = new TagInfo((string)r.Cells[iTitleColIndex].Value, (string)r.Cells[iArtistColIndex].Value, 
       (string)r.Cells[iAlbumColIndex].Value); 
      oRet.imgAlbumArt = (System.Drawing.Image)r.Cells[iArtColIndex].Tag; 
      oParent.TagWithInfo(oRet, true); 
     } 

답변

0

이 작업을 수행하는 데 사용되는 방법은 DataGrid에서 수행 된 이벤트 (예 : 버튼 클릭)를 잡아 내고 컨트롤을 가져 와서 그에 따라 추가 작업을 수행하는 것입니다. 사용자 정의 DataGridView에 대한 여러 자습서를 통해 답변을 찾았습니다.

0

당신의 목표는 단지 ​​사용자는 내장 된 DataGridViewButtonColumn (DataGridViewButtonColumn)를 사용하여 클릭 할 수있는 버튼이하는 경우.

+0

DataGridViewButtonColumn을 사용하고 있었지만 원하는대로 작동하지 않았습니다. 컨트롤이 셀을 채우기 위해 크기가 조정되고이를 막을 수있는 속성을 찾을 수 없습니다. 나는 임시로 DataGridViewLinkColumn을 사용하고 있는데, 클릭 이벤트를 발생시키는 방법을 알아낼 때까지 – Gayan

관련 문제