2016-09-25 17 views
5

すべての列をテンプレートとする動的aspグリッドビューがあります。 Gridviewの列も動的であり、列の数は毎回変わる可能性があります。Javascriptを使用した動的グリッドビューの複数列の合計の列

public void FillPoDetails() 
{ 
    DataTable dt = new DataTable();   
    dt = pmdata.createdatatable(int.Parse(Session["OurStyleid"].ToString()), int.Parse(Session["PoPackid"].ToString())); 
    GenerateTable(dt.Columns.Count, dt.Rows.Count,dt); 
    foreach (DataColumn col in dt.Columns) 
    { 
     //Declare the bound field and allocate memory for the bound field. 
     TemplateField bfield = new TemplateField(); 

     //Initalize the DataField value. 
     bfield.HeaderTemplate = new ArtWebApp.Controls.GridViewTemplate(ListItemType.Header, col.ColumnName); 

     //Initialize the HeaderText field value. 
     bfield.ItemTemplate = new ArtWebApp.Controls.GridViewTemplate(ListItemType.Item, col.ColumnName); 

     //Add the newly created bound field to the GridView. 
     GrdDynamic.Columns.Add(bfield); 
    } 
    GrdDynamic.DataSource = dt;  
    GrdDynamic.DataBind(); 
} 
public GridViewTemplate(ListItemType type, string colname) 
{ 
    //Stores the template type. 
    _templateType = type; 

    //Stores the column name. 
    _columnName = colname; 
} 

void ITemplate.InstantiateIn(System.Web.UI.Control container) 
{ 
    switch (_templateType) 
    { 
     case ListItemType.Header: 
      //Creates a new label control and add it to the container. 
      Label lbl = new Label();    
      //Allocates the new label object. 
      lbl.Text = _columnName; 
      lbl.CssClass = "Headerclass"; 
      //Assigns the name of the column in the lable. 
      container.Controls.Add(lbl);   
      //Adds the newly created label control to the container. 
      break; 
     case ListItemType.Item: 
      //Creates a new text box control and add it to the container. 
      TextBox tb1 = new TextBox();        
      //Allocates the new text box object. 
      tb1.DataBinding += new EventHandler(tb1_DataBinding);  
      //Attaches the data binding event. 
      tb1.Columns =6; 
      //Creates a column with size 4. 
      // tb1.Width = System.Web.UI.WebControls.Unit.Percentage(100); 
      tb1.Width = 100; 
      tb1.Wrap = true; 
      tb1.ID = "txt_" + _columnName; 
      if(_columnName== "ColorTotal") 
      { 
       tb1.CssClass = "ColorTotal"; 
      } 
      else if (_columnName == "Color") 
      { 
       tb1.CssClass = "Color"; 
      } 
      else 
      { 
       tb1.CssClass = "txtCalQty"; 
       tb1.Attributes.Add("onkeypress", "return isNumberKey(event,this)"); 
       tb1.Attributes.Add("onkeyup", "sumofQty(this)"); 
      }      
      container.Controls.Add(tb1);        
      //Adds the newly created textbox to the container. 
      break; 
    } 
} 

以下のコードを見つけて、私はKeyDownイベントイベントにJavaScript関数を追加し、その作業を明確に

//calculate the sum of qty on keypress 
    function sumofQty(objText) { 


     var cell = objText.parentNode; 

     var row = cell.parentNode; 

     var sum = 0; 
     var textboxs = row.getElementsByClassName("txtCalQty"); 

     for (var i = 0; i < textboxs.length; i++) 
     { 
      sum += parseFloat(textboxs[i].value); 
     } 
     var textboxtotalqtys = row.getElementsByClassName("ColorTotal"); 
     textboxtotalqtys[0].value = sum.toString();   

    } 

た行の合計を取得するために、順序どおり、結果は enter image description here以下のようなものですしてください

誰もが各列(すべて同じcssclass)の合計を見つけるのを手伝ってください。そして、それをt彼は列をループすることができないので、Sizetotal行

答えて

1

すべてのテキストボックスに行IDと列IDをhtml5 data attributesで指定します。 javascript(jQuery)では、列idを通してテキストボックスをフィルタリングします。

例:ところで

.. 
var sum = 0; 
$("input[data-column-id='" + selectedColumnId + "']").each(function(index) 
{ 
    sum += parseFloat($(this).val()); 
}); 
.. 

、jQueryのを使用します。すごい。

1

非常に簡単な方法があります。

各列にフッターとラベルを追加するデータベースの最適な計算はLINQとグループ化し、各列のフッターラベルコントロールを見つけ、それらのフッターのラベルコントロールに値をバインドします。負荷。

は、コードについてはこちらをご覧ください:グリッド内

.ASPXページ:

<asp:TemplateField HeaderText="Total"> 
    <ItemTemplate> 
    <asp:Literal ID="ltrlTotal" Text='<%#Eval("Total") %>' runat="server"> </asp:Literal> // For Sub Total 
</ItemTemplate> 
<FooterTemplate> 
<strong><asp:Literal ID="ltrlGrandTotal" runat="server"> // This is Grand Total 
    </asp:Literal></strong> 
</FooterTemplate>      
</asp:TemplateField> 

C#コード:

var searchResult = soService.SearchResult(companyId); 
      var grandTotal = searchResult.Select(so => so.Total).Sum(); 
      searchResult.All(aa => aa.GrandTotal == grandTotal); 

      gridSo.DataSource = searchResult; 
      gridSo.DataBind(); 

      if (searchResult.Count > 0) 
      { 
       Literal ltrlGrandTotal = gridSo.FooterRow.FindControl("ltrlGrandTotal") as Literal; 
       if (ltrlGrandTotal != null) 
        ltrlGrandTotal.Text = string.Format("Grand Total : $ {0}", grandTotal); 
      } 
+0

こんにちはShyamしかし、gridviewは動的で、列の値がクライアントによって変更されるたびに計算が再度行われるべきです –

+0

jqueryの例を提供してください –

1

あなたの条件について確認してくださいが、これはあなたを助けるかもしれませんデータをグリッドにバインドした後、再び列リストをループする

関連する問題