2011-10-27 130 views
8

有沒有辦法在C#中的Winforms DataGridView單元格中添加控件(例如按鈕)?向Winforms DataGridView添加按鈕

(是我的目標是在把各種控制在網格的不同細胞...)

+0

http://stackoverflow.com/questions/6085930/add-button-column-in-a-databound-datagridview –

回答

6

你可以像這樣....

這裏有兩種方法可以做到這一點:

  • 1)。將DataGridViewCell投射到存在的某個單元格類型。例如,對於 示例,將DataGridViewTextBoxCell轉換爲 DataGridViewComboBoxCell類型。 2)。創建一個控件並將其添加到DataGridView的控件集合中,將其位置和大小設置爲適合主機爲 的單元格。

查看我的示例代碼,下面說明了這些技巧。

代碼段

private void Form5_Load(object sender, EventArgs e) 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("name"); 
     for (int j = 0; j < 10; j++) 
     { 
      dt.Rows.Add(""); 
     } 
     this.dataGridView1.DataSource = dt; 
     this.dataGridView1.Columns[0].Width = 200; 

     /* 
     * First method : Convert to an existed cell type such ComboBox cell,etc 
     */ 

     DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell(); 
     ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" }); 
     this.dataGridView1[0, 0] = ComboBoxCell; 
     this.dataGridView1[0, 0].Value = "bbb"; 

     DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell(); 
     this.dataGridView1[0, 1] = TextBoxCell; 
     this.dataGridView1[0, 1].Value = "some text"; 

     DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell(); 
     CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
     this.dataGridView1[0, 2] = CheckBoxCell; 
     this.dataGridView1[0, 2].Value = true; 

     /* 
     * Second method : Add control to the host in the cell 
     */ 
     DateTimePicker dtp = new DateTimePicker(); 
     dtp.Value = DateTime.Now.AddDays(-10); 
     //add DateTimePicker into the control collection of the DataGridView 
     this.dataGridView1.Controls.Add(dtp); 
     //set its location and size to fit the cell 
     dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location; 
     dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size; 
    } 
+1

雖然第二種方法不顯示控件,但當單元格滾動時它保持靜態 - 是否有任何方法將新控件鎖定到基礎單元格? – CrazyHorse

+0

第一種方法運行良好,第二種方法導致控件在滾動時「掉出」網格。我想你可以使用格式化事件再移回去。 –

6

DataGridViewButtonColumn是包含一個可點擊的按鈕提供的列類型。您可以添加自己的控件到細胞:

http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

請記住它並不總是微不足道的,但我看到有人把整個DataGridView進入細胞 - 看着怪異。

也有其他規定列:

DataGridViewButtonColumn 
DataGridViewCheckBoxColumn 
DataGridViewComboBoxColumn 
DataGridViewImageColumn 
DataGridViewLinkColumn 
DataGridViewTextBoxColumn 

您可以在Visual Studio的設計師列編輯器改變這些,或代碼將它們添加到Columns集合。

+0

啊,所以你說我可以添加任何類型的控件到DataGridViewButtonColumn中?當我偶然發現這一點時,我認爲它是一個充滿按鈕的列;-) – Boris

+0

@Boris不,不需要按鈕列,您可以創建一個列類型來託管任何控件並將該列類型添加到網格中。例如,您可能會創建一個承載NumericUpDown控件的DataGridViewNumericUpDownColumn。該列被編碼爲託管一個特定的控制類型,這不是一個運行時決定。 –

+0

但我認爲還存在一個誤區:採用這種方法,整列是否得到相同類型的控制?因爲我想要的是每個細胞都有不同的控制。 – Boris

-1

我會在設計中添加它,使模板領域。簡單的方法去把你想要的任何東西在DataGridView

<asp:DataGrid ID="somedatagrid" runat="server"> 
    <Columns> 
     <asp:TemplateColumn> 
      <ItemTemplate> 
       <asp:Button ID="somebutton" runat="server" Text="some button" /> 
      </ItemTemplate> 
     </asp:TemplateColumn> 
    </Columns> 
</asp:DataGrid> 
+0

不幸的是,這個問題是關於winforms,而不是.NET的網頁。 – Zorgarath