| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1877 人关注过本帖
标题:[求助]如何在DataGrid中添加其它控件?
只看楼主 加入收藏
guxinglengyue
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-7-17
收藏
 问题点数:0 回复次数:14 
[求助]如何在DataGrid中添加其它控件?

DataGrid的单元格默认是TextBox,如何用CheckBox、ComboBox等控件来代替,还有如何在TextBox的右边添加一个按钮,我看到很多输入文本框的右边有个...的按钮,按下...按钮会跳出相应的界面。谢过各位先。

搜索更多相关主题的帖子: DataGrid 控件 TextBox 按钮 
2006-07-18 16:17
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 

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

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
ComboBox combo; //手动声明combox
DateTimePicker date; //手动声明DataTimePicker控件
DataSet ds=new DataSet(); //数据集
private System.Windows.Forms.DataGrid allData;
private System.Windows.Forms.Button button1;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.allData = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.allData)).BeginInit();
this.SuspendLayout();
//
// allData
//
this.allData.DataMember = "";
this.allData.Dock = System.Windows.Forms.DockStyle.Top;
this.allData.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.allData.Location = new System.Drawing.Point(0, 0);
this.allData.Name = "allData";
this.allData.Size = new System.Drawing.Size(528, 224);
this.allData.TabIndex = 0;
this.allData.CurrentCellChanged += new System.EventHandler(this.allData_CurrentCellChanged);
this.allData.Leave += new System.EventHandler(this.allData_Leave);
this.allData.Scroll += new System.EventHandler(this.allData_Scroll);
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 240);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "保存";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.allData);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.allData)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"\\Users.mdb");
OleDbDataAdapter da=new OleDbDataAdapter("select * from [User]",con);
try
{
da.Fill(ds,"User"); //填充数据集
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}

combo=new ComboBox(); //实例化combox
combo.Items.Add("男"); //加入内容
combo.Items.Add("女");
combo.Visible=false; //设为不可见
combo.SelectedIndexChanged+=new EventHandler(combo_SelectedIndexChanged); //注册委托
//combo.SelectedIndex=0;
this.allData.Controls.Add(combo); //将combox加入DataGrid控件
date=new DateTimePicker(); //实例化DataTimePicker控件
date.Value=DateTime.Now; //初始化
date.ValueChanged+=new EventHandler(date_ValueChanged); //注册委托
date.Visible=false; //设为不可见
this.allData.Controls.Add(date); //加入DataGrid控件
this.allData.DataSource=ds.Tables[0]; //数据绑定
}

//combox选项改变事件
private void combo_SelectedIndexChanged(object sender, EventArgs e)
{
//注意:一定要操作DataGrid控件,而不是操作DataSet,因为可能会指到还未添加到DataSet的新行上
//以下语句容易出错:
//ds.Tables[0].Rows[allData.CurrentRowIndex][2]=combo.SelectedItem;
allData[allData.CurrentCell.RowNumber,allData.CurrentCell.ColumnNumber]=combo.SelectedItem;
}

//DataTimePicker控件的值改变事件
private void date_ValueChanged(object sender, EventArgs e)
{
//注意:一定要操作DataGrid控件,而不是操作DataSet,因为可能会指到还未添加到DataSet的新行上
//以下语句容易出错:
//ds.Tables[0].Rows[allData.CurrentRowIndex][3]=data.Value.ToShortDataString();
allData[allData.CurrentCell.RowNumber,allData.CurrentCell.ColumnNumber]=date.Value.ToShortDateString();
}

//DataGrid控件的单元格改变事件
private void allData_CurrentCellChanged(object sender, System.EventArgs e)
{
int col=allData.CurrentCell.ColumnNumber; //得到所选单元格的所在列号
//如果为2(第三列)
if(col==2)
{
if(date.Visible)
date.Visible=false;
Rectangle rect=allData.GetCellBounds(allData.CurrentCell); //得到该单元格的矩形大小
combo.Location=new Point(rect.Left,rect.Top); //设置combox出现的位置
combo.SetBounds(rect.Left,rect.Top,rect.Width,rect.Height); //设置combox的大小
combo.Visible=true; //设置为可见
//确定显示哪个值
for(int i=0;i<combo.Items.Count;i++)
{
if(combo.Items[i].ToString().Trim()==allData[allData.CurrentCell.RowNumber,allData.CurrentCell.ColumnNumber].ToString().Trim())
{
combo.SelectedIndex=i;
break;
}
}
}
//如果为3(第四列)
else if(col==3)
{
if(combo.Visible)
combo.Visible=false;
Rectangle rect=allData.GetCellBounds(allData.CurrentCell); //得到当前列的矩形
date.Location=new Point(rect.Left,rect.Top); //设置控件的位置
date.SetBounds(rect.Left,rect.Top,rect.Width,rect.Height); //设置大小
date.Visible=true; //设为可见
}
//其他列则不显示combox和DataTimePicker控件
else
{
combo.Visible=false;
date.Visible=false;
}
}

//DataGrid控件的滚动事件
private void allData_Scroll(object sender, System.EventArgs e)
{
combo.Visible=false;
date.Visible=false;
}

//保存按钮点击事件
private void button1_Click(object sender, System.EventArgs e)
{
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"\\Users.mdb");
OleDbCommand cmd=new OleDbCommand(); //用于更改的Command
cmd.Connection=con;
cmd.CommandText="update [User] set id=?,name=?,sex=?,joindate=? where id=? and name=? and sex=? and joindate=?";
cmd.Parameters.Add("newid",OleDbType.Char,10,"id");
cmd.Parameters.Add("newname",OleDbType.Char,10,"name");
cmd.Parameters.Add("newsex",OleDbType.Char,2,"sex");
cmd.Parameters.Add("newdate",OleDbType.Date,0,"joindate");
OleDbParameter para=cmd.Parameters.Add("oldid",OleDbType.Char,10,"id");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("oldname",OleDbType.Char,10,"name");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("oldsex",OleDbType.Char,2,"sex");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("olddate",OleDbType.Date,0,"joindate");
para.SourceVersion=DataRowVersion.Original;
OleDbCommand insertcmd=new OleDbCommand(); //用于插入的Command
insertcmd.Connection=con;
insertcmd.CommandText="insert into [User] values(?,?,?,?)";
insertcmd.Parameters.Add("newid",OleDbType.Char,10,"id");
insertcmd.Parameters.Add("newname",OleDbType.Char,10,"name");
insertcmd.Parameters.Add("newsex",OleDbType.Char,2,"sex");
insertcmd.Parameters.Add("newdate",OleDbType.Date,0,"joindate");
OleDbDataAdapter da=new OleDbDataAdapter();
da.UpdateCommand=cmd;
da.InsertCommand=insertcmd;
try
{
//只更新已被修改或新添加的行
da.Update(ds.Tables[0].Select("","",DataViewRowState.ModifiedCurrent|DataViewRowState.Added));
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
MessageBox.Show("OK");
}

//DataGrid控件的失去焦点事件
private void allData_Leave(object sender, System.EventArgs e)
{
combo.Visible=false;
date.Visible=false;
}
}
}


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-07-18 18:41
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
上面的代码为长时间积累的心得,望版主加威望!

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-07-18 18:44
guxinglengyue
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-7-17
收藏
得分:0 
非常感谢

看到这么多代码,我还没仔细看,今天一早就赶紧回帖了,写了这么多,真的很感动,谢谢marer。

2006-07-19 07:59
guxinglengyue
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-7-17
收藏
得分:0 
怎么加威望?
如果我可以加,我一定给你加,怎么加啊?
2006-07-19 08:06
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
要版主加啊,楼主有心可无力吧?应该是这样,我不太清楚。

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-07-19 09:12
xxxxx52
Rank: 4
等 级:贵宾
威 望:13
帖 子:689
专家分:0
注 册:2006-4-30
收藏
得分:0 
如果这个帖子加精 是楼主加经验金钱的~~ marer可能什么都没有~~
建议marer有什么好东西直接showhand 如果大家说好我会给你加精的~!
另外 威望是每个月加一次的 以后你当版主的时候体验吧 哈

好的资料下载网站http:///in.asp?id=xuelion2006 嘿嘿帮点一下拉~
2006-07-19 09:51
xxxxx52
Rank: 4
等 级:贵宾
威 望:13
帖 子:689
专家分:0
注 册:2006-4-30
收藏
得分:0 
给你单贴加钱加经验了~ 以后好帖子直接发出来 让大家摩拜拉~

好的资料下载网站http:///in.asp?id=xuelion2006 嘿嘿帮点一下拉~
2006-07-19 09:55
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
以下是引用marer在2006-7-19 9:12:36的发言:
要版主加啊,楼主有心可无力吧?应该是这样,我不太清楚。

好象只有每个月斑竹选举的时候前几名才能加威望的
不过你这么热心的帮大家解决问题
到时候大家会选你给你加威望的
我会投你一票的


此号自封于2006年11月30日
2006-07-19 11:04
xupeng
Rank: 1
等 级:新手上路
帖 子:4049
专家分:0
注 册:2006-2-12
收藏
得分:0 
能把你的数据库发上来吗?
或者把整个代码包括数据库打包发上来吗?
谢谢了啊!

反清复明 http://xupeng.
2006-07-19 12:00
快速回复:[求助]如何在DataGrid中添加其它控件?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035205 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved