2013-02-20 21 views
6

(例えば隠された値に基づいてrowcolor設定する)onrowdatabound使用してGridViewのasp.netのC#の中に隠された細胞から値を取得する

を、あなたはそれをこの

<asp:GridView ID="Timeevents" runat="server" 
     OnRowDataBound="Timeevents_RowDataBound" 
     OnRowCommand = "Timeevents_RowCommand" 
     AutoGenerateColumns="False"> 
     <columns> 
     <asp:BoundField DataField="CaseID" HeaderText="CaseID" Visible = "False" /> 
     <asp:BoundField DataField="caseworkerID" HeaderText="CwID" Visible = "False" /> 
     <asp:BoundField DataField="EventTypeID" HeaderText="EvTypeID" Visible = "False" /> 
     <asp:BoundField DataField="CaseWorker" HeaderText="Case Worker" /> 
     <asp:BoundField DataField="EventDate" HeaderText="Event Date" /> 
     <asp:BoundField DataField="Code" HeaderText="Code" /> 
     <asp:BoundField DataField="TotalUnits" HeaderText="Total Units" /> 
     <asp:BoundField DataField="EventType" HeaderText="Event Type" /> 
     <asp:BoundField DataField="UnitCost" HeaderText="Unit Cost" /> 
     <asp:BoundField DataField="TotalCost" HeaderText="Total Cost"/> 
     <asp:TemplateField HeaderText="ADD"> 
       <ItemTemplate> 
        <asp:Button ID="AddUnit" runat="server" Text=" +1 " 
        CommandName="AddUnit" 
        CommandArgument='<%# Eval("CaseID")+ ";" + Eval("CaseworkerID")+ ";" + Eval("EventDate")+ ";" + Eval("EventTypeID")+ ";" + ("1")%>'/> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
</asp:GridView> 

のような細胞を隠されたGridViewのを持っている場合

BoundFieldsをVisible = "False" に設定しないようにしてこの問題を回避しました。これらは表示されるようになりました。デフォルトでは "true"です。 onRowDataboundハンドラで必要な値をコードの中に入れてから、それらを非表示にします。このような。それは、ハンドラは隠しデータバインドされたフィールドを参照してください傾けることを把握してくれラウンド多くのフォーラムやデバッグをグーグルの良い取引を取って、私は上のベースrowcolourを設定する方法の答えを見つけるように見えるcouldntのかなり緑であること

protected void Timeevents_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { // just for the datarows 
       int a = (int.Parse(e.Row.Cells[2].Text)); 

       if (a % 2 == 0) 
       { 
        e.Row.BackColor = System.Drawing.Color.Gainsboro; 
       } 
       else 
       { 
        e.Row.BackColor = System.Drawing.Color.White; 
       } 
      } 
      // end if so this applies to header and data rows 
      e.Row.Cells[0].Visible = false; 
      e.Row.Cells[1].Visible = false; 
      e.Row.Cells[2].Visible = false; 

     } 

隠されたフィールドなので、私はIDだけでもこれを投稿して他の人に見つけることができます

いずれかのエキスパートがより良いまたは代替の方法を知っていれば、おそらくいくつかのコード/コメントを追加することもできます 歓声!

答えて

4

私は...それはhere

// the underlying data item is a DataRowView object. 
    DataRowView rowView = (DataRowView)e.Row.DataItem; 

    // Retrieve the EventTypeID value for the current row. 
    int a = Convert.ToInt32(rowView["EventTypeID"]); 
+2

おかげV多くKAFを言うように、あなたがDataItemを使用することができると思うと、私は、私はSmartyのズボンか何かだと思っていました! – Sonny123

関連する問題