Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get values from Hidden cells in gridview asp.net c# using onrowdatabound

(eg to set rowcolor based on hidden value)

if you have a gridview which has hidden cells like this

<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 appears impossible to get those values in the onRowDatabound Handler using (e.Row.Cells[2].Text)

i got around this problem by not setting any of the BoundFields to Visible = "False" so they are visible = "true" by default. getting the values i needed 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;

        }

being a fairly green it took me a good deal of googling round many forums and debugging to figure out that the handler cant see hidden databound fields and i couldnt seem to find the answer of how to set rowcolour based on a hidden field so i though id just post this up for others to find

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

like image 914
Sonny123 Avatar asked Feb 20 '13 18:02

Sonny123


1 Answers

I think you could use 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"]);
like image 120
Kaf Avatar answered Oct 19 '22 18:10

Kaf