WinForms: How to add any control to a DataGrid column
-
Hello, I have been trying to add a custom control to one of the columns in my DataGrid, but I have failed so far. What I want to do is to add my custom control (or any other standard control for that matter) to the control collection of a certain column style. When I do so, the control will not appear until I click on any of the cells, which is supposed to show the control. Is it even possible to add controls to the control collection of textbox column styles, considering what I want to do? If you have any ideas, please use my sample code below as a starting point and reply to me with a modified version of it. Also, I am curious to know what causes this kind of weird behaviour. I would have expected the button to appear right away, but that's obviously not the case :wtf:. Does anyone have extremely detailed knowledge about how and when controls are painted? I would like to know the exact chain of events that causes this behaviour and why. Thank you for your patience, I hope you can help!! (I wont settle for a workaround ;))
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; DataGrid grid = new DataGrid(); DataTable table; public Form1() { InitializeComponent(); this.Controls.Add( this.grid ); // create the table this.table = new DataTable("A_Table"); this.table.Columns.Add( new DataColumn("TextColumn", typeof(string)) ); this.table.Columns.Add( new DataColumn("ButtonColumn", typeof(string)) ); // bind the table to the grid DataSet dataSet = new DataSet(); dataSet.Tables.Add( this.table ); this.grid.SetDataBinding(dataSet, "A_Table"); // set up column style... DataGridTableStyle tStyle = new DataGridTableStyle(); tStyle.MappingName = "A_Table"; // ...for the left column DataGridTextBoxColumn textStyle = new DataGridTextBoxColumn(); textStyle.MappingName = "TextColumn"; textStyle.HeaderText= table.Columns[0].Caption; textStyle.Width = 200; textStyle.ReadOnly = true; tStyle.GridColumnStyles.Add(textStyle); // ...for the right column textStyle = new DataGridTextBoxColumn(); textStyle.MappingName = "ButtonColumn"; textStyle.HeaderText= table.Columns[1].Captio