如何在winform 中的datagrid 中使用 combobox列? 5.5 How can I put a combobox in a column of a datagrid?http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q480q 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 定義一個事件,返回三個參數,分別是表名,字段名,ComboBox,public static void GetData(string TableName,string FieldName,ComboBox com) { SqlCommand cmd=new SqlCommand("select * from "+TableName,cn); SqlDataAdapter da=new SqlDataAdapter(cmd); DataSet ds=new DataSet(); da.Fill(ds,"talbe"); ArrayList al=new ArrayList(); foreach(DataRow dr in ds.Tables["talbe"].Rows) { al.Add(new Userinfo((string)dr[FieldName])); } com.DataSource=al; com.DisplayMember="Name"; } 下面是可以直接运行的代码(新建一个cs项目,在窗口上画一个datagrid1和comboBox1)using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace Cs{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.ComboBox comboBox1; /// <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.dataGrid1 = new System.Windows.Forms.DataGrid(); this.comboBox1 = new System.Windows.Forms.ComboBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(4, 6); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(568, 262); this.dataGrid1.TabIndex = 0; this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click); this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick); this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged); this.dataGrid1.Scroll += new System.EventHandler(this.dataGrid1_Scroll); // // comboBox1 // this.comboBox1.Location = new System.Drawing.Point(20, 206); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(168, 20); this.comboBox1.TabIndex = 1; this.comboBox1.Text = "comboBox1"; this.comboBox1.Visible = false; this.comboBox1.TextChanged += new System.EventHandler(this.comboBox1_TextChanged); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(578, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.comboBox1, this.dataGrid1}); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }//*********************************关键事件*********************************/* private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { this.comboBox1.Width=this.dataGrid1.GetCurrentCellBounds().Width; }*/ private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e) { this.comboBox1.Items.Clear(); this.comboBox1.Items.Add(this.dataGrid1[this.dataGrid1.CurrentCell]); this.comboBox1.Items.Add("这里可以根据不同的列用switch case语句加其它对应值"); //MessageBox.Show(this.dataGrid1.CurrentRowIndex.ToString()+"行" + this.dataGrid1.CurrentCell.ColumnNumber.ToString()+"列"+this.dataGrid1[this.dataGrid1.CurrentCell]); //隐藏 this.comboBox1.Visible=false; this.comboBox1.Width=0; //占据网格单元格位置 this.comboBox1.Left=this.dataGrid1.GetCurrentCellBounds().Left+5; this.comboBox1.Top=this.dataGrid1.GetCurrentCellBounds().Top+5; this.comboBox1.Width=this.dataGrid1.GetCurrentCellBounds().Width; this.comboBox1.Height=this.dataGrid1.GetCurrentCellBounds().Height; this.comboBox1.Text=this.dataGrid1[this.dataGrid1.CurrentCell].ToString(); //可见 this.comboBox1.Visible=true; this.comboBox1.Width=this.dataGrid1.GetCurrentCellBounds().Width; this.comboBox1.SelectionStart=0; this.comboBox1.Focus(); //置于上一层 this.comboBox1.BringToFront(); } private void comboBox1_TextChanged(object sender, System.EventArgs e) { this.dataGrid1[this.dataGrid1.CurrentCell]=this.comboBox1.Text; }//*********************************关键事件********************************* private void GetDataTable() { DataTable dt =new DataTable(); int intRowIndex; int intColIndex; for (intRowIndex=0;intRowIndex<9;intRowIndex++) { dt.Rows.Add(dt.NewRow()); dt.Columns.Add(); } for (intRowIndex=0;intRowIndex<9;intRowIndex++) { for (intColIndex=0;intColIndex<9;intColIndex++) { dt.Rows[intRowIndex][intColIndex]=intRowIndex.ToString()+"行"+intColIndex.ToString()+"列"; } } this.dataGrid1.DataSource=dt; } private void Form1_Load(object sender, System.EventArgs e) { GetDataTable(); } private void dataGrid1_Scroll(object sender, System.EventArgs e) { this.comboBox1.Visible=false; this.comboBox1.Width=0; } private void dataGrid1_DoubleClick(object sender, System.EventArgs e) { this.comboBox1.Visible=false; this.comboBox1.Width=0; } private void dataGrid1_Click(object sender, System.EventArgs e) { this.comboBox1.Visible=false; this.comboBox1.Width=0; } }} 如何调用IE或firefox的内核? 对多数据库支持的问题 只读属性与Get×××()方法的选用 读写配置文件的源码 C# 多线程多任务怎么实现。 请教:win8系统64位环境下VS2010,C#开发的兼容性问题 要休五一了,特散分以示庆祝。。。。。。 有关vs.net的调试器的问题! c#里如何使用shbrowseforfolder?那位兄弟给个例子 请教个问题! 在DataGrid中怎么让空值不显示“(空)”呢? saucer(思归, MS .NET MVP)和各位GGJJDDMM请进,希望开个处方:优秀的邮件列单或好的国外学习站点。
public static void GetData(string TableName,string FieldName,ComboBox com)
{
SqlCommand cmd=new SqlCommand("select * from "+TableName,cn);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds,"talbe");
ArrayList al=new ArrayList();
foreach(DataRow dr in ds.Tables["talbe"].Rows)
{
al.Add(new Userinfo((string)dr[FieldName]));
}
com.DataSource=al;
com.DisplayMember="Name";
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Cs
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.ComboBox comboBox1;
/// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
this.comboBox1 = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(4, 6);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(568, 262);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick);
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
this.dataGrid1.Scroll += new System.EventHandler(this.dataGrid1_Scroll);
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(20, 206);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(168, 20);
this.comboBox1.TabIndex = 1;
this.comboBox1.Text = "comboBox1";
this.comboBox1.Visible = false;
this.comboBox1.TextChanged += new System.EventHandler(this.comboBox1_TextChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(578, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.comboBox1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}//*********************************关键事件*********************************
/* private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
this.comboBox1.Width=this.dataGrid1.GetCurrentCellBounds().Width;
}
*/
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
this.comboBox1.Items.Clear();
this.comboBox1.Items.Add(this.dataGrid1[this.dataGrid1.CurrentCell]); this.comboBox1.Items.Add("这里可以根据不同的列用switch case语句加其它对应值");
//MessageBox.Show(this.dataGrid1.CurrentRowIndex.ToString()+"行" + this.dataGrid1.CurrentCell.ColumnNumber.ToString()+"列"+this.dataGrid1[this.dataGrid1.CurrentCell]);
//隐藏
this.comboBox1.Visible=false;
this.comboBox1.Width=0; //占据网格单元格位置
this.comboBox1.Left=this.dataGrid1.GetCurrentCellBounds().Left+5;
this.comboBox1.Top=this.dataGrid1.GetCurrentCellBounds().Top+5;
this.comboBox1.Width=this.dataGrid1.GetCurrentCellBounds().Width;
this.comboBox1.Height=this.dataGrid1.GetCurrentCellBounds().Height;
this.comboBox1.Text=this.dataGrid1[this.dataGrid1.CurrentCell].ToString();
//可见
this.comboBox1.Visible=true;
this.comboBox1.Width=this.dataGrid1.GetCurrentCellBounds().Width;
this.comboBox1.SelectionStart=0;
this.comboBox1.Focus();
//置于上一层
this.comboBox1.BringToFront();
} private void comboBox1_TextChanged(object sender, System.EventArgs e)
{
this.dataGrid1[this.dataGrid1.CurrentCell]=this.comboBox1.Text;
}
//*********************************关键事件********************************* private void GetDataTable()
{
DataTable dt =new DataTable();
int intRowIndex;
int intColIndex;
for (intRowIndex=0;intRowIndex<9;intRowIndex++)
{
dt.Rows.Add(dt.NewRow());
dt.Columns.Add();
} for (intRowIndex=0;intRowIndex<9;intRowIndex++)
{
for (intColIndex=0;intColIndex<9;intColIndex++)
{
dt.Rows[intRowIndex][intColIndex]=intRowIndex.ToString()+"行"+intColIndex.ToString()+"列";
}
}
this.dataGrid1.DataSource=dt;
} private void Form1_Load(object sender, System.EventArgs e)
{
GetDataTable();
} private void dataGrid1_Scroll(object sender, System.EventArgs e)
{
this.comboBox1.Visible=false;
this.comboBox1.Width=0;
} private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
{
this.comboBox1.Visible=false;
this.comboBox1.Width=0;
} private void dataGrid1_Click(object sender, System.EventArgs e)
{
this.comboBox1.Visible=false;
this.comboBox1.Width=0;
} }}