Asked by:
gridview

Question
-
User-587451504 posted
Hi all I need help
I want to sum two columns and put the reasult in third columns
when I put 5 in column 1 and put 10 in column 2 the reasult will apear in column 3 =15
for each row
Wednesday, May 1, 2019 12:14 PM
All replies
-
User475983607 posted
Share the code you have written up to this point.
Wednesday, May 1, 2019 12:27 PM -
User-587451504 posted
I have no code Iasked for
I need help
Wednesday, May 1, 2019 12:42 PM -
User-587451504 posted
How can I sum the values of two GridView not dataGridView columns and store the result in a third column for each row ?
Wednesday, May 1, 2019 12:55 PM -
User475983607 posted
safaruda
How can I sum the values of two GridView not dataGridView columns and store the result in a third column for each row ?
It is very difficult to provide assistance without source code as there are several methods to solve this very fundamental GridView programming problem. I recommend doing the SUM in SQL then bind the GridView as normal. GridViews are generally used to Read/Update table records so it makes sense to use SQL for data manipulation.
Lets assume this is your SQL to fetch records.
SELECT Col1, Col2 FROM SomeTable
Then a very simple change yields the SUM.
SELECT Col1, Col2, (Col1 + Col2) AS [TheSum] FROM SomeTable
Can you at least post the GridView source code without the SUM? Do you know how to bind records to the GridView?
Wednesday, May 1, 2019 1:37 PM -
User-587451504 posted
Hello
I create a database in SQL server 2014 and I work on ASP.NET\C#\webform 2015
I create a table have 3 columns (col1,col2,col3)when I put value in columns 1 and 2 the reasult appear in col 3 in gridview and make the code in C# not on SQL SERVER
but it does not work (dataGridView) not (GridView) I change the code to GridView
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
try {
(sender as DataGridView).CurrentRow.Cells[2].Value =
Convert.ToSingle((sender as DataGridView).CurrentRow.Cells[0].Value) +
Convert.ToSingle((sender as DataGridView).CurrentRow.Cells[1].Value);
}catch(Exception ex) { }
}the errore massage
Compiler Error Message: CS0246: The type or namespace name 'gvMovingBudgetCellEventArgs' could not be found (are you missing a using directive or an assembly reference?)
do you have teamviewer
Wednesday, May 1, 2019 10:31 PM -
User-158764254 posted
but it does not work (dataGridView) not (GridView) I change the code to GridViewWhat you've posted is only applicable to windows forms apps / DataGridViews.
Is this a WinForms app?
Wednesday, May 1, 2019 10:59 PM -
User475983607 posted
I recommend using SQL or you business layer to sum values as that's what SQL and your business layer are for. The GridView is for reading and writing records. You're moving business logic to the UI which clutters the UI and you end up with logic all over the place.
Anyway, use the row data bound event to sum server control values found in the GridView if you want to code this the hard way.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewDemo3.aspx.cs" Inherits="WebFormsDemo.GridViewDemo3" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False" OnRowDataBound="MyGridView_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Col1"> <ItemTemplate> <asp:TextBox ID="Col1Text" runat="server" Text='<%# Bind("Col1") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Col2"> <ItemTemplate> <asp:TextBox ID="Col2Text" runat="server" Text='<%# Bind("Col2") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Col3"> <ItemTemplate> <asp:TextBox ID="Col3Text" runat="server" Text='<%# Bind("Col3") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> <div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebFormsDemo { public partial class GridViewDemo3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { MyGridView.DataSource = CreateDataTable(); MyGridView.DataBind(); } } protected DataTable CreateDataTable() { // Create a new table. DataTable dt = new DataTable("Items"); // Create the columns. dt.Columns.Add("Col1", typeof(int)); dt.Columns.Add("Col2", typeof(int)); dt.Columns.Add("Col3", typeof(int)); //Add data to the new table. for (int i = 0; i < 10; i++) { DataRow tableRow = dt.NewRow(); tableRow["Col1"] = i; tableRow["Col2"] = i + 3; tableRow["Col3"] = 0; dt.Rows.Add(tableRow); } return dt; } protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { TextBox col1 = e.Row.FindControl("Col1Text") as TextBox; TextBox col2 = e.Row.FindControl("Col2Text") as TextBox; TextBox col3 = e.Row.FindControl("Col3Text") as TextBox; col3.Text = (int.Parse(col1.Text) + int.Parse(col2.Text)).ToString(); } } } }
Wednesday, May 1, 2019 11:27 PM -
User409696431 posted
You say you are using webforms. Your error is because webforms does not have a dataGridView. That is in Windows Forms. Webforms does have a GridView, with its own set of event args for different events.
Thursday, May 2, 2019 4:28 AM