I have tried in days to get the specified row and column data in the gridview c#, so that I could use them to compute a result and stored it into another column of the same row.
Fox example, a list of quotation records were shown. I need to update the 5th row record's quantity and unit price. With the updated data, the system will generate a new result of total price and show on the screen.
I have tried to use the gridview row index and cell but fail to do so.
So, when the item is Updating, you need to store the total price as well?
Why do you have a table that holds dirived data? It is much better not to store total price, and have it calculated in the select. Then you won't need to update it when the fields it is derived from are updated.
If you want to (or have to) stick to this data model, handle the RowUpdating event. In e.NewValues you will find price and quantity. Calculate the total price, and put it in e.NewValues, with the name of the total price field as key. Then you can use that
as parameter in the UpdateCommand.
Also, you can use FindControl directly on the row. No need to specify which Cell to search. This will make the code independent of what column goes where.
Have you tried using the debugger to see what happens when you click Add to Total?
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
ronnieang
Member
46 Points
36 Posts
using gridview in c# to get the specified row and column data for compute
Feb 29, 2012 12:31 AM|LINK
I have tried in days to get the specified row and column data in the gridview c#, so that I could use them to compute a result and stored it into another column of the same row.
Fox example, a list of quotation records were shown. I need to update the 5th row record's quantity and unit price. With the updated data, the system will generate a new result of total price and show on the screen.
I have tried to use the gridview row index and cell but fail to do so.
Please advice.
sujithkumar
Contributor
3664 Points
728 Posts
Re: using gridview in c# to get the specified row and column data for compute
Feb 29, 2012 02:51 AM|LINK
hi,
refer the below link .. you will find all info abt grid vie..
http://www.highoncoding.com/Categories/7_GridView_Control.aspx
For Row update in grid view..refer these
http://forums.asp.net/p/1255252/2331422.aspx
http://www.dotnetfunda.com/articles/article29.aspx
http://msdn.microsoft.com/en-us/library/ms972948.aspx
hope these will help you... If not please post your code..
basheerkal
Star
10672 Points
2426 Posts
Re: using gridview in c# to get the specified row and column data for compute
Feb 29, 2012 04:33 AM|LINK
In select command of the Datasource try retrieving values from database as [value1] * [value2] AS NewValue. Then bind the gridview.
Hope this helps
(Talk less..Work more)
ronnieang
Member
46 Points
36 Posts
Re: using gridview in c# to get the specified row and column data for compute
Feb 29, 2012 07:32 AM|LINK
I have try here and there. Do not know where I miss out. When I press add to total button, nothing happen. Please advice.
[aspx]
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="dm_quotation.aspx.cs" Inherits="dm_category" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns
="http://www.w3.org/1999/xhtml">
<
head runat
="server">
<title></title>
<style type="text/css">
.style2
{
height: 26px;
width: 320px;
font-size: x-large;
}
</style>
</
head
>
<
body
>
<form id="form1" runat="server">
<customControls:Header ID="Header1" runat="server" />
<h1 class="style2">Data Maintenance - Quotation</h1>
<p>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="quotation_id"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="quotation_id" HeaderText="ID" ReadOnly="True"
SortExpression="quotation_id">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="quotation_number" HeaderText="Quo No"
ReadOnly="True" SortExpression="quotation_number"">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="Quo Date" SortExpression="quotation_date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("quotation_date", " {0:yyyy/MM/dd}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("quotation_date", " {0:yyyy/MM/dd}") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="date required"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="yyyy/mm/dd"
MaximumValue="9999/12/31" MinimumValue="2000/01/01" Type="Date"></asp:RangeValidator>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name" SortExpression="company_name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("company_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="company_name"
DataValueField="company_name" AppendDataBoundItems="False"
SelectedValue='<%# Bind("company_name") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT company_category, company_name FROM [company master file] WHERE (company_category = N'customer')">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Descriptions"
SortExpression="product_descriptions">
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%# Bind("product_descriptions") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource3" DataTextField="product_descriptions"
DataValueField="product_descriptions" Height="22px"
SelectedValue='<%# Bind("product_descriptions") %>' Width="322px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT product_descriptions FROM [products master file]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Packing" SortExpression="product_packing">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("product_packing") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource4" DataTextField="product_packing_method"
DataValueField="product_packing_method" Height="22px"
SelectedValue='<%# Bind("product_packing") %>' Width="320px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT product_packing_method FROM [products master file]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Packing Type"
SortExpression="product_packing_type">
<ItemTemplate>
<asp:Label ID="Label5" runat="server"
Text='<%# Bind("product_packing_type") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource5" DataTextField="product_packing_type" DataValueField="product_packing_type"
Height="22px" SelectedValue='<%# Bind("product_packing_type") %>'
Width="388px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT product_packing_type FROM [products master file] ORDER BY product_packing_type">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Measurement"
SortExpression="unit_measurement">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("unit_measurement") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource6" DataTextField="unit_measurement"
DataValueField="unit_measurement"
SelectedValue='<%# Bind("unit_measurement") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT [unit_measurement] FROM [products master file]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"
Text='<%# Bind("order_quantity") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtQuantity" Display="Dynamic"
ErrorMessage="must key in"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator2" runat="server"
ControlToValidate="txtQuantity" Display="Dynamic" ErrorMessage="numeric field"
MaximumValue="9999999999999999" MinimumValue="1" Type="Double"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("order_quantity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="txtPrice" runat="server"
Text='<%# Bind("unit_price") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtPrice" Display="Dynamic" ErrorMessage="must key in"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator3" runat="server"
ControlToValidate="txtPrice" Display="Dynamic" ErrorMessage="numeric field"
MaximumValue="9999999999999999.99" MinimumValue=".01"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("unit_price") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="Generate Total">
<EditItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="AddToTotal" Text="Add to Total" />
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<%# Bind("total_price") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtTotal" runat="server" Text='<%# Bind("total_price") %>'></asp:Label>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="remarks" HeaderText="Remarks"
SortExpression="remarks">
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
DeleteCommand="DELETE FROM [quotation master file] WHERE [quotation_id] = @quotation_id"
InsertCommand="INSERT INTO [quotation master file] ([quotation_number], [quotation_date], [company_name], [product_descriptions], [product_packing], [product_packing_type], [order_quantity], [unit_measurement], [unit_price], [total_price], [delivery_number], [delivery_date], [your_order_number], [your_delivery_order_number], [remarks]) VALUES (@quotation_number, @quotation_date, @company_name, @product_descriptions, @product_packing, @product_packing_type, @order_quantity, @unit_measurement, @unit_price, @total_price, @delivery_number, @delivery_date, @your_order_number, @your_delivery_order_number, @remarks)"
ProviderName="<%$ ConnectionStrings:ge_mgoConnectionString1.ProviderName %>"
SelectCommand="SELECT quotation_id, quotation_number, quotation_date, company_name, product_descriptions, product_packing, product_packing_type, order_quantity, unit_measurement, unit_price, total_price, delivery_number, delivery_date, your_order_number, your_delivery_order_number, remarks FROM [quotation master file]"
UpdateCommand="UPDATE [quotation master file] SET [quotation_date] = @quotation_date, [company_name] = @company_name, [product_descriptions] = @product_descriptions, [product_packing] = @product_packing, [product_packing_type] = @product_packing_type, [order_quantity] = @order_quantity, [unit_measurement] = @unit_measurement, [unit_price] = @unit_price, [total_price] = @total_price, [delivery_number] = @delivery_number, [delivery_date] = @delivery_date, [your_order_number] = @your_order_number, [your_delivery_order_number] = @your_delivery_order_number, [remarks] = @remarks WHERE [quotation_id] = @quotation_id">
<DeleteParameters>
<asp:Parameter Name="quotation_id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="quotation_number" Type="Decimal" />
<asp:Parameter DbType="Date" Name="quotation_date" />
<asp:Parameter Name="company_name" Type="String" />
<asp:Parameter Name="product_descriptions" Type="String" />
<asp:Parameter Name="product_packing" Type="String" />
<asp:Parameter Name="product_packing_type" Type="String" />
<asp:Parameter Name="order_quantity" Type="Decimal" />
<asp:Parameter Name="unit_measurement" Type="String" />
<asp:Parameter Name="unit_price" Type="Decimal" />
<asp:Parameter Name="total_price" Type="Decimal" />
<asp:Parameter Name="delivery_number" Type="Decimal" />
<asp:Parameter DbType="Date" Name="delivery_date" />
<asp:Parameter Name="your_order_number" Type="String" />
<asp:Parameter Name="your_delivery_order_number" Type="String" />
<asp:Parameter Name="remarks" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="Date" Name="quotation_date" />
<asp:Parameter Name="company_name" Type="String" />
<asp:Parameter Name="product_descriptions" Type<span style="color: #0000ff; font-size: small;" size="2" color=%2
ronnieang
Member
46 Points
36 Posts
Re: using gridview in c# to get the specified row and column data for compute
Feb 29, 2012 07:33 AM|LINK
I have try here and there. Do not know where I miss out. When I press add to total button, nothing happen. Please advice.
[aspx]
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="dm_quotation.aspx.cs" Inherits="dm_category" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns
="http://www.w3.org/1999/xhtml">
<
head runat
="server">
<title></title>
<style type="text/css">
.style2
{
height: 26px;
width: 320px;
font-size: x-large;
}
</style>
</
head
>
<
body
>
<form id="form1" runat="server">
<customControls:Header ID="Header1" runat="server" />
<h1 class="style2">Data Maintenance - Quotation</h1>
<p>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="quotation_id"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="quotation_id" HeaderText="ID" ReadOnly="True"
SortExpression="quotation_id">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="quotation_number" HeaderText="Quo No"
ReadOnly="True" SortExpression="quotation_number"">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="Quo Date" SortExpression="quotation_date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("quotation_date", " {0:yyyy/MM/dd}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("quotation_date", " {0:yyyy/MM/dd}") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="date required"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="yyyy/mm/dd"
MaximumValue="9999/12/31" MinimumValue="2000/01/01" Type="Date"></asp:RangeValidator>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name" SortExpression="company_name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("company_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="company_name"
DataValueField="company_name" AppendDataBoundItems="False"
SelectedValue='<%# Bind("company_name") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT company_category, company_name FROM [company master file] WHERE (company_category = N'customer')">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Descriptions"
SortExpression="product_descriptions">
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%# Bind("product_descriptions") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource3" DataTextField="product_descriptions"
DataValueField="product_descriptions" Height="22px"
SelectedValue='<%# Bind("product_descriptions") %>' Width="322px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT product_descriptions FROM [products master file]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Packing" SortExpression="product_packing">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("product_packing") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource4" DataTextField="product_packing_method"
DataValueField="product_packing_method" Height="22px"
SelectedValue='<%# Bind("product_packing") %>' Width="320px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT product_packing_method FROM [products master file]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Packing Type"
SortExpression="product_packing_type">
<ItemTemplate>
<asp:Label ID="Label5" runat="server"
Text='<%# Bind("product_packing_type") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource5" DataTextField="product_packing_type" DataValueField="product_packing_type"
Height="22px" SelectedValue='<%# Bind("product_packing_type") %>'
Width="388px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT product_packing_type FROM [products master file] ORDER BY product_packing_type">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Measurement"
SortExpression="unit_measurement">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("unit_measurement") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource6" DataTextField="unit_measurement"
DataValueField="unit_measurement"
SelectedValue='<%# Bind("unit_measurement") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
SelectCommand="SELECT [unit_measurement] FROM [products master file]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"
Text='<%# Bind("order_quantity") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtQuantity" Display="Dynamic"
ErrorMessage="must key in"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator2" runat="server"
ControlToValidate="txtQuantity" Display="Dynamic" ErrorMessage="numeric field"
MaximumValue="9999999999999999" MinimumValue="1" Type="Double"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("order_quantity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="txtPrice" runat="server"
Text='<%# Bind("unit_price") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtPrice" Display="Dynamic" ErrorMessage="must key in"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator3" runat="server"
ControlToValidate="txtPrice" Display="Dynamic" ErrorMessage="numeric field"
MaximumValue="9999999999999999.99" MinimumValue=".01"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("unit_price") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="Generate Total">
<EditItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="AddToTotal" Text="Add to Total" />
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<%# Bind("total_price") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtTotal" runat="server" Text='<%# Bind("total_price") %>'></asp:Label>
</EditItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="remarks" HeaderText="Remarks"
SortExpression="remarks">
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ge_mgoConnectionString1 %>"
DeleteCommand="DELETE FROM [quotation master file] WHERE [quotation_id] = @quotation_id"
InsertCommand="INSERT INTO [quotation master file] ([quotation_number], [quotation_date], [company_name], [product_descriptions], [product_packing], [product_packing_type], [order_quantity], [unit_measurement], [unit_price], [total_price], [delivery_number], [delivery_date], [your_order_number], [your_delivery_order_number], [remarks]) VALUES (@quotation_number, @quotation_date, @company_name, @product_descriptions, @product_packing, @product_packing_type, @order_quantity, @unit_measurement, @unit_price, @total_price, @delivery_number, @delivery_date, @your_order_number, @your_delivery_order_number, @remarks)"
ProviderName="<%$ ConnectionStrings:ge_mgoConnectionString1.ProviderName %>"
SelectCommand="SELECT quotation_id, quotation_number, quotation_date, company_name, product_descriptions, product_packing, product_packing_type, order_quantity, unit_measurement, unit_price, total_price, delivery_number, delivery_date, your_order_number, your_delivery_order_number, remarks FROM [quotation master file]"
UpdateCommand="UPDATE [quotation master file] SET [quotation_date] = @quotation_date, [company_name] = @company_name, [product_descriptions] = @product_descriptions, [product_packing] = @product_packing, [product_packing_type] = @product_packing_type, [order_quantity] = @order_quantity, [unit_measurement] = @unit_measurement, [unit_price] = @unit_price, [total_price] = @total_price, [delivery_number] = @delivery_number, [delivery_date] = @delivery_date, [your_order_number] = @your_order_number, [your_delivery_order_number] = @your_delivery_order_number, [remarks] = @remarks WHERE [quotation_id] = @quotation_id">
<DeleteParameters>
<asp:Parameter Name="quotation_id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="quotation_number" Type="Decimal" />
<asp:Parameter DbType="Date" Name="quotation_date" />
<asp:Parameter Name="company_name" Type="String" />
<asp:Parameter Name="product_descriptions" Type="String" />
<asp:Parameter Name="product_packing" Type="String" />
<asp:Parameter Name="product_packing_type" Type="String" />
<asp:Parameter Name="order_quantity" Type="Decimal" />
<asp:Parameter Name="unit_measurement" Type="String" />
<asp:Parameter Name="unit_price" Type="Decimal" />
<asp:Parameter Name="total_price" Type="Decimal" />
<asp:Parameter Name="delivery_number" Type="Decimal" />
<asp:Parameter DbType="Date" Name="delivery_date" />
<asp:Parameter Name="your_order_number" Type="String" />
<asp:Parameter Name="your_delivery_order_number" Type="String" />
<asp:Parameter Name="remarks" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="Date" Name="quotation_date" />
<asp:Parameter Name="company_name" Type="String" />
<asp:Parameter Name="product_descriptions" Type="String" />
<asp:Parameter Name="product_packing" Type="String" />
<asp:Parameter Name="product_packing_type" Type="String" />
<asp:Parameter Name="order_quantity" Type="Decimal" />
<asp:Parameter Name="unit_measurement" Type="String" />
<asp:Parameter Name="unit_price" Type="Decimal" />
<asp:Parameter Name="total_price" Type="Decimal" />
<asp:Parameter Name="delivery_number" Type="Decimal" />
<asp:Parameter DbType="Date" Name="delivery_date" />
<asp:Parameter Name="your_order_number" Type="String" />
<asp:Parameter Name="your_delivery_order_number" Type="String" />
<asp:Parameter Name="remarks" Type="String" />
<asp:Parameter Name="quotation_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/de_quotation.aspx">Add new quotation</asp:HyperLink>
</p>
<p>
</p>
<p>
</p>
</form>
</
body
>
</
html
>
[aspx.cs]
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial class dm_category : System.Web.UI.
Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToTotal")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
TextBox txtboxQuantity =
(
TextBox)row.Cells[9].FindControl("txtQuantity");
string quantity = txtboxQuantity.Text;
TextBox txtboxPrice =
(
TextBox)row.Cells[10].FindControl("txtPrice");
string price = txtboxPrice.Text;
decimal mul = decimal.Parse(quantity) * decimal.Parse(price);
Label txtTotal = (Label)row.Cells[12].FindControl("txtTotal");
txtTotal.Text = mul.ToString(
"0.00");
}
}
}
superguppie
All-Star
48225 Points
8679 Posts
Re: using gridview in c# to get the specified row and column data for compute
Mar 01, 2012 08:44 AM|LINK
So, when the item is Updating, you need to store the total price as well?
Why do you have a table that holds dirived data? It is much better not to store total price, and have it calculated in the select. Then you won't need to update it when the fields it is derived from are updated.
If you want to (or have to) stick to this data model, handle the RowUpdating event. In e.NewValues you will find price and quantity. Calculate the total price, and put it in e.NewValues, with the name of the total price field as key. Then you can use that as parameter in the UpdateCommand.
Also, you can use FindControl directly on the row. No need to specify which Cell to search. This will make the code independent of what column goes where.
Have you tried using the debugger to see what happens when you click Add to Total?
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.