BindingSource组件使用

第一次意识到BindingSource组件的强大!所以需要学习一下。首先是尝试建立了一些例子,以期对其机制和相关的内容进一步详细理解!

第一个例子:

 界面设计:一个DataGridView、BindingNavigater;将DataGridView的DataSource属性设置为选择数据源,这里选择了一个Database1.mdb的Access数据库。选择完成后会生成一个DataSet、BindingSource和TableAdapter。将bindingNavigater1的BindingSource属性设置为bindingsource;

打开窗体的代码视图会发现已经自动生成了如下代码:

View Code
private void Form2_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“database1DataSet.Stra”中。您可以根据需要移动或移除它。
this.straTableAdapter.Fill(this.database1DataSet.Stra);

}

为bindingSource添加CurrentItemChanged事件:

View Code
 private void straBindingSource_CurrentItemChanged(object sender, EventArgs e)
{

if (database1DataSet.GetChanges() == null)
{
return;
}
straTableAdapter.Update(database1DataSet.Stra);
this.straBindingSource.EndEdit();
}

有兴趣可以看看Database1DataSet1.Designer.cs中的代码,实现了数据库到Dataset的连接,主要是一些Fill、Update、delete方法。应该理解为实现了数据实体到DataSet中间桥梁。不过程序运行会有问题:编辑更新有的时候保存了,有的时候好像又没有保存!这里一个理解是复制的项目中的Database1.mdb和Debug文件下的Database1.mdb不一致。更新发生在Debug文件夹下,但是项目中的没有改变。具体什么原因不得而知。还有分析一下!

第二个例子:

界面设计:一个DataGridView、Bindingsource和BindingNavigater;bindingNavigater1的BindingSource属性设置为bindingsource1;

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace BindingSourcePrj
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OracleConnection orlConn = new OracleConnection();
OracleDataAdapter sda;
DataTable dtSelect;
private void Form1_Load(object sender, EventArgs e)
{
orlConn.ConnectionString = "user id=scott;password=tiger;data source=orcl";
orlConn.Open();
sda = new OracleDataAdapter("select * from emp", orlConn);///此处运用,对全局sda赋值,注意没有清除
dtSelect = new DataTable();
sda.Fill(dtSelect);
bindingSource1.DataSource = dtSelect;
this.dataGridView1.DataSource = bindingSource1;
orlConn.Close();
}

private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{

}

private void bindingSource1_CurrentItemChanged(object sender, EventArgs e)
{
if (dtSelect.GetChanges() == null)
{
return;
}
OracleCommandBuilder commBuilder = new OracleCommandBuilder(this.sda);
this.sda.Update(dtSelect);
bindingSource1.EndEdit();
}

private void bindingSource1_DataError(object sender, BindingManagerDataErrorEventArgs e)
{
MessageBox.Show(e.ToString());
}
}
}

编辑每一行后,可以实现自动更新,这和Access很像不是吗?当然用sda的Update方法整表更新似乎不太合适,应该用一行更新!当然Sda有这样重载的方法!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Uniapp中,父组件向子组件传递值可以通过props属性来实现。首先,在父组件中引入子组件并注册,然后在子组件标签中使用数据绑定的形式将值传递给子组件的props属性。例如:[2] 在父组件中,通过import语句引入子组件,并在components中进行注册。然后,在子组件的标签中使用:data-binding的形式将父组件的值传递给子组件的props属性。例如:<zi :userInfo="张三"></zi> 在子组件中,使用props来接收父组件传过来的值。通过在子组件的props中声明需要接收的属性名,例如:props:['userInfo']。在子组件的模板中,可以直接使用这个属性值。例如:{{userInfo}}。 另外,如果需要子组件向父组件传值,可以使用事件机制。子组件通过$emit方法触发一个事件,并传递需要传递的值。父组件在子组件标签上监听这个事件,并定义一个方法来接收子组件传过来的值。例如: 在子组件中,可以在需要传值的地方使用@click事件触发一个事件,并通过this.$emit方法向父组件传递值。例如:this.$emit("sendData", "我是子") 在父组件中,通过@事件名来监听子组件触发的事件,并在methods中定义一个方法来接收子组件传过来的值。例如:@sendData="getData" 通过以上方法,就可以在Uniapp中实现父组件向子组件传值以及子组件向父组件传值的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue中组件通信详解(父子组件, 爷孙组件, 兄弟组件)](https://download.csdn.net/download/weixin_38736760/13128096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uniapp父子组件之间传值](https://blog.csdn.net/weixin_43167546/article/details/107362671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值