完整代码如下:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WinTest {
public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { List<Demo> list = new List<Demo> {new Demo{ ID=1, Name="aaa"},new Demo{ ID=1, Name="bbb"} }; this.dataGridView1.DataSource = list; for (int i = 0; i < list.Count; i++) { ComboBox cb = new ComboBox(); this.dataGridView1.Controls.Add(cb);
} } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (dataGridView1.CurrentCell.GetType().Name == "DataGridViewComboBoxCell") { ComboBox comboBox = (ComboBox)e.Control; if (comboBox.Tag==null) { comboBox.Tag = 1; comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged); }
} } void comboBox_SelectedIndexChanged(object sender, EventArgs e) { //1是int类型 dataGridView1.CurrentRow.Cells[2].Value = "你要改变的值"; } } public class Demo { public int ID { get; set; } public string Name { get; set; } } } 界面 namespace WinTest { partial class Form2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (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.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1}); this.dataGridView1.Location = new System.Drawing.Point(135, 85); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 23; this.dataGridView1.Size = new System.Drawing.Size(240, 150); this.dataGridView1.TabIndex = 0; this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing); // // Column1 // this.Column1.HeaderText = "Column1"; this.Column1.Items.AddRange(new object[] { "汇率1", "汇率2", "汇率3"}); this.Column1.Name = "Column1"; // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(413, 328); this.Controls.Add(this.dataGridView1); this.Name = "Form2"; this.Text = "Form2"; this.Load += new System.EventHandler(this.Form2_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.DataGridViewComboBoxColumn Column1; } }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WinTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
List<Demo> list = new List<Demo> {new Demo{ ID=1,
Name="aaa"},new Demo{ ID=1,
Name="bbb"} }; this.dataGridView1.DataSource = list; for (int i = 0; i < list.Count; i++)
{
ComboBox cb = new ComboBox();
this.dataGridView1.Controls.Add(cb);
} } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.GetType().Name == "DataGridViewComboBoxCell")
{
ComboBox comboBox = (ComboBox)e.Control; if (comboBox.Tag==null)
{
comboBox.Tag = 1;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
} void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//1是int类型
dataGridView1.CurrentRow.Cells[2].Value = "你要改变的值";
}
}
public class Demo { public int ID { get; set; }
public string Name { get; set; } }
}
界面
namespace WinTest
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1});
this.dataGridView1.Location = new System.Drawing.Point(135, 85);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(240, 150);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);
//
// Column1
//
this.Column1.HeaderText = "Column1";
this.Column1.Items.AddRange(new object[] {
"汇率1",
"汇率2",
"汇率3"});
this.Column1.Name = "Column1";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(413, 328);
this.Controls.Add(this.dataGridView1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewComboBoxColumn Column1;
}
}
这是我写的用combobox实现的dgv的combobox列的效果,根据你需要的值当你离开combobox时把后面的只读的值赋给对应的单元格
public static void DataGridAddCobx(DataGridView DataGD, ComboBox Cobx)
{
try
{
DataGD.Controls.Add(Cobx);
Rectangle rect = DataGD.GetCellDisplayRectangle(DataGD.CurrentCell.ColumnIndex, DataGD.CurrentCell.RowIndex, false);
Cobx.Left = rect.Left;
Cobx.Top = rect.Top;
Cobx.Width = rect.Width;
Cobx.Height = rect.Height;
Cobx.Visible = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
我用的是DevExpress的GridControl控件实现的,Windows自带的DataGridView控件没实现过,不过应该都差不多吧