Trouble creating a custom column in my gridview using C#

I have a DataTable that I am binding to a gridview.  The DataTable has two fields that are flags indicating the type of Title the record represents.  The first field is called IsMgr and the second IsSupervisor.  What I want my gridview to display is a column called Title Type and to display Manager if the IsMgr flag in the datatable is Y, Supervisor if the IsSupervisor flag in the datatable is Y otherwise display blank.

I thought I could do this in the RowCreated event but I can't access the data in the IsMgr and IsSupervisor colulmns.  (I added the fields from the datatable to the gridview as hidden fields).

Can someone please tell what I am doing wrong?

Here is my GridView markup:
            <asp:GridView ID="gvTitles" runat="server" CellPadding="5" 
                OnPageIndexChanging="gvTitles_PageIndexChanging" OnRowCancelingEdit="gvTitles_RowCancelingEdit"
                OnRowEditing="gvTitles_RowEditing" OnRowUpdating="gvTitles_RowUpdating"
                AlternatingRowStyle-BackColor="#0099CC" AlternatingRowStyle-BorderColor="Black" 
                AlternatingRowStyle-BorderStyle="Solid" AlternatingRowStyle-ForeColor="White" 
                AlternatingRowStyle-HorizontalAlign="Left" AlternatingRowStyle-VerticalAlign="Middle" 
                BorderStyle="Solid" HeaderStyle-BackColor="Silver" BorderColor="Black" 
                HeaderStyle-BorderColor="Black" HeaderStyle-BorderStyle="Solid" 
                HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" 
                HorizontalAlign="Left" RowStyle-BorderColor="Black" RowStyle-BorderStyle="Solid" 
                RowStyle-HorizontalAlign="Left" RowStyle-VerticalAlign="Middle" 
                EmptyDataText="No Titles Found" AllowPaging="True">
                <AlternatingRowStyle HorizontalAlign="Left" VerticalAlign="Middle" BackColor="#0099CC" BorderColor="Black" 
                    BorderStyle="Solid" ForeColor="White"></AlternatingRowStyle>
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="Silver" BorderColor="Black" 
                <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="Black" BorderStyle="Solid"></RowStyle>
                    <asp:BoundField DataField="TitleID" HeaderText="ID" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Title">
                            <asp:TextBox ID="tbEdit" runat="server" ToolTip="Title" Text='<%# Bind("TitleDesc") %>'
                                CssClass="TextBoxFormat" MaxLength="30"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="tbTitleRequiredFieldValidator2" runat="server" 
                                ErrorMessage="Title is required" BackColor="Red" Font-Bold="True" ForeColor="White"
                                ControlToValidate="tbEdit" Display="Dynamic" validationgroup="Info"
                            <asp:Label ID="lblTitle" runat="server" Text='<%# Bind("TitleDesc") %>'></asp:Label>                       
                    <asp:TemplateField HeaderText="Title Type">
                            <asp:RadioButton ID="rbEdit0NonManagement" runat="server" Checked="True" GroupName="TitleType" Text="Non-Management" ToolTip="Non-Management Title Flag" ValidationGroup="Info" />
                            <asp:RadioButton ID="rbEditSupervisor" runat="server" Checked="False" GroupName="TitleType" Text="Supervisor" ToolTip="Supervisor Title Flag" ValidationGroup="Info" />
                            <asp:RadioButton ID="rbEditManager" runat="server" Checked="False" GroupName="TitleType" Text="Manager" ToolTip="Manager Title Flag" ValidationGroup="Info" />
                            <asp:Label ID="lblTitleType" runat="server" Text=""></asp:Label>
                    <asp:TemplateField HeaderText="Active">
                            <asp:DropDownList ID="ddlStatus" runat="server">
                                <asp:ListItem Value="Y">ACTIVE</asp:ListItem>
                                <asp:ListItem Value="N">INACTIVE</asp:ListItem>
                            <asp:Label ID="lblStatusFlag" runat="server" Text='<%# Bind("ActiveStatusFlag") %>'></asp:Label>
                    <asp:BoundField DataField="IsMgr" HeaderText="Manager" ReadOnly="true" Visible="False" />
                    <asp:BoundField DataField="IsSupervisor" HeaderText="Supervisor" ReadOnly="true" Visible="False" />

Open in new window

Here is my Code Behind for the RowCreated event (the Title Type column only displays blank and the e.Row.Cells[3].Text and e.Row.Cells[4].Text contain "")
        protected void gvTitles_RowCreated(Object sender, GridViewRowEventArgs e)
            // Set TitleType based on value of IsMgr and IsSupervisor

            // Find the Title Type Label
            Label lbl = null;
            if (e.Row.RowType == DataControlRowType.DataRow)
                lbl = e.Row.FindControl("lblTitleType") as Label;

                // If title type is Manager, display Manager
                if (lbl != null && e.Row.Cells[3].Text == "Y")
                    lbl.Text = "MANAGER";
                // If title type is Supervisor, display Supervisor
                else if (lbl != null && e.Row.Cells[4].Text == "Y")
                    lbl.Text = "SUPERVISOR";

Open in new window

lbl contains the cell for the Title Type column and if I hardcode a value using lbl.Text = "some value", the some value is displayed in the GridView.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omgangIT ManagerCommented:
I'm not sure on this but I believe I ran into a very similar issue a year or so ago.  I have a gridview where I wanted three columns to be hidden but needed to be able to access the values in those columns from the code behind.  I ended up resolving the problem by leaving the three columns visible in gridview control but then hiding them during the RowCreated event.  I'm wondering if doing the same will resolve your problem, e.g. set visibility for those columns back to True on the gridview control and then hide them at the end of your RowCreated event.

OM Gang

    protected void BillsGridViewBE_RowCreated(object sender, GridViewRowEventArgs e)
        //hide last three columns of gridview - BusinessUnit, TrackingAssignmentID, Bill_DFT_NO
        e.Row.Cells[22].Visible = false;
        e.Row.Cells[23].Visible = false;
        e.Row.Cells[24].Visible = false;
Try doing it in RowDataBound event. You can get the value directly from the datarowview
if(e.Row.RowType== DataControlRowType.DataRow)
            DataRowView drv = e.Row.DataItem as DataRowView;
            string IsManager = (string)drv["IsMgr"];
            string IsSuperVisor= (string)drv["IsSupervisor"];
            //Do you finding label and setting the value here.

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
Thank you.  That gives me what I need.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.