欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现绑定DataGridView与TextBox之间关联的方法

程序员文章站 2023-11-30 10:20:46
本文实例讲述了c#实现绑定datagridview与textbox之间关联的方法。分享给大家供大家参考。具体实现方法如下: using system; usin...

本文实例讲述了c#实现绑定datagridview与textbox之间关联的方法。分享给大家供大家参考。具体实现方法如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
namespace system.windows.forms.samples
{
 public partial class form1 : form
 {
  public form1()
  {
   initializecomponent();
  }
  private void form1_load(object sender, eventargs e)
  {
   /* create a dataset with 1 datatable */
   dataset  dataset = new dataset();
   datatable datatable = dataset.tables.add("numbers");
   datatable.columns.add("id", typeof(int));
   datatable.columns.add("name", typeof(string));
   datatable.rows.add(0, "zero");
   datatable.rows.add(1, "one");
   currencymanager cm;
   /* get a currencymanager */
   // cm = (this.bindingcontext[dataset, "numbers"] as currencymanager);
   /* this gets a different currencymanager */
   cm = (this.bindingcontext[datatable] as currencymanager);
   /* bind left datagridview and textbox */
   this.datagridview1.datasource = dataset;
   this.datagridview1.datamember = "numbers";
   this.textbox1.databindings.add("text", dataset, "numbers.name", true);
   /* bind left datagridview and textbox */
   this.datagridview2.datasource = datatable;
   this.textbox2.databindings.add("text", datatable, "name", true);
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。