开发者

Gridview programmatic tooltips using dictionary

I have the following code that I think should work - however, what I think and what actually works is something completely different! This being demonstrated by the fact it doesn't work!

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Dictionary<String, String> headerTooltips = new Dictionary<String, String>();
        headerTooltips["UnitId"] = "text goes here";
        headerTooltips["Product Description"] = "text goes here";
        headerTooltips["Productpriority"] = "text goes here";
        headerTooltips["Buyer"] = "text goes here";
        headerTooltips["Sub-Category"] = "text goes here";
        headerTooltips["Material"] = "text goes here";
        headerTooltips["Packaging Type"] = "text goes here";
        headerTooltips["Weight (g)"] = "text goes here";
        headerTool开发者_JS百科tips["Status"] = "text goes here";
        headerTooltips["Source"] = "text goes here";
        headerTooltips["Weighed Date"] = "text goes here";
        headerTooltips["Product %"] = "text goes here";
        headerTooltips["Recycled Content %"] = "text goes here";
        headerTooltips["Biodegradable"] = "text goes here";
        headerTooltips["Recyclability Notes"] = "text goes here";
        headerTooltips["Feedback"] = "text goes here";

       if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                foreach (System.Web.UI.Control ctl in cell.Controls)
                {
                    if (ctl.GetType().ToString().Contains("DataControlLinkButton"))
                    {
                        String headerText = cell.Text;
                        cell.Attributes.Add("title", headerTooltips[headerText]);
                    }

                }
            }
        }
    } 

So essentially I would like a tooltip to be assigned to each column I have in my gridview i.e. hover over Unit to reveal "text goes here"

However, when I know attempt to populate the gridview, I receive a "KeyNotFoundException" - "The given key was not present in the dictionary" error.

Could someone point out where I am going wrong with this?

Gridview code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" 
        DataSourceID="LQProductWeightsDS" CellPadding="4" Font-Size="X-Small" 
        ForeColor="#333333" GridLines="None" 
        style="z-index: 1; left: 25px; top: 550px; position: absolute; height: 150px;  width: 1400px; text-align: center;" 
            DataKeyNames="PriKey" 
            >
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
         <asp:BoundField DataField="Productpriority" HeaderText="Productpriority" 
                    SortExpression="Productpriority" ReadOnly="True" Visible="False" />
                <asp:BoundField DataField="UnitId" HeaderText="Product ID" 
                    SortExpression="UnitId" ReadOnly="True" >
                <HeaderStyle Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="UnitDescription" HeaderText="Product Description" 
                    SortExpression="UnitDescription" ReadOnly="True">
                <HeaderStyle Width="450px" />
                </asp:BoundField>
                <asp:BoundField DataField="UnitUserfield1" HeaderText="Buyer" 
                    SortExpression="UnitUserfield1" ReadOnly="True" >
                <HeaderStyle Width="450px" />
                </asp:BoundField>
                <asp:BoundField DataField="UnitUserfield2" HeaderText="Sub-Category" 
                    SortExpression="UnitUserfield2" ReadOnly="True" >
                <HeaderStyle Width="450px" />
                </asp:BoundField>
               <asp:BoundField DataField="MaterialText" HeaderText="Material" 
                    SortExpression="MaterialText" ReadOnly="True" />
                <asp:BoundField DataField="PackagingTypeCode" HeaderText="Packaging Type" 
                    SortExpression="PackagingTypeCode" ReadOnly="True" >
                <HeaderStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="UnitWeight" HeaderText="Weight (g)" 
                    SortExpression="UnitWeight" DataFormatString="{0:F2}" ReadOnly="True" 
                    ShowHeader="False" >
                <HeaderStyle Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="WeightStatus" HeaderText="Status" 
                    SortExpression="WeightStatus" ReadOnly="True" />
                <asp:BoundField DataField="RevisionSourceCode" HeaderText="Source" 
                    SortExpression="RevisionSourceCode" ReadOnly="True" />
                <asp:BoundField DataField="RevisionDate" HeaderText="Weighed Date" 
                    SortExpression="RevisionDate" DataFormatString="{0:dd/MM/yyyy}" 
                    ReadOnly="True" >    
                <HeaderStyle Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="ProductPercentage" HeaderText="Product %" 
                    SortExpression="ProductPercentage" DataFormatString="{0:F4}" 
                    ReadOnly="True" >
                <HeaderStyle Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="RecycledContent" HeaderText="Recycled Content %" 
                    SortExpression="RecycledContent" ReadOnly="True" >
                <HeaderStyle Width="350px" />
                </asp:BoundField>
                <asp:BoundField DataField="IsBiodegradable" HeaderText="Biodegradable" 
                    SortExpression="IsBiodegradable" ReadOnly="True" />
                <asp:BoundField DataField="Recyclability" HeaderText="Recyclability Notes" 
                    SortExpression="Recyclability" ReadOnly="True" >
                <HeaderStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Feedback" HeaderText="Feedback" 
                    SortExpression="Feedback" >
                    <HeaderStyle Width="750px" />
                </asp:BoundField>

            <asp:CommandField ShowEditButton="True" />


        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>


Without seeing the design-view Gridview code, I was able to get the code to work by making the following changes:

    if (e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            String headerText = cell.Text;
            cell.Attributes.Add("title", headerTooltips[cell.Text]);
        }
    }

If you can post the design-view code, I might be able be able to figure out what's going on. Here's what I used in my set up:

       <asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound" AutoGenerateColumns="false" AllowSorting="true">
        <Columns>
            <asp:BoundField DataField="UnitId" HeaderText="UnitId" />
            <asp:BoundField DataField="ProductDesc" HeaderText="Product Description" />
        </Columns>
    </asp:GridView>

Obviously I didn't add the full dataset.

Update 2: Give this a try:

    if (e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            foreach (System.Web.UI.Control ctl in cell.Controls)
            {
                if (ctl.GetType().ToString().Contains("DataControlLinkButton"))
                {
                    String headerText = ((LinkButton)ctl).Text;
                    cell.Attributes.Add("title", headerTooltips[headerText]);
                }

            }
        }
    }

In think the problem was you were trying to read the text of the cell instead of the text of the control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜