Gridview程序化工具提示使用字典

时间:2010-01-13 14:32:17

标签: c# dictionary key

我有以下代码,我认为应该有效 - 但是,我的想法和实际工作是完全不同的!事实证明这不起作用!

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";
        headerTooltips["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]);
                    }

                }
            }
        }
    } 

基本上我希望将一个工具提示分配给我在gridview中的每一列,即将鼠标悬停在Unit上以显示“text goes here”

但是,当我知道尝试填充gridview时,我收到“KeyNotFoundException” - “字典中没有给定的密钥”错误。

有人能指出我错在哪里吗?

Gridview代码:

<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>

1 个答案:

答案 0 :(得分:0)

在没有看到设计视图Gridview代码的情况下,我能够通过进行以下更改来使代码工作:

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

如果您可以发布设计视图代码,我或许能够弄清楚发生了什么。这是我在设置中使用的内容:

       <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>

显然我没有添加完整的数据集。

更新2:尝试一下:

    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]);
                }

            }
        }
    }

认为问题是你试图读取单元格的文本而不是控件的文本。