6

(例:非表示の値に基づいて行の色を設定する)

このような非表示のセルがあるグリッドビューがある場合

<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>

その場合、(e.Row.Cells [2] .Text)を使用してonRowDataboundハンドラーでこれらの値を取得することは不可能のようです。

この問題を回避するには、BoundFieldsをVisible = "False"に設定しないで、デフォルトで表示="true"にします。コードビハインドのonRowDataboundハンドラーで必要な値を取得し、後でそれらを非表示にします。このような。

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;

        }

かなり環境に配慮しているので、多くのフォーラムをグーグルで調べてデバッグし、ハンドラーが非表示のデータバインドフィールドを認識できず、非表示のフィールドに基づいて行の色を設定する方法の答えを見つけることができなかったようです。他の人が見つけられるようにこれを投稿してください

専門家がより良いまたは代替の方法を知っている場合、おそらく彼らはいくつかのコード/コメントの歓声を追加することもできます!

4

1 に答える 1

6

ここDataItemに書いてある通りに使えると思います

   // 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"]);
于 2013-02-20T18:45:43.383 に答える