Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
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!
Posted
Comments
Orcun Iyigun 14-Apr-11 18:06pm    
I know thats not an answer to your question but do you have to use a textbox to update that cell value? are you displaying your gridview? Just another option, you may do inline update. Like editable cells in gridview. you click to the cell and then you can edit it.
[no name] 15-Apr-11 1:48am    
No, I don't like inline edit, it can throw a message saying invalid data input, I want user to input data from a form (I prevent user from inputting wrong data right at here). I don't see the difference between "displaying data" and "inputting data" in a datagridview, the dislayed data means the data have already input (into datagridview). That will cause the datatable (as a binding source to the datagridview) changing, and we can use dataadapter to update the real data by passing this datatable over the method Update.
That's my idea, if I use "Insert into", the datatable filled by the datadapter won't change, so the datagridview also won't change, so I have to "reload" the data by making a new "SELECT" command and use datdadapter to re-fill the datatable to show the data just input! That way is more complex than the way I want (DataBindings), isn't it?
Thank you so much!
Aman4.net 15-Apr-11 7:42am    
Dear King Boy,
I just go through your question and comments posted on it. I found that you can prompt to user for enter appropriate value at DataError Event of DataGridView in lieu of placing an extra control and binding DataGridView again.
[no name] 15-Apr-11 22:47pm    
Thank you! My problem isn't simply to check valid data, I also want to change the UI and maybe disable the editability of the datagridview.
Thanks again!

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.

XML
<asp:TemplateField HeaderText="fromtextbox">
               <ItemTemplate>
                           <asp:TextBox ID="mygrdviewtext" runat="server" Text='<%# GetTextValue() %>' />
                </ItemTemplate>
               </asp:TemplateField>
 
Share this answer
 
Comments
[no name] 19-Apr-11 8:30am    
Thank you!
I'll try it! I don't think it becomes so complex like that!
I like the simplicity like simply using Control.DataBindings.Add(...)
But it seems that it's not useful here in this case!
Thank you again!
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.
 
Share this answer
 
v2
Comments
[no name] 17-Apr-11 10:48am    
Oh yeah, I only want to transfer texts to datagridview without concerning to the database (I will update it later).
My problem is how to do that not when to do that, we have so many points of time to do that such as when a button clicked, the textbox lost focus, ... no matter when it is. Could you give me some way to do what I want? Please remember that (revise my OP), I don't want the normal way that we can access to each cell of the datagridview and assign its value to the Text of the textbox. I want to do that with DataBindings.
Thank you!
Cyrus Neah 17-Apr-11 22:40pm    
I posted the solution above. See if it applies to you at all. Although I really still think the creating a form in the EditItemTemplate and handling it there is more standard. The button is just there as a place holder to show event handling. The code needs to be re-factored etc...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900