首先,我必须强调:“HOW TO:将 ComboBox 控件添加到 Windows 窗体 DataGrid 控件”这篇文章我看过了。但是我的问题不是这样的。下面是我的代码:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using tm;
using tm.data;
using tm.data.sql;namespace ComboBoxInDataGrid
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private tm.data.sql.ConnectionFactory connFactory;
private System.Data.SqlClient.SqlConnection conn;
private DataSet dsHR;
private tm.data.sql.Adapter adptEmployee;
private tm.data.sql.Adapter adptDepartment;
private System.Windows.Forms.DataGrid dgEmployee;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgEmployee = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dgEmployee)).BeginInit();
this.SuspendLayout();
// 
// dgEmployee
// 
this.dgEmployee.DataMember = "";
this.dgEmployee.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgEmployee.Name = "dgEmployee";
this.dgEmployee.Size = new System.Drawing.Size(496, 248);
this.dgEmployee.TabIndex = 0;
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
  this.dgEmployee});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dgEmployee)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
this.FillDataSet();
this.InitDataGrid();
} /// <summary>
/// 填充数据集
/// </summary>
private void FillDataSet()
{
//制作数据连接
this.connFactory = new ConnectionFactory();
this.connFactory.Server = "tm-3";
this.connFactory.DataBase = "hr";
this.connFactory.UserId = "sa";
this.connFactory.Password = "";
this.conn = this.connFactory.MakeConnection(); //数据集
this.dsHR = new DataSet(); this.adptDepartment = new Adapter(this.conn, this.dsHR, "Departments");
this.adptEmployee = new Adapter(this.conn, this.dsHR, "Employees"); //填充
this.adptDepartment.Fill();
this.adptEmployee.Fill();
} //初始化DataGrid
private void InitDataGrid()
{
//数据绑定
this.dgEmployee.SetDataBinding(this.adptEmployee.DataView, ""); //表样式
DataGridTableStyle tsl = new DataGridTableStyle();
tsl.MappingName = "Employees"; //列:雇员编号
DataGridTextBoxColumn colEmployeeId = new DataGridTextBoxColumn();
colEmployeeId.HeaderText = "雇员编号";
colEmployeeId.MappingName = "emp_id";
tsl.GridColumnStyles.Add(colEmployeeId); //列:雇员姓名
DataGridTextBoxColumn colEmployeeName = new DataGridTextBoxColumn();
colEmployeeName.HeaderText = "雇员姓名";
colEmployeeName.MappingName = "emp_name";
tsl.GridColumnStyles.Add(colEmployeeName); //列:雇员年龄
DataGridTextBoxColumn colEmployeeAge = new DataGridTextBoxColumn();
colEmployeeAge.HeaderText = "雇员年龄";
colEmployeeAge.MappingName = "emp_age";
tsl.GridColumnStyles.Add(colEmployeeAge);

//列:雇员性别
DataGridTextBoxColumn colEmployeeSex = new DataGridTextBoxColumn();
colEmployeeSex.HeaderText = "雇员性别";
colEmployeeSex.MappingName = "emp_sex";
tsl.GridColumnStyles.Add(colEmployeeSex); //列:所在部门
DataGridTextBoxColumn colDepartment = new DataGridTextBoxColumn();
colDepartment.HeaderText = "所在部门";
colDepartment.MappingName = "emp_department";
tsl.GridColumnStyles.Add(colDepartment); //首先弄一个combobox
ComboBox cmbDepartment = new ComboBox();
cmbDepartment.DataSource = this.adptDepartment.DataTable;
cmbDepartment.DisplayMember = "dept_name";
cmbDepartment.ValueMember = "dept_id";
cmbDepartment.DropDownStyle = ComboBoxStyle.DropDownList;
cmbDepartment.Dock = DockStyle.Fill;
cmbDepartment.SelectionChangeCommitted += 
new EventHandler(cmbDepartment_SelectionChangeCommitted); //把combobox加入到datagrid的最后一列
colDepartment.TextBox.Controls.Add(cmbDepartment); this.dgEmployee.TableStyles.Add(tsl);
} private void cmbDepartment_SelectionChangeCommitted(object sender, EventArgs e)
{
this.dgEmployee[this.dgEmployee.CurrentCell] = 
((ComboBox)sender).SelectedValue;
}
}
}数据库结构:
DataBase: hr
表:departments
dept_id              int
dept_name            nvarchar
dept_description     nvarchar
表:employees
emp_id               int
emp_name             nvarchar
emp_department       int
emp_age              int
emp_sex              bit
其中emp_department和dept_id有外键约束关系。当然department是父表。运行之后发现问题如下:
1.所在部门那列(也就是绑定colDepartment的那列)开始的时候仍然显示数字(1,2等)
2.单击该列之后可以显示ComboBox,而且可以选择,但是当焦点离开该Cell后,该Cell仍然恢复数字麻烦各位高手给看以看这个问题如何解决?请说详细一点。这个问题对于我非常重要。入能帮我解决这歌问题,我将不胜感激。谢谢。

解决方案 »

  1.   

    DataGridCombocColumn组建的关键内容:
    ......
    public DataGridComboBoxColumn(DataTable DataSource, int DisplayMember,int ValueMember)
    {
    Combo = new DataGridComboBox();
    _DisplayMember = DataSource.Columns[DisplayMember].ToString();
    _ValueMember = DataSource.Columns[ValueMember].ToString();

    Combo.Visible=false;
    Combo.DataSource = DataSource;
    Combo.DisplayMember = _DisplayMember;
    Combo.ValueMember = _ValueMember;
    Combo.DropDownStyle = ComboBoxStyle.DropDown;
    }
    ......
      

  2.   

    sorry! 上边的一段写错了!
    你把你要帮定的列从新帮定成string 应该就可以了!
      

  3.   

    我现在的问题关键是:
    emp_department和dept_id之间是外键约束。
    而我必须在DataGrid里面的ComboBox显示dept_name,然后在数据源更新emp_department。