回答
你可以像這樣....
這裏有兩種方法可以做到這一點:
- 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;
}
雖然第二種方法不顯示控件,但當單元格滾動時它保持靜態 - 是否有任何方法將新控件鎖定到基礎單元格? – CrazyHorse
第一種方法運行良好,第二種方法導致控件在滾動時「掉出」網格。我想你可以使用格式化事件再移回去。 –
的DataGridViewButtonColumn
是包含一個可點擊的按鈕提供的列類型。您可以添加自己的控件到細胞:
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
請記住它並不總是微不足道的,但我看到有人把整個DataGridView
進入細胞 - 看着怪異。
也有其他規定列:
DataGridViewButtonColumn
DataGridViewCheckBoxColumn
DataGridViewComboBoxColumn
DataGridViewImageColumn
DataGridViewLinkColumn
DataGridViewTextBoxColumn
您可以在Visual Studio的設計師列編輯器改變這些,或代碼將它們添加到Columns集合。
我會在設計中添加它,使模板領域。簡單的方法去把你想要的任何東西在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>
不幸的是,這個問題是關於winforms,而不是.NET的網頁。 – Zorgarath
- 1. 在datagridview中添加按鈕列
- 2. 向datagridView中的某些單元添加按鈕
- 3. 如何使用按鈕向DataGridView添加新行
- 4. 向DataGridView添加行
- 5. 向頁面添加按鈕
- 6. 向UIPickerView添加按鈕
- 7. 向塊添加按鈕
- 8. GWT - 向HighCharts添加按鈕
- 9. 如何將行添加到datagridview winforms?
- 10. 將組合框添加到datagridview C#Winforms
- 11. 在C#DataGridView(Winforms)中添加Combox
- 12. 將按鈕列添加到C#或VB.NET中的DataGridView的右側
- 13. 向DataGridView添加新行C#
- 14. 向datagridview添加列usercontrol
- 15. C# - 向DataGridView添加新行
- 16. DataGridView按鈕
- 17. 向Weebly添加隨機頁面按鈕
- 18. 向jQuery添加刷新按鈕Datatable
- 19. 使用ActionBarSherlock向ActionBar添加按鈕
- 20. 向W D儀表板添加按鈕
- 21. 向後退按鈕添加操作
- 22. 向按鈕添加/刪除類
- 23. 向UINavigationController添加標題+按鈕
- 24. 向按鈕添加事件監聽器
- 25. 向按鈕添加自動佈局
- 26. 向按鈕添加自定義功能
- 27. 向單選按鈕添加標籤
- 28. 向gwt添加更多按鈕Suggest Box
- 29. 如何向Eclipse IDE添加新按鈕
- 30. WPF:向按鈕添加觸發器
http://stackoverflow.com/questions/6085930/add-button-column-in-a-databound-datagridview –