Please I need help with a datagrid item on edit

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have been on this for hours and just cannot work it out.

I have a datagrid. I set the edititem index to a row to edit, that works.
However if i want to put a value into this textbox it wont let me. I just
cant find the control in the controlcollection for the cell. Is it because it
hasnt rendered yet? (very annoying as i have set the edititem to the index,
done my databound etc.) why doesnt it recognise the textbox? so:

dgThing.EditItemIndex = e.Item.ItemIndex;
get my source and do DataBind();
TextBox aBox = (TextBox)e.Item.Cells[2].Controls[0]; //i found this code in
msdn so i know its right
thisText.Text="set this text into the textbox";

Because i have to just work out the cells (there is not cell id) i have just
counted. I am def on the right one. The one before it has some text in it,
so i know where i am in the row. but it cant find the textbox control having
just gone into edit mode. Has anyone got round this somehow? Another thing,
why do I have to databind again. I only want to change one row yet i have to
go to my sql database and run a complicated query EVERY time to populate data
which was already there, just to get my item to go into edit mode! is this
the only way?
 
Hi

1) Remember to rebind the datagrid in the EditCommand:

DataBind()

2) In OnDataBoundEvent do the:

TextBox aBox = (TextBox)e.Item.Cells[2].Controls[0];
thisText.Text="set this text into the textbox";

Regards,

Daniel Roth
MCSD.NET
 
Hi Louise,

In this special situation, you should use

TextBox aBox = (TextBox)dgThing.Items[e.Item.ItemIndex].Cells[2].Controls[0];

to get reference of an ‘Edit’ item.

At this moment, e.Item is a normal datagrid item rather than an edit
datagrid item.

HTH

Elton Wang
(e-mail address removed)
 
Hi guys thanks for your responses i shall test them out later. Regarding the
first offering, I think I have already done that??

Elton W said:
Hi Louise,

In this special situation, you should use

TextBox aBox = (TextBox)dgThing.Items[e.Item.ItemIndex].Cells[2].Controls[0];

to get reference of an ‘Edit’ item.

At this moment, e.Item is a normal datagrid item rather than an edit
datagrid item.

HTH

Elton Wang
(e-mail address removed)



louise raisbeck said:
Hi, I have been on this for hours and just cannot work it out.

I have a datagrid. I set the edititem index to a row to edit, that works.
However if i want to put a value into this textbox it wont let me. I just
cant find the control in the controlcollection for the cell. Is it because it
hasnt rendered yet? (very annoying as i have set the edititem to the index,
done my databound etc.) why doesnt it recognise the textbox? so:

dgThing.EditItemIndex = e.Item.ItemIndex;
get my source and do DataBind();
TextBox aBox = (TextBox)e.Item.Cells[2].Controls[0]; //i found this code in
msdn so i know its right
thisText.Text="set this text into the textbox";

Because i have to just work out the cells (there is not cell id) i have just
counted. I am def on the right one. The one before it has some text in it,
so i know where i am in the row. but it cant find the textbox control having
just gone into edit mode. Has anyone got round this somehow? Another thing,
why do I have to databind again. I only want to change one row yet i have to
go to my sql database and run a complicated query EVERY time to populate data
which was already there, just to get my item to go into edit mode! is this
the only way?
 
Back
Top