Get values ​​from hidden cells in gridview asp.net c # using onrowdatabound

(e.g. to set rowcolor based on a hidden value)

if you have a gridview that has hidden cells like

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

then it turns out to be impossible to get these values ​​in the onRowDatabound handler using (e.Row.Cells [2] .Text)

I circumvented this problem by not setting any of the BoundFields to Visible = "False" so by default they are visible = "true". getting the values ​​that I need in the onRowDatabound handler in the code behind, and then making them invisible afterwards. like this.

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

was pretty green, it took me many times to go into many forums and debug it to understand that the handler did not see the hidden data binding fields, and I did not seem to find an answer to the question of how to set rowcolour based on the hidden field, so I although id just posted it for others to find

If any experts know a better or alternative way, perhaps they can also add cheers code / comments!

+6
source share
1 answer

I think you could use a DataItem as it says 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"]); 
+4
source

All Articles