| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3316 人关注过本帖
标题:DataGridView如何定指单元格?
只看楼主 加入收藏
IDeric
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-12-25
收藏
 问题点数:0 回复次数:15 
DataGridView如何定指单元格?
vs2003 可以用 gridcolumnstyle.textbox.controls.add(控件)
定制单元格,往单元格里加日历或者其他控件。
2005 怎么往 DataGridView 里添加一列 日历控件?
搜索更多相关主题的帖子: DataGridView 单元 
2007-02-28 21:18
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
我一般使用dataGrid1,dataGridView1的使用方法应该差不多

/*下面的一段程序功能是:动态获取dataGrid1中选中行的各字段的值*/
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
string myID = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString().Trim();
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * From student where ID='" + myID + "'", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
this.textBox1.Text = dr["sno"].ToString();
this.textBox2.Text = dr["sname"].ToString();
this.textBox3.Text = dr["sex"].ToString();
}
dr.Close();
cn.Close();
}

海鸽 is My Lover!!
2007-03-02 08:56
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
你稍微改动一下上面的程序,就能在dataGridView1中使用,自己摸索哦



海鸽 is My Lover!!
2007-03-02 08:57
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
程序说明:点击datagrid中的行,就在textbox中显示该行的单元格的值

student数据库中有 student表,字段为 ID sno sname sex
其中ID是自动编号的

数据库是sql 2000

海鸽 is My Lover!!
2007-03-02 09:01
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
liuminghui 你的方法楼主应该不能用到吧??呵

飘过~~
2007-03-02 12:47
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
收藏
得分:0 
就是只能读取各列的数据
不能添加什么东西。

有事无事都密我. MSN: cyyu_ryh@hotmail.co.jp E-mail: cyyu_ryh@
2007-03-02 13:01
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
我只是看到他的题目,他的详细问题,我没有看啊!
我要好好修炼,才能更好的帮别人解决问题

海鸽 is My Lover!!
2007-03-02 13:02
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
版主们,高手们解决这个问题啊

海鸽 is My Lover!!
2007-03-02 13:03
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分: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;
}
}
}

2007-03-02 15:09
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
是不是这种效果?
图片附件: 游客没有浏览图片的权限,请 登录注册


2007-03-02 15:11
快速回复:DataGridView如何定指单元格?
数据加载中...
 
   



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

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