各位大侠可以帮我看看这个例子错在哪里吗? C#那部分,提示"datagrid不包含 Item的定义". 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 改为 DataGrid1[DataGrid1.CurrentCell] gujunyan(ivy阿亮),改了之后是没有提示错误了,但打开的datagrid什么都没有,你运行的时候也是这样吗? 事件委托没了,,试试吧using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.SqlClient;namespace WindowsApplication7{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid DataGrid1; ComboBox MyCombo=new ComboBox(); SqlConnection con = new SqlConnection("server=localhost;database=northwind;uid=sa;pwd=sa"); SqlDataAdapter daEmp; DataSet ds=new DataSet(); /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); daEmp = new SqlDataAdapter("Select * From Employees", con); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } private void Form1_Load(object sender, System.EventArgs e) { MyCombo.Name = "MyCombo"; MyCombo.Visible = false; MyCombo.Items.Clear(); MyCombo.Items.Add("Sales Representative"); MyCombo.Items.Add("Inside Sales Coordinator"); MyCombo.Items.Add("Vice President, Sales"); MyCombo.Items.Add("Sales Manager"); MyCombo.Items.Add("Flunky"); MyCombo.TextChanged+=new EventHandler(Ctrls_TextChanged); daEmp.Fill(ds, "Employees"); DataGrid1.PreferredRowHeight = MyCombo.Height; DataGrid1.DataSource = ds; DataGrid1.DataMember = "Employees"; DataGrid1.Controls.Add(MyCombo); } private void DataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if (DataGrid1.CurrentCell.ColumnNumber == 3) MyCombo.Width = DataGrid1.GetCurrentCellBounds().Width; } private void Ctrls_TextChanged(object sender, System.EventArgs e) { if (DataGrid1.CurrentCell.ColumnNumber == 3) { MyCombo.Visible = false; if(DataGrid1[DataGrid1.CurrentCell] + "" == "") { SendKeys.Send("*");} DataGrid1[DataGrid1.CurrentCell] = MyCombo.Text; } } private void DataGrid1_CurrentCellChanged(object sender, System.EventArgs e) { if (DataGrid1.CurrentCell.ColumnNumber == 3) { MyCombo.Visible = false; MyCombo.Width = 0; MyCombo.Left = DataGrid1.GetCurrentCellBounds().Left; MyCombo.Top = DataGrid1.GetCurrentCellBounds().Top; MyCombo.Text = DataGrid1[DataGrid1.CurrentCell].ToString(); MyCombo.Visible = true; } else { MyCombo.Visible = false; MyCombo.Width = 0; } } private void DataGrid1_Scroll(object sender, System.EventArgs e) { MyCombo.Visible = false; MyCombo.Width = 0; } private void DataGrid1_Click(object sender, System.EventArgs e) { MyCombo.Visible = false; MyCombo.Width = 0; } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.DataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).BeginInit(); this.SuspendLayout(); // // DataGrid1 // this.DataGrid1.DataMember = ""; this.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.DataGrid1.Location = new System.Drawing.Point(24, 24); this.DataGrid1.Name = "DataGrid1"; this.DataGrid1.Size = new System.Drawing.Size(392, 184); this.DataGrid1.TabIndex = 0; this.DataGrid1.Click += new System.EventHandler(this.DataGrid1_Click); this.DataGrid1.CurrentCellChanged += new System.EventHandler(this.DataGrid1_CurrentCellChanged); this.DataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.DataGrid1_Paint); this.DataGrid1.Scroll += new System.EventHandler(this.DataGrid1_Scroll); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(456, 273); this.Controls.Add(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()); } }} DataGrid1.Items[(int)DataGrid1.EditItemInddex].Cells[0].ToString(); LiSDN(LOCK ON),谢谢你,可以了.我想附带问一下,正如你所看到,那datagrid里面的 combobox,开始的时候外表只是一个单元格,只有当mouse点下,才知道是一个combobox.有没有办法一开始,不经过mouse点击,就出现 combobox呢? 不太明白你的意思,,你是想每行的第四列都有一个combobox吗? 不是,我的意思是datagrid刚一打开,如果第一眼看这个,根本不知道有一列是combobox, 只有用mouse 点combobox所在的列,才知道原来这个单元格是一个combobox.怎样一打开datagrid,就让那combobox显露出来? private void DataGrid1_CurrentCellChanged(object sender, System.EventArgs e){ MyCombo.Left = DataGrid1.GetCellBounds(1,3).X; MyCombo.Top = DataGrid1.GetCurrentCellBounds().Top; MyCombo.Text = DataGrid1[DataGrid1.CurrentCell.RowNumber,3].ToString();}你试试~ 呵呵,,我这里做到了,,点击任何的列在第四列都会显示出combobox, LiSDN(LOCK ON) ,可以不用点击也可以显示combobox吗? 如何使用APDU命令操作IC卡 COM方面的问题。注册表中未找到组件,但程序里用guid引用了,为什么能用 求c/s应用程序自动更新 DataGridViewComboBoxColumn问题 曲线平滑? 传递给 Icon 的 Win32 句柄无效或者类型错误, 怎么办??? .Net运行环境小问题 关于EXCEL判断的问题。 NET开发的实践与案例讲座有朋友去国的吗?可以谈谈体会? 有关字符串的操作,请指教??? 参数关键字ref和out有用处? C#中和VB的with有同样作用的语句是什么??
DataGrid1[DataGrid1.CurrentCell]
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication7
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid DataGrid1;
ComboBox MyCombo=new ComboBox();
SqlConnection con = new SqlConnection("server=localhost;database=northwind;uid=sa;pwd=sa");
SqlDataAdapter daEmp;
DataSet ds=new DataSet();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
daEmp = new SqlDataAdapter("Select * From Employees", con); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void Form1_Load(object sender, System.EventArgs e)
{ MyCombo.Name = "MyCombo";
MyCombo.Visible = false;
MyCombo.Items.Clear();
MyCombo.Items.Add("Sales Representative");
MyCombo.Items.Add("Inside Sales Coordinator");
MyCombo.Items.Add("Vice President, Sales");
MyCombo.Items.Add("Sales Manager");
MyCombo.Items.Add("Flunky");
MyCombo.TextChanged+=new EventHandler(Ctrls_TextChanged);
daEmp.Fill(ds, "Employees");
DataGrid1.PreferredRowHeight = MyCombo.Height; DataGrid1.DataSource = ds; DataGrid1.DataMember = "Employees";
DataGrid1.Controls.Add(MyCombo);
} private void DataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{ if (DataGrid1.CurrentCell.ColumnNumber == 3)
MyCombo.Width = DataGrid1.GetCurrentCellBounds().Width;
} private void Ctrls_TextChanged(object sender, System.EventArgs e)
{
if (DataGrid1.CurrentCell.ColumnNumber == 3)
{
MyCombo.Visible = false;
if(DataGrid1[DataGrid1.CurrentCell] + "" == "")
{ SendKeys.Send("*");} DataGrid1[DataGrid1.CurrentCell] = MyCombo.Text;
} } private void DataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{ if (DataGrid1.CurrentCell.ColumnNumber == 3)
{
MyCombo.Visible = false;
MyCombo.Width = 0;
MyCombo.Left = DataGrid1.GetCurrentCellBounds().Left;
MyCombo.Top = DataGrid1.GetCurrentCellBounds().Top;
MyCombo.Text = DataGrid1[DataGrid1.CurrentCell].ToString();
MyCombo.Visible = true;
}
else
{ MyCombo.Visible = false;
MyCombo.Width = 0;
}
} private void DataGrid1_Scroll(object sender, System.EventArgs e)
{
MyCombo.Visible = false;
MyCombo.Width = 0; } private void DataGrid1_Click(object sender, System.EventArgs e)
{
MyCombo.Visible = false;
MyCombo.Width = 0;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).BeginInit();
this.SuspendLayout();
//
// DataGrid1
//
this.DataGrid1.DataMember = "";
this.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.DataGrid1.Location = new System.Drawing.Point(24, 24);
this.DataGrid1.Name = "DataGrid1";
this.DataGrid1.Size = new System.Drawing.Size(392, 184);
this.DataGrid1.TabIndex = 0;
this.DataGrid1.Click += new System.EventHandler(this.DataGrid1_Click);
this.DataGrid1.CurrentCellChanged += new System.EventHandler(this.DataGrid1_CurrentCellChanged);
this.DataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.DataGrid1_Paint);
this.DataGrid1.Scroll += new System.EventHandler(this.DataGrid1_Scroll);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 273);
this.Controls.Add(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());
}
}
}
{
MyCombo.Left = DataGrid1.GetCellBounds(1,3).X;
MyCombo.Top = DataGrid1.GetCurrentCellBounds().Top;
MyCombo.Text = DataGrid1[DataGrid1.CurrentCell.RowNumber,3].ToString();
}你试试~