mark jones

mark jones

  • NA
  • 31
  • 0

Datagrid combobox

Feb 26 2009 6:53 PM

Ca someone please help me with the following problem.  I am trying to create a program with a datagrid that has a combobox in the current cell of the datagrid. I have tried creating columnstyles but to no avail. Please find the code I have created below

Any help would be very much appreciated

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CellProperty_Prog
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private CellProperty_Prog.DataSet1 dataSet11;
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private DataGridTextBoxColumn datagridtextBox;
private System.Windows.Forms.ComboBox comboControl;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataSet11 = new CellProperty_Prog.DataSet1();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "select dbo.[authors].* from dbo.[authors]";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=SHERMAN2;packet size=4096;integrated security=SSPI;data source=\"SH" +
"ERMAN2\\SQLEXPRESS\";attachdbfilename=\"C:\\Program Files\\Microsoft SQL Server\\MSSQL" +
".1\\MSSQL\\Data\\pubs.mdf\";persist security info=False";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("en-GB");
//
// dataGrid1
//
this.dataGrid1.DataMember = "Table";
this.dataGrid1.DataSource = this.dataSet11;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(32, 32);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(328, 248);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
this.dataGrid1.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.dataGrid1_HelpRequested);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 302);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{


comboControl = new ComboBox();
comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;
comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboControl.Dock = DockStyle.Fill;
comboControl.Items.AddRange(new string[8]{"","Horror","Action/Thriller","TV/Series","Sci-fi/Fantasy","Family","Drama","Comedy"});



sqlDataAdapter1.Fill(dataSet11);

}

private void dataGrid1_HelpRequested(object sender, System.Windows.Forms.HelpEventArgs hlpevent)
{

}

private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{

int column = this.dataGrid1.CurrentCell.ColumnNumber;
int row = this.dataGrid1.CurrentCell.RowNumber;


DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "authors";
this.dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(ts);


DataGridTextBoxColumn style1 = new DataGridTextBoxColumn();
style1.TextBox.Controls.Add(comboControl);
style1.MappingName ="au_ID";
style1.HeaderText= "Author ID";
ts.GridColumnStyles.Add(style1);

DataGridTextBoxColumn style2= new DataGridTextBoxColumn();
style2.TextBox.Controls.Add(comboControl);
style2.MappingName= "au_fname";
style2.HeaderText="First Name";
ts.GridColumnStyles.Add(style2);

DataGridTextBoxColumn style3 = new DataGridTextBoxColumn();
style3.TextBox.Controls.Add(comboControl);
style3.MappingName="au_lname";
style3.HeaderText="Last Name";
ts.GridColumnStyles.Add(style3);











}
}
}


Answers (1)