为什么数据显示在datagridview中而不显示在两个文本框中? [英] Why Data display in datagridview and not display in two text boxes ?

查看:46
本文介绍了为什么数据显示在datagridview中而不显示在两个文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家

数据显示在datagridview中并且不会同时显示在两个文本框中?

这是我的按钮代码:



hi ,everyone
Data display in datagridview and not display in two text boxes in the same time ??
and this is my code in button:

da.SelectCommand = new SqlCommand("select * from tblcontacts ",cs);
           ds.Clear();
           da.Fill(ds);
           dataGridView1.DataSource = ds.Tables[0];
           DataTable dt = new DataTable();
           dt.Columns.Add("firstname", typeof(string));
           dt.Columns.Add("lastname", typeof(string));

           tblnamesbs.DataSource = dt;
           this.textBox1.DataBindings.Add("Text", this.tblnamesbs, "firstname", true);
           this.textBox2.DataBindings.Add("Text", this.tblnamesbs, "lastname", true);

推荐答案

数据表dt不是来自ds.Tables [0]的对象引用。

在您的代码中,您创建了dt作为新数据表,并创建了两列,但表从未填充。
The datatable dt is not a object reference from ds.Tables[0].
In your code, you created dt as a new datatable, and created the two columns, but the table is never filled.


示例:

sample:
OleDbDataAdapter dAdapter = new OleDbDataAdapter("select * from oui_table", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bd.mdb");
dAdapter.Fill(ds, "t1");

this.dataGridView1.DataSource = ds.Tables["t1"];

this.textBox1.DataBindings.Add(new Binding("text", ds.Tables["t1"], "id_oui"));


您正在尝试将空数据分配给 bindingsource



尝试这样。

you are trying to assign a empty datatable to bindingsource.

try like this.
tblnamesbs.DataSource = dt;
tblnamesbs.DataSource = ds.Tables[0];
this.textBox1.DataBindings.Add("Text", this.tblnamesbs, "firstname", true);
this.textBox2.DataBindings.Add("Text", this.tblnamesbs, "lastname", true);


这篇关于为什么数据显示在datagridview中而不显示在两个文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆