Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

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"
                OnRowCreated="gvTitles_RowCreated"
                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" 
                AutoGenerateColumns="False" 
                AutoGenerateEditButton="True"
                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" 
                    BorderStyle="Solid"></HeaderStyle>
                <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="Black" BorderStyle="Solid"></RowStyle>
                <Columns>
                    <asp:BoundField DataField="TitleID" HeaderText="ID" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Title">
                        <EditItemTemplate>
                            <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"
                                Text="*"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblTitle" runat="server" Text='<%# Bind("TitleDesc") %>'></asp:Label>                       
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Title Type">
                        <EditItemTemplate>
                            <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" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblTitleType" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Active">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlStatus" runat="server">
                                <asp:ListItem Value="Y">ACTIVE</asp:ListItem>
                                <asp:ListItem Value="N">INACTIVE</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblStatusFlag" runat="server" Text='<%# Bind("ActiveStatusFlag") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="IsMgr" HeaderText="Manager" ReadOnly="true" Visible="False" />
                    <asp:BoundField DataField="IsSupervisor" HeaderText="Supervisor" ReadOnly="true" Visible="False" />
                </Columns>
            </asp:GridView>

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.
Avatar of omgang
omgang
Flag of United States of America image

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;
    }
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh
dyarosh

ASKER

Thank you.  That gives me what I need.