Solved

Getting old value of the cell - Datagrid

Posted on 2004-08-10
8
1,153 Views
Last Modified: 2008-02-26
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!
0
Comment
Question by:skaykay
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
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
0
 
LVL 4

Author Comment

by:skaykay
Comment Utility
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.
0
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
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
0
 
LVL 4

Author Comment

by:skaykay
Comment Utility
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!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Accepted Solution

by:
mmarinov earned 150 total points
Comment Utility
ok, here is the way

Page_load
 if not ispostback then
    call BindGrid sub
 end if

BindGrid
    if Session("DataSourceOld") Nothing
    get dataset(or datatable or the object that will be set the datasource property of datagrid to )
    add this object to a session object named DataSourceOld
    End If
    bind grid

DataGrid edit command
  set the edit item
  call BindGrid sub

DataGrid save commnad
  from session collection with key DataSourceOld get the old data
  get the new data
  compare them
  update date
  call BindGrid sub


Hope this helps
Regards
B..M
0
 
LVL 4

Author Comment

by:skaykay
Comment Utility
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?
0
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
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
0
 
LVL 4

Author Comment

by:skaykay
Comment Utility
yep, I should certainly do that a day. Thanks a lot for your time and response.

Cheers & have fun!
kaykay!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now