Search code examples
c#asp.netgridviewtrim

Trim Gridview field with ellipsis and tooltip in C#


I am running into some trouble with this GridView. Yesterday I actually had this function working, but then ended up changing my Gridview to use ItemTemplate instead of Databound fields, and now the function no longer works, and all my attempts to find the problem have fallen short. I'd like to be able to trim the longer fields, and show the full text on tooltip.

Here is my GridView code:

<asp:GridView ID="ItemView" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    HorizontalAlign="Center"
    PageSize="10" HeaderStyle-BackColor="#6f263d" HeaderStyle-ForeColor="White" CellPadding="7"
    CellSpacing="0" DataKeyNames="orderItemId" AllowSorting="True"
    AlternatingRowStyle-BorderStyle="Solid" AlternatingRowStyle-BackColor="White"
    AlternatingRowStyle-ForeColor="#6f263d" AlternatingRowStyle-CssClass="fancylink"
    Font-Size="Small" RowStyle-BackColor="#63666A" RowStyle-BorderStyle="None"
    RowStyle-ForeColor="White" SelectedRowStyle-BackColor="#D3D0C8"
    SelectedRowStyle-ForeColor="Black" Width="100%"
    OnSelectedIndexChanged="ItemView_SelectedIndexChanged" ShowHeaderWhenEmpty="false"
    PagerStyle-CssClass="fancylink" PagerStyle-BackColor="#6f263d" PagerStyle-ForeColor="White"
    PagerSettings-FirstPageText="First Page&nbsp;&nbsp;&nbsp;" PagerSettings-Mode="NextPrevious"
    PagerSettings-NextPageText="Next Page" PagerSettings-LastPageText="End"
    PagerSettings-PreviousPageText="Previous Page" PagerStyle-HorizontalAlign="Center"
    PagerStyle-Width="20%" OnPageIndexChanging="OnPageIndexChanging"
    OnRowDataBound="ItemView_RowDataBound" OnRowEditing="ItemView_EditRow"
    OnRowUpdating="ItemView_OnRowUpdating" BorderStyle="None"
    OnRowCancelingEdit="ItemView_RowCancelingEdit" OnRowDeleting="ItemView_DeleteRow"
    OnSelectedIndexChanging="ItemView_OnSelectedIndexChanging" AutoGenerateSelectButton="False">

    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="lbl_itemId" runat="server" Text='<%#Eval("orderItemId") %>'
                    ReadOnly="True"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item">
            <ItemTemplate>
                <asp:LinkButton ID="btn_Select" runat="server" Text='<%#Eval("item") %>'
                    CommandName="Select" ItemStyle-HorizontalAlign="Center" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="PO Num">
            <ItemTemplate>
                <asp:Label ID="lbl_poNum" runat="server" Text='<%#Eval("poNum") %>'
                    NullDisplayText="--"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_poNum" runat="server" Text='<%#Eval("poNum") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item Amount">
            <ItemTemplate>
                <asp:Label ID="lbl_amount" runat="server" Text='<%#Eval("amount") %>'
                    NullDisplayText="--"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_amount" runat="server" Text='<%#Eval("amount") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:Label ID="lbl_quantity" runat="server" Text='<%#Eval("quantity") %>'
                    NullDisplayText="--"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_quantity" runat="server" Text='<%#Eval("quantity") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date Ordered">
            <ItemTemplate>
                <asp:Label ID="lbl_ordered" runat="server" Text='<%#Eval("ordered") %>'
                    NullDisplayText="--"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_ordered" runat="server" Text='<%#Eval("ordered") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date Received">
            <ItemTemplate>
                <asp:Label ID="lbl_received" runat="server" Text='<%#Eval("received") %>'
                    NullDisplayText="--"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_received" runat="server" Text='<%#Eval("received") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Tech Notes">
            <ItemTemplate>
                <asp:Label ID="lbl_techNotes" runat="server" Text='<%#Eval("techNotes") %>'
                    NullDisplayText="--"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_techNotes" runat="server" Text='<%#Eval("techNotes") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit"
                    ItemStyle-HorizontalAlign="Center" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="btn_Update" runat="server" Text="Update" CommandName="Update"
                    ItemStyle-HorizontalAlign="Center" />
                <asp:LinkButton ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"
                    ItemStyle-HorizontalAlign="Center" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The 2 fields that need to be trimmed at lbl_item and lbl_techNotes.

This is the code-behind that I'm trying to get to work:

protected void ItemView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    int i = 0;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            i++;
            string s = cell.Text;
            int maxLength = 50;

            if ((cell.Text.Length > maxLength) && ((i == 1) || (i == 8)))
                cell.Text = cell.Text.Substring(0, maxLength) + "...";
            cell.ToolTip = s;

        }
    }
}

Note: I did try looking at those specific cells but that didn't work either. I think there is something I am not doing correctly to target those cells.

Any help is much appreciated. I've exhausted everything I could find on the web. I'm really just not that savvy with using GridViews yet. Thanks!


Solution

  • Keep in mind that two VERY different means exist to reference controls in a GridView exist.

    If the controls in the GV are autogenerated, or they are datafields, then you use the cells[] collection.

    If the controls in the GV are templated columns (hence, using .net controls such as text box or labels), then you have to use .FindControl of that given row.

    Given your markup, then you are clearly using templated controls, and thus you can't use the. cells() collection, but are to use .FindControl method of that given GV row.

    Hence, the following code should work:

            const int maxLength = 50;
    
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label LblItemID = (Label)e.Row.FindControl("LblItem");
                string s = LblItemID.Text;
                LblItemID.Text = s.Substring(0, maxLength) + "...";
                LblItemID.ToolTip = s;
    
                Label lbl_techNotes = (Label)e.Row.FindControl("lbl_techNotes");
                s = lbl_techNotes.Text;
                lbl_techNotes.Text = s.Substring(0, maxLength) + "...";
                lbl_techNotes.ToolTip = s;
    
            }
    

    Also, you look to have a type-o, since this:

    the 2 fields that need to be trimmed at lbl_item and lbl_techNotes.

    I don't see a lbl_item in the GV markup, but only can find and see something called "lbl_itemId" in your markup. So, until you figure out what labiated control ID you want to change, then such code will never work.