GridView - Getting value(s) of currently edited row

mcdonnc2004
01-24-2007, 12:34 PM
Hi all,

I am trying to do what I thought would be a simple page but it is turning out to be challenging for me!

I have a page with a GridView containing a bunch of offers that would be converted to orders. Say a sales rep submitted an offer through our purchasing system. This would be sent to my web page. The person who converts these offers would check my page to see what offers are waiting. They look up the order number on the purchasing system and check the offer is valid. If it is, they tick the box. This works fine as demonstrated here (feel free to play with it):

http://cherrysoft.co.uk/OrderGrid.aspx

My problem is, I'd like it to be a little more complex. I want the page to be able to send an e-mail to the sales rep to say the offer is valid. Ideally I'd like this to fire when the Update link is clicked, so the database is updated and the e-mail is sent. So what I need the page to do is to check the primary key field for the current row being updated. Once I have that ID value I can then use code to connect to various other tables and retrieve the info (email address, full name, etc) but I need the ID first and I cannot work out how to get it when the update link is clicked!

My database fields are:

OrderID [Primary Key - ID field]
sales_rep [Char]
order_no [Int]
order_date [DateTime]
order_value[Int]
order_checked [Bit]

How can I retrieve the OrderID of the row currently being updated and store it in a variable for use in the code-behind?

Thanks.

wayneph
01-24-2007, 09:54 PM
I haven't really played with it, but I think you're looking for the either the RowUpdating or RowUpdated event of the GridView object.

The EventArgs should allow you to access the data on the grid to send an email or do something else with it.

mcdonnc2004
01-25-2007, 01:54 AM
I thought that might be the case. With the OrderID field not being shown in the GridView, how would I gain access to that value? I originally thought I could query the @OrderID parameter of the SqlDataSource control but that doesn't seem possible.

Eduardo Lorenzo
01-25-2007, 02:01 AM
is OrderID not shown or not included? You can still refer to it even if it is not visible.

mcdonnc2004
01-25-2007, 02:08 AM
OrderID is selected in the SQL statement, just not shown on the GridView. Here is my SQL statement:

SELECT * FROM [orders] ORDER BY [order_value], [order_date], [sales_rep]

My GridView looks like this:

<asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1" DataKeyNames="OrderID" AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowDeleteButton="False" ShowEditButton="True" />
<asp:BoundField DataField="sales_rep" HeaderText="Sales Rep Code" ReadOnly="True"
SortExpression="sales_rep" />
<asp:BoundField DataField="order_no" HeaderText="Order Number" ReadOnly="True" SortExpression="order_no" />
<asp:BoundField DataField="order_date" HeaderText="Order Date" ReadOnly="True" SortExpression="order_date" />
<asp:BoundField DataField="order_value" HeaderText="Value" ReadOnly="True" SortExpression="order_value" />
<asp:CheckBoxField DataField="order_checked" HeaderText="Order OK" SortExpression="order_checked" />
</Columns>
</asp:GridView>

Eduardo Lorenzo
01-25-2007, 02:21 AM
Include OrderID as an invisible column in the grid so you can refer to it.

mcdonnc2004
01-25-2007, 02:22 AM
I took wayneph's advice and I put my code in the RowUpdated event and used the following to get the value of OrderID:

e.NewValues.Item("OrderID").ToString()

Thanks guys. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum