BindingSource的用法,请大神看下

shsyzl007 2016-09-01 11:56:57
先贴一段简单的demo代码
private void InitializeControlsAndDataSource()
{
DataGridView dataGridView1 = new DataGridView();
BindingSource bindingSource1 = new BindingSource();
TextBox textBox1 = new TextBox();

textBox1.Location = new Point(132, 156);
textBox1.Size = new Size(100, 20);
this.Controls.Add(textBox1);
this.Controls.Add(dataGridView1);

DataSet set1 = new DataSet();
set1.Tables.Add("Menu");
set1.Tables[0].Columns.Add("Beverages");
set1.Tables[0].Rows.Add("coffee");
set1.Tables[0].Rows.Add("tea");
set1.Tables[0].Rows.Add("hot chocolate");
set1.Tables[0].Rows.Add("milk");
set1.Tables[0].Rows.Add("orange juice");
bindingSource1.DataSource = set1;//DataSet 绑定到bindingSource1
bindingSource1.DataMember = "Menu";

dataGridView1.DataSource = bindingSource1;//dataGridView1 绑定bindingSource1
textBox1.DataBindings.Add("Text", bindingSource1,//这里什么意思?
"Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
bindingSource1.BindingComplete +=
new BindingCompleteEventHandler(bindingSource1_BindingComplete);
}

private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)//这个函数是干嘛用的
{
if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && e.Exception == null)
{ e.Binding.BindingManagerBase.EndCurrentEdit(); }
}


现在有这样一个需求。股票行情不断推送过来当前合约的价格。有个持仓函数a可以获取当前股票的持仓,包括买入的价格,但实时盈亏需要根据推送过来的当前合约价格自己计算。有个函数b可以获取当前账户总资金和总的浮动盈亏。

需求1:持仓的股票盈亏要根据当前股票的价格实时变动,实时更新
需求2:浮动盈亏要实时更新。
我原来的做法是,得到持仓品种当前的价格和开仓价格不断更新listview某个显示盈亏的列;不断刷新浮动盈亏这个函数。

请问大神,用BindingSource的方法怎么实现上面的需求?
好像可以不用去硬更新listview的某个列了?价格如果有更新他会自动重新绑定数据?
...全文
767 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2016-09-01
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning的回复:
随着技术的进步,DataSet、BindingSource 这些 C++ 一出现就有的控件,将逐步退出历史舞台 你只需将 DataTable 或 BindingList<T> 实例赋值到 DataGridView.DataSource 就可完成数据绑定 前者的任何变化都会立即反映到后者
控件会闪烁吗数据更新的时候
xuzuning 2016-09-01
  • 打赏
  • 举报
回复
随着技术的进步,DataSet、BindingSource 这些 C++ 一出现就有的控件,将逐步退出历史舞台 你只需将 DataTable 或 BindingList<T> 实例赋值到 DataGridView.DataSource 就可完成数据绑定 前者的任何变化都会立即反映到后者
「已注销」 2016-09-01
  • 打赏
  • 举报
回复
我想问下一个数据控件绑定了一个databingsource,当dataset有更新时,数据控件的数据会自动变吗,会闪烁吗?因为数据更新还是挺频繁的
LFH__ 2016-09-01
  • 打赏
  • 举报
回复
// Declare the controls to be used. private BindingSource bindingSource1; private TextBox textBox1; private TextBox textBox2; private DataGridView dataGridView1; private void InitializeControlsAndDataSource() { // Initialize the controls and set location, size and // other basic properties. this.dataGridView1 = new DataGridView(); this.bindingSource1 = new BindingSource(); this.textBox1 = new TextBox(); this.textBox2 = new TextBox(); this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Dock = DockStyle.Top; this.dataGridView1.Location = new Point(0, 0); this.dataGridView1.Size = new Size(292, 150); this.textBox1.Location = new Point(132, 156); this.textBox1.Size = new Size(100, 20); this.textBox2.Location = new Point(12, 156); this.textBox2.Size = new Size(100, 20); this.ClientSize = new Size(292, 266); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.dataGridView1); // Declare the DataSet and add a table and column. DataSet set1 = new DataSet(); set1.Tables.Add("Menu"); set1.Tables[0].Columns.Add("Beverages"); // Add some rows to the table. set1.Tables[0].Rows.Add("coffee"); set1.Tables[0].Rows.Add("tea"); set1.Tables[0].Rows.Add("hot chocolate"); set1.Tables[0].Rows.Add("milk"); set1.Tables[0].Rows.Add("orange juice"); // Set the data source to the DataSet. bindingSource1.DataSource = set1; //Set the DataMember to the Menu table. bindingSource1.DataMember = "Menu"; // Add the control data bindings. dataGridView1.DataSource = bindingSource1; textBox1.DataBindings.Add("Text", bindingSource1, "Beverages", true, DataSourceUpdateMode.OnPropertyChanged); textBox2.DataBindings.Add("Text", bindingSource1, "Beverages", true, DataSourceUpdateMode.OnPropertyChanged); bindingSource1.BindingComplete += new BindingCompleteEventHandler(bindingSource1_BindingComplete); } private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e) { // Check if the data source has been updated, and that no error has occured. if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && e.Exception == null) // If not, end the current edit. e.Binding.BindingManagerBase.EndCurrentEdit(); } 下面的代码演示如何使用 BindingSource 组件跨窗体共享绑定数据,具体代码如下: using System; using System.Drawing; using System.Windows.Forms; using System.Data; namespace BindingSourceMultipleForms { public class MainForm : Form { public MainForm() { this.Load += new EventHandler(MainForm_Load); } private BindingSource bindingSource1; private Button button1; private void MainForm_Load(object sender, EventArgs e) { InitializeData(); } private void InitializeData() { bindingSource1 = new System.Windows.Forms.BindingSource(); // Handle the BindingComplete event to ensure the two forms // remain synchronized. bindingSource1.BindingComplete += new BindingCompleteEventHandler(bindingSource1_BindingComplete); ClientSize = new System.Drawing.Size(292, 266); DataSet dataset1 = new DataSet(); // Some xml data to populate the DataSet with. string musicXml = "<?xml version='1.0' encoding='UTF-8'?>" + "<music>" + "<recording><artist>Dave Matthews</artist>" + "<cd>Under the Table and Dreaming</cd>" + "<releaseDate>1994</releaseDate><rating>3.5</rating></recording>" + "<recording><artist>Coldplay</artist><cd>X&Y</cd>" + "<releaseDate>2005</releaseDate><rating>4</rating></recording>" + "<recording><artist>Dave Matthews</artist>" + "<cd>Live at Red Rocks</cd>" + "<releaseDate>1997</releaseDate><rating>4</rating></recording>" + "<recording><artist>U2</artist>" + "<cd>Joshua Tree</cd><releaseDate>1987</releaseDate>" + "<rating>5</rating></recording>" + "<recording><artist>U2</artist>" + "<cd>How to Dismantle an Atomic Bomb</cd>" + "<releaseDate>2004</releaseDate><rating>4.5</rating></recording>" + "<recording><artist>Natalie Merchant</artist>" + "<cd>Tigerlily</cd><releaseDate>1995</releaseDate>" + "<rating>3.5</rating></recording>" + "</music>"; // Read the xml. System.IO.StringReader reader = new System.IO.StringReader(musicXml); dataset1.ReadXml(reader); // Get a DataView of the table contained in the dataset. DataTableCollection tables = dataset1.Tables; DataView view1 = new DataView(tables[0]); // Create a DataGridView control and add it to the form. DataGridView datagridview1 = new DataGridView(); datagridview1.ReadOnly = true; datagridview1.AutoGenerateColumns = true; datagridview1.Width = 300; this.Controls.Add(datagridview1); bindingSource1.DataSource = view1; datagridview1.DataSource = bindingSource1; datagridview1.Columns.Remove("artist"); datagridview1.Columns.Remove("releaseDate"); // Create and add a button to the form. button1 = new Button(); button1.AutoSize = true; button1.Text = "Show/Edit Details"; this.Controls.Add(button1); button1.Location = new Point(50, 200); button1.Click += new EventHandler(button1_Click); } // Handle the BindingComplete event to ensure the two forms // remain synchronized. private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e) { if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && e.Exception == null) e.Binding.BindingManagerBase.EndCurrentEdit(); } // The detailed form will be shown when the button is clicked. private void button1_Click(object sender, EventArgs e) { DetailForm detailForm = new DetailForm(bindingSource1); detailForm.Show(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MainForm()); } } // The detail form class. public class DetailForm : Form { private BindingSource formDataSource; // The constructor takes a BindingSource object. public DetailForm(BindingSource dataSource) { formDataSource = dataSource; this.ClientSize = new Size(240, 200); TextBox textBox1 = new TextBox(); this.Text = "Selection Details"; textBox1.Width = 220; TextBox textBox2 = new TextBox(); TextBox textBox3 = new TextBox(); TextBox textBox4 = new TextBox(); textBox4.Width = 30; textBox3.Width = 50; // Associate each text box with a column from the data source. textBox1.DataBindings.Add("Text", formDataSource, "cd", true, DataSourceUpdateMode.OnPropertyChanged); textBox2.DataBindings.Add("Text", formDataSource, "artist", true); textBox3.DataBindings.Add("Text", formDataSource, "releaseDate", true); textBox4.DataBindings.Add("Text", formDataSource, "rating", true); textBox1.Location = new Point(10, 10); textBox2.Location = new Point(10, 40); textBox3.Location = new Point(10, 80); textBox4.Location = new Point(10, 120); this.Controls.AddRange(new Control[] { textBox1, textBox2, textBox3, textBox4 }); } } }
  • 打赏
  • 举报
回复
textBox1.DataBindings. 这个是输入框跟BindingSource绑定列(属性),这样才能更新时自动刷新值 这么做的时候,你后面要变更就不能重新给BindingSource赋DataSource了,必须通过改列(属性)值的方式,即数据源不能发生变化了,虽然BindingSource有个方法貌似是整体重刷,但记得当初用时好像对数据源都变了的无效

111,117

社区成员

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

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

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