2013-02-20 39 views
6

(ví dụ để thiết lập rowcolor dựa trên giá trị ẩn)nhận được giá trị từ các tế bào Ẩn trong asp.net GridView C# sử dụng onrowdatabound

nếu bạn có một GridView mà đã ẩn các tế bào như thế này

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

sau đó nó xuất hiện không thể để có được những giá trị trong xử lý onRowDatabound sử dụng (e.Row.Cells [2] .Text)

tôi gặp vấn đề này bằng cách không đặt bất kỳ BoundFields thành Visible = "False" để chúng hiển thị = "true" theo mặc định. nhận được các giá trị tôi cần trong Trình xử lý onRowDatabound trong đoạn mã phía sau và sau đó làm cho chúng vô hình sau đó. như thế này.

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; 

     } 

là một khá xanh nó đã cho tôi một việc tốt googling vòng nhiều diễn đàn và gỡ lỗi để tìm ra rằng xử lý không thể nhìn thấy cánh đồng databound ẩn và i couldnt dường như để tìm câu trả lời về cách thức để thiết lập rowcolour dựa trên một trường ẩn vì vậy tôi cho dù id chỉ đăng bài này để những người khác tìm thấy

Nếu bất kỳ chuyên gia nào biết một cách tốt hơn hoặc thay thế có lẽ họ cũng có thể thêm một số mã/nhận xét cổ vũ!

Trả lời

4

Tôi nghĩ rằng bạn có thể sử dụng DataItem như nó nói 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

Cảm ơn V nhiều Kaf ... và đã có tôi nghĩ tôi là một quần smarty hoặc một cái gì đó !! – Sonny123

Các vấn đề liên quan