如何将文本框中的数据绑定到datagridview? [英] How to bind data from textboxes to a datagridview?

查看:75
本文介绍了如何将文本框中的数据绑定到datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我可以将数据(来自datagridview的单元格)绑定到文本框的Text属性,但是反向绑定方向太难了,我想当我在文本框中输入一些文本并点击Enter时,文本输入应该要在指定的单元格中显示,当然是使用DataBindings,这不是我想要的,通常的方式是访问该行,然后访问该单元格以将其值分配给文本.
你能帮我吗?
我喜欢代码片段,具体且易于理解!
非常感谢!

Hi!
I can Bind data from a cell (of a datagridview) to the Text property of a textbox, but the reverse binding direction is too hard to do, I want when I type in some text in my textbox and tap Enter, the text input should be display in a specified cell, of course using DataBindings, the normal way by accessing to the row and then to the cell to assign its value to the text is not what I want.
Could you please help me!
I like code snippets, concrete and easy to understand!
Thank you so much!

推荐答案

将此添加到您的代码中后

受保护的List< Control> listOfControls;

受保护的字符串GetTextValue()
{


TextBox textbox =(TextBox)Page.FindControl("textbox");
返回textbox.Text;
}


受保护的void Button1_Click(对象发送者,EventArgs e)
{

//您可以对此进行改进,但您可以理解,这将更改第三行的单元格
//对于第一行执行1等操作,应将其重构
TextBox txt =(TextBox)FindControlRecursive(GridView1,"mygrdviewtext",3);

txt.DataBind();
}

公共控件FindControlRecursive(控件根,字符串ID,整数行)
{


listOfControls = new List< Control>();

FindControlRecursive(Root,Id);

返回listOfControls [row -1];
}


公共控件FindControlRecursive(控件根,字符串ID)
{
如果(Root.ID == Id)
{
listOfControls.Add(Root);
}

foreach(Root.Controls中的Control Ctl)
{
控件FoundCtl = FindControlRecursive(Ctl,Id);

如果(FoundCtl!= null)
{
listOfControls.Add(FoundCtl);

}
}

返回null;
}


//在aspx页面中,将这样的内容添加到gridview中的单元格所在的位置,再次对它进行更改.
Add this to your code behind

protected List<Control> listOfControls;

protected string GetTextValue()
{


TextBox textbox = (TextBox)Page.FindControl("textbox");
return textbox.Text;
}


protected void Button1_Click(object sender, EventArgs e)
{

// you can improve this but you can get the idea, this would change the cell on row three
// for row one do 1 etc, this should be re-factored
TextBox txt = (TextBox)FindControlRecursive(GridView1, "mygrdviewtext",3);

txt.DataBind();
}

public Control FindControlRecursive(Control Root, string Id, int row)
{


listOfControls = new List<Control>();

FindControlRecursive(Root, Id);

return listOfControls[row -1];
}


public Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
{
listOfControls.Add(Root);
}

foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);

if (FoundCtl != null)
{
listOfControls.Add(FoundCtl);

}
}

return null;
}


// in your aspx page add something like this to your gridview , where the cell is, again change as it applies to you.

<asp:TemplateField HeaderText="fromtextbox">
               <ItemTemplate>
                           <asp:TextBox ID="mygrdviewtext" runat="server" Text='<%# GetTextValue() %>' />
                </ItemTemplate>
               </asp:TemplateField>


您可以在DataGrid_EditCommand中控制验证,也可以在单元格上进行客户端验证.如果仅使用gridview显示数据,那么为什么在填写表单后不更新数据库中的值,然后刷新gridview.您是说不想更新数据库,而只是将值从文本框传输到gridview中的单元格吗?那么您为什么不在文本框的文本更改事件中执行此操作.标准解决方案通常是最好的.
You can control the validation in DataGrid_EditCommand or else put client side validation on the cells. If you are only using the gridview to display data, then why don''t you update the values in the database after your form is filled out and then refresh the gridview. Are you saying you don''t want to update the database and just transfer the value from the textbox to a cell in the gridview? then why don''t you do that in the text changed event of the textbox. standard solutions are usually the best.


这篇关于如何将文本框中的数据绑定到datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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