Stupid question, but annoying nonetheless: I am trying to add dropdown boxes to a datagrid control. I have successfully gotten them added, but the rows do not show the selected value. I try updating the DataTable, but to no avail. Any ideas?
Ryan Ross
Code:
private System.Windows.Forms.DataGrid CatalogLanguages;
private System.Windows.Forms.DataGridTextBoxColumn CatalogLanguagesComboBoxColumn;
private DataGrid.HitTestInfo CatalogLanguagesHitTest;
private System.Data.DataTable Languages;
private System.Windows.Forms.ComboBox CatalogLanguagesComboBox;
private void NewCatalog_Load(object sender, System.EventArgs e)
{
Languages = new System.Data.DataTable();
Languages.Columns.Add(new System.Data.DataColumn("Language"));
Languages.Columns.Add(new System.Data.DataColumn("Display Name"));
CatalogLanguages.DataSource = Languages;
if(!CatalogLanguages.TableStyles.Contains("Language"))
{
//Create a DataGridTableStyle object
DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
//Set its properties
dgdtblStyle.MappingName = Languages.TableName;//its table name of dataset
CatalogLanguages.TableStyles.Add(dgdtblStyle);
dgdtblStyle.RowHeadersVisible = false;
dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;
dgdtblStyle.AllowSorting = false;
dgdtblStyle.HeaderBackColor = Color.Navy;//.FromArgb(8,36,107);
dgdtblStyle.HeaderForeColor = Color.White;
dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
dgdtblStyle.GridLineColor = Color.DarkGray;
dgdtblStyle.PreferredRowHeight = 22;
CatalogLanguages.BackgroundColor= Color.DarkGray;
//Take the columns in a GridColumnStylesCollection object and set //the size of the
//individual columns
GridColumnStylesCollection colStyle;
colStyle = CatalogLanguages.TableStyles[0].GridColumnStyles;
colStyle[0].Width = 220;
colStyle[1].Width = 220;
//colStyle[2].Width = 150;
}
CatalogLanguagesComboBoxColumn = (DataGridTextBoxColumn)CatalogLanguages.TableStyles[0].GridColumnStyles[0];
CatalogLanguagesComboBoxColumn.TextBox.GotFocus += new EventHandler(this.CLCBC_GotFocus);
//CatalogLanguagesComboBoxColumn.TextBox.LostFocus += new EventHandler(this.CLCBC_LostFocus);
}
private void CLCBC_GotFocus(object o, EventArgs e)
{
CatalogLanguagesComboBox = new ComboBox();
CatalogLanguagesComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
CatalogLanguagesComboBox.Cursor = System.Windows.Forms.Cursors.Arrow;
CatalogLanguagesComboBox.Dock = DockStyle.Fill;
CatalogLanguagesComboBox.Items.AddRange(new string[5]{"","Information Technology","Computer Science","Bio Technology","Electrical Engg"});
for(int i = 0; i < Languages.Rows.Count; i++)
{
CatalogLanguagesComboBoxColumn.TextBox.Controls.Add(CatalogLanguagesComboBox);
CatalogLanguagesComboBox.BringToFront();
CatalogLanguagesComboBox.SelectedValueChanged += new EventHandler(this.CLCB_LostFocus);
}
}
private void CLCB_LostFocus(object o, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(CatalogLanguagesComboBox.SelectedItem);
//System.Diagnostics.Debug.WriteLine(Languages.Rows.Count);
//System.Diagnostics.Debug.WriteLine(CatalogLanguagesHitTest.Row);
for(int i = 0; i < Languages.Rows.Count; i++)
{
if(CatalogLanguagesHitTest.Row == i)
{
//System.Diagnostics.Debug.WriteLine(i);// = CatalogLanguagesComboBox.SelectedItem;
Languages.Rows[i].ItemArray[0] = "Here";
//Languages.AcceptChanges();
}
}
}
private void CatalogLanguages_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CatalogLanguagesHitTest = CatalogLanguages.HitTest(e.X,e.Y);
}