Link to home
Start Free TrialLog in
Avatar of skaykay
skaykay

asked on

Getting old value of the cell - Datagrid

Hi Experts,

I've a datagrid in a webform in which I have 2 columns, both defined as templatecolumns. First column is a textbox and second is a dropdownlist. E'thing is fine so far. What I need is this.

When I try to edit this, I need to fetch the current value of both the cells before I edit. How to do this? It works fine if I use a bound column and use this..

e.Item.Cells[1].Text;

But this I reckon won't work with a templatecolumn. How will I fetch the value of the cells? I hope I'm clear with the problem.

TIA & cheers,
kaykay!
Avatar of mmarinov
mmarinov

Hi,

to get contents of the cell when you use Template column you have to use this

TextBox _tb = (TextBox)e.Item.FindControl("id of the textbox control");
then _tb.Text will return the contents of the text box


for the dropdownlist it is absoulutely the same:

DropDownList _drp = (DropDownList)e.Item.FindControl(" id of the dropdownlist control");


Regards,
B..M
Avatar of skaykay

ASKER

Thanks for your response.

This findControl(<ctrlname>) gets the current value of the cell after I make any changes. What if I need the value of the cell before I make the changes. I need these values too to write my update command.

hope I'm clear.
i show you the way
the value that this code will get depends on the place you execute it
so, in your case you have to get this value before bind the datagrid, because after that you'll be able to change it

so in my opinion the best way to get values is to keep the dataset in a session object so you will have all the values
then when submit the page you will have the new one and you can easy compare them

B..M
Avatar of skaykay

ASKER

I get your point, but is this the solution? I'm currently struck at Edit and Delete function of a  datagrid. I choose to edit and then change the value of the columns. Now at edit, I can't update the record coz I don't have the previous value of the column.

Now where do you suggest me to populate the dataset and how do I check the old and new record correspondingly?

kaykay!
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skaykay

ASKER

Thanks BM.. I was pondering over the point that you said and something else struck me... I did a workaround to this by adding corresponding non visible readonly columns to which I need the old value. And then use this in my function.

Not sure if this is efficient one, but solves my purpose. Do you have any comments on this workaround?
yes, this is also a solution and it is not a problem to use it
actually a prefer to use the solution with the session because i have to keep the data in the memory because i don't want to call every time to the database
unfortunatelly this solution is not applicable in every case becase in most of the cases the information is updated many times and you have to show to the client the newest one

so if this workaround work for you - it is great
may one day when you have time you can check the other solution and compare them

Regards
B..M
Avatar of skaykay

ASKER

yep, I should certainly do that a day. Thanks a lot for your time and response.

Cheers & have fun!
kaykay!