• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

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.
1 Solution
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

dyaroshAuthor Commented:
Thank you.  That gives me what I need.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now