dataGrid的问题?兄弟请帮帮我,有效立即给分!

刘物峰 2007-01-30 04:25:51
我绑定了一个dataGrid和几个TextBox,
  我想实现:
    单击dataGrid时,几个TextBox中的值也和dataGrid中的当前记录一样,也就是一个同步显示的问题!
我的绑定代码是:
this.tBName.DataBindings.Clear();
this.tBName.DataBindings.Add("Text",dataSet1.Tables["用户"],"名");
this.tBNo.DataBindings.Clear();
this.tBNo.DataBindings.Add("Text",dataSet1.Tables["用户"],"号");
this.tBPart.DataBindings.Clear();
this.tBPart.DataBindings.Add("Text",dataSet1.Tables["用户"],"局");
this.tBPhone.DataBindings.Clear();
this.tBPhone.DataBindings.Add("Text",dataSet1.Tables["用户"],"电话");
this.tBAddress.DataBindings.Clear();
this.tBAddress.DataBindings.Add("Text",dataSet1.Tables["用户"],"地址");

dataGrid1.SetDataBinding(dataSet1,"用户档案");
...全文
236 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdelphic 2008-05-26
  • 打赏
  • 举报
回复
this.tBPart.DataBindings.Clear();
this.tBPart.DataBindings.Add( "Text ",dataSet1.Tables[ "用户 "], "局 ");
this.tBPhone.DataBindings.Clear();
this.tBPhone.DataBindings.Add( "Text ",dataSet1.Tables[ "用户 "], "电话 ");
this.tBAddress.DataBindings.Clear();
this.tBAddress.DataBindings.Add( "Text ",dataSet1.Tables[ "用户
刘物峰 2007-02-04
  • 打赏
  • 举报
回复
不好意思,我已经在另一个地方给分了!
谢谢,朋友
liujia_0421(SnowLover) 有点高哦,

MyBind.Position :的意思就在绑定的位置!
我是定义了一个全局的:private BindingManagerBase MyBind ; //绑定
this.MyBind = this.BindingContext[dataSet1.Tables["用户档案"]];


lixuanshuai() 朋友这回你可以明白了吧!

我刚学C# 7天,以前我是用delphi开发的,刚刚学还有好多不明白!
lixuanshuai 2007-01-31
  • 打赏
  • 举报
回复
MyBind.Position 中MyBind是什么呀?
怎么设置MyBind呀?
liujia_0421 2007-01-30
  • 打赏
  • 举报
回复
有什么不明白的再说出来...
liujia_0421 2007-01-30
  • 打赏
  • 举报
回复
TO:还有一点想一起问了:
我还想实现:
  我有几个“上一个”“下一个”的按键代码:
if (MyBind.Position == MyBind.Count-1 )
 MyBind.Position = 0 ;
else
 MyBind.Position += 1;
在点击时,窗体上的几个TextBox,也同步变话,说白了就是“按键”、dataGrid、textBox
三个同步!


实现你这个功能没你想的那么麻烦...
我在你的上一个贴子里已说得很清楚了,可能你没太明白...
看下我上面写的那个例子,就两句代码:

//前一个
this.BindingContext[ds.Tables["student"]].Position += 1;
//后一个
this.BindingContext[ds.Tables["student"]].Position -= 1;
liujia_0421 2007-01-30
  • 打赏
  • 举报
回复
private DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataBind();
}
private void DataBind()
{
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
SqlDataAdapter sda = new SqlDataAdapter("select * from studentDetails", con);
sda.Fill(ds, "student");
//数据绑定
this.dataGridView1.DataSource = ds.Tables["student"];
this.textBox1.DataBindings.Add("Text", ds.Tables["student"], "sno");
this.textBox2.DataBindings.Add("Text", ds.Tables["student"], "sname");
this.textBox3.DataBindings.Add("Text", ds.Tables["student"], "sage");
}
private void button1_Click(object sender, EventArgs e)
{
//前一个
this.BindingContext[ds.Tables["student"]].Position += 1;
}
private void button2_Click(object sender, EventArgs e)
{
//后一个
this.BindingContext[ds.Tables["student"]].Position -= 1;
}
liujia_0421 2007-01-30
  • 打赏
  • 举报
回复
算了,我给你写了个例子,不过我用的是DataGridView,不过用法都是一样...

界面上一个DataGridView,三个TextBox,两个Button("上一个","下一个")...

数据库为student,表名为studentDetails,三个字段("sno","sname","sage")...

DataGridView绑定整个表,三个textBox分别绑定数据的三个字段,两个button分别得到上一条和下一条记录...

代码如下:
liujia_0421 2007-01-30
  • 打赏
  • 举报
回复
好像楼主问过吧...

还没有解决吗?
Eddie005 2007-01-30
  • 打赏
  • 举报
回复
楼上正解...

并且实现“上一个”“下一个”的按键也很简单,控制dataGrid1的CurrentCellIndex属性即可~
EddieOO5 2007-01-30
  • 打赏
  • 举报
回复
dataGrid1.SetDataBinding(dataSet1,"用户档案");
改为:
dataGrid1.DataSource = dataSet1.Tables["用户档案"];
刘物峰 2007-01-30
  • 打赏
  • 举报
回复
还有一点想一起问了:
我还想实现:
  我有几个“上一个”“下一个”的按键代码:
if (MyBind.Position == MyBind.Count-1 )
 MyBind.Position = 0 ;
else
 MyBind.Position += 1;
在点击时,窗体上的几个TextBox,也同步变话,说白了就是“按键”、dataGrid、textBox
三个同步!

分不够我还可以++

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧