2011-01-15 6 views
0

Ich möchte Ihre Hilfe mit meinem Code. Ich schrieb diesen QuellcodeWie finde ich die Gesamtzahl in den Spalten gridview

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowLand.aspx.cs" Inherits="ShowLand" %> 

<!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> 
</head> 
<body MS_POSITIONING="GridLayout" bgColor="#FFF8DC" aLink="#330066"> 
    <form id="form1" runat="server"> 
<asp:Label id="Label6" 
     style="Z-INDEX: 101; LEFT: 176px; POSITION: absolute; TOP: 8px; text-align: center;" runat="server" 
       Font-Size="X-Large" Font-Italic="True" Font-Underline="True" Width="608px" ForeColor="Navy" 
       Font-Bold="True">Land For Sale</asp:Label> 
    <br /> 
    <br /> 
    <br /> 
<div> 
     <asp:GridView ID="gridView1" runat="server" CellPadding="4" ForeColor="#333333" 
      GridLines="None"> 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
      <Columns> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:CheckBox ID="chkSelect" runat="server" /> 
         <asp:HiddenField ID="hdValue" runat="server" Value='<%#Eval("ID") %>' /> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
      <EditRowStyle BackColor="#999999" /> 
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
      <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
      <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
      <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
     </asp:GridView> 
    </div> 
    <div> 
     <asp:Button ID="btnMove" runat="server" Text="Add To Cart" OnClick="btnMove_Click" /> 
    </div> 
    <div> 

     <asp:GridView ID="gridView2" runat="server" CellPadding="4" ForeColor="#333333" 
      GridLines="None"> 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
     <asp:TemplateField HeaderText="Qty"> 
      <ItemTemplate> 
      <asp:TextBox ID="tbQty" runat="server" Width="25px" 
      MaxLength="3" /> 
      </ItemTemplate> 
      <ItemStyle Width="25px" HorizontalAlign="Center"/> 
     </asp:TemplateField> 
     </Columns> 
      <EditRowStyle BackColor="#999999" /> 
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
      <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
      <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
      <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
     </asp:GridView> 
    </div> 
    <br /> 
    <br /> 
    <br /> 
    </form> 
</body> 

</html> 

und i erstellt auch diese 2 Gridviews wie dies in ShowLand.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

public partial class ShowLand : System.Web.UI.Page 
{ 
    const string key = "MyDataSource3"; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindGridView(); 
     } 

    } 
    private void BindGridView() 
    { 
     if (Session[key] == null) 
     { 
      gridView1.DataSource = GetDataSource(); 
      gridView1.DataBind(); 
     } 
     else 
     { 
      gridView1.DataSource = (DataTable)Session[key]; 
      gridView1.DataBind(); 
     } 

    } 
    protected DataTable GetDataSource() 
    { 
     try 
     { 
      DataTable dt = new DataTable(); 
      dt = new DataTable(); 
      dt.Columns.Add("ID", typeof(int)).AutoIncrement = true; 
      dt.Columns.Add("Name", typeof(string)); 
      dt.Columns.Add("Price(Grouch)/Hectares", typeof(float)); 
      DataColumn[] keys = new DataColumn[2]; 
      keys[0] = dt.Columns["ID"]; 
      dt.PrimaryKey = keys; 
      dt.Rows.Add("1", "Seaside Location", 1.5); 
      dt.Rows.Add("2", "Arable Land", 0.1); 
      dt.Rows.Add("3", "Foothills of the mountains", 1.5); 
      dt.Rows.Add("4", "Industrial Land", 0.1); 
      dt.Rows.Add("5", "Rolling Farmland", 0.5); 
      Session[key] = dt; 
      return dt; 

     } 
     catch 
     { 
      return null; 
     } 
    } 
    protected void btnMove_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      DataTable dtMain = Session[key] as DataTable; 
      //copy the schema of source table 
      DataTable dtClone = dtMain.Clone(); 
      foreach (GridViewRow gv in gridView1.Rows) 
      { 
       CheckBox chk = gv.FindControl("chkSelect") as CheckBox; 
       HiddenField hdValue = gv.FindControl("hdValue") as HiddenField; 
       if (chk.Checked) 
       { 
        //get only the rows you want 
        DataRow[] results = dtMain.Select("ID=" + hdValue.Value + ""); 
        //populate new destination table 
        foreach (DataRow dr in results) 
        { 
         dtClone.ImportRow(dr); 
        } 
       } 
       gridView2.DataSource = dtClone; 
       gridView2.DataBind(); 
      } 
     } 
     catch 
     { 
      BindGridView(); 
     } 

    } 
} 

In diesem Code der Benutzer mit der Checkbox seiner Wahl gewählt und mit der Knopf, um diese Wahl in der zweiten gridview hinzuzufügen. Auch im Textkasten kann die Quantität geben. Ich möchte einen Weg finden, wie man die Quantität mit der Preisspalte multipliziert. Wie kann ich das tun?

Antwort

0

Während Sie die Gridview durchlaufen, haben Sie Zugriff auf die Zeile und ihre Zellen. Also der Wert Ihrer Menge und Preis von entsprechenden Zellen und die Berechnung z. gv.Cells [2] .Text

prüfen dies: Calculate Price Total

By the way, ich sah nur noch einmal den Code und sehen nicht eine Spalte Menge?

0

Es wird am besten sein, insgesamt aus der Datentabelle zu berechnen, wo Sie sind verbindlich, dass gridview wie diese

 datatable.Compute("Sum(ColumnName)","") 

Weil wir für Schleife hier nicht verwenden müssen und so viel Zeit wird hier gespeichert.

Verwandte Themen