且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据绑定到Datagrid视图中的文本框中

更新时间:2023-10-06 22:41:58

<asp:TextBox ID="txtValue" runat="server" Text='<%# Bind("Value") %>'></asp:TextBox>



这里 Value name是 DataSource $ c中的列名 $ c>。



假设您从数据库获取数据并将其绑定到 DataSet

所以,这个 Value 列应该出现在 DataSet $ c $里面c>。


Here "Value" name is the Column Name in the DataSource.

Suppose, you are getting data from Database and Binding it to a DataSet.
So, this "Value" Column should be present inside the DataSet.


你好,



绑定 DataGridView中的单元格到文本框,我们可以使用 TextBox.DataBinding.Add()方法使用以下代码。这里 Text 表示要绑定的控件属性的名称,dt是对象表示数据源,两个是要绑定到的属性或列表(此处为列名称)。



Hello,

To bind a cell in DataGridView to a Text Box, we could just use the following codes, using TextBox.DataBinding.Add() method. And here "Text" means the name of control property to bind, dt is the object represents the data source, and "Two" is the property or list to bind to (column name here).

DataTable dt = new DataTable();

dt.Columns.Add("One");
dt.Columns.Add("Two");
dt.Columns.Add("Three");
dt.Rows.Add(new string[] { "1", "2", "3" });
dt.Rows.Add(new string[] { "4", "5", "6" });
dt.Rows.Add(new string[] { "7", "8", "9" });
dataGridView1.DataSource = dt;
textBox1.DataBindings.Add("Text", dt, "Two");


查看一些演示项目,链接如下所示



GridView控件



带文本框,复选框,单选按钮和DropDown列表的可编辑网格视图大>

See some demo projects, Links are following below

GridView Control

Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List