C# 使用字典的Gridview编程工具提示

C# 使用字典的Gridview编程工具提示,c#,dictionary,key,C#,Dictionary,Key,我有下面的代码,我认为应该工作-然而,我认为什么和什么实际工作是完全不同的东西!事实证明这一点,它不起作用 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { Dictionary<String, String> headerTooltips = new Dictionary<String, String>(); headerT

我有下面的代码,我认为应该工作-然而,我认为什么和什么实际工作是完全不同的东西!事实证明这一点,它不起作用

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

                }
            }
        }
    } 
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
字典头ToolTips=新字典();
headerTooltips[“UnitId”]=“文本在此显示”;
headerTooltips[“产品说明”]=“文本在此显示”;
headerTooltips[“Productpriority”]=“文本在此显示”;
headerTooltips[“买家”]=“文本在此显示”;
headerTooltips[“子类别”]=“文本在此显示”;
headerTooltips[“材质”]=“文本在此显示”;
headerTooltips[“包装类型”]=“文本在此显示”;
headerTooltips[“重量(g)”]=“文本在此显示”;
headerTooltips[“状态”]=“文本在此显示”;
headerTooltips[“源”]=“文本在此显示”;
headerTooltips[“称重日期”]=“文本在此显示”;
headerTooltips[“产品%”]=“文本在此显示”;
headerTooltips[“回收内容%”]=“文本在此显示”;
headerTooltips[“生物可降解”]=“文本在此显示”;
headerTooltips[“可回收性注释”]=“文本在此显示”;
headerTooltips[“反馈”]=“文本在此显示”;
if(e.Row.RowType==DataControlRowType.Header)
{
foreach(e.Row.Cells中的表格单元格)
{
foreach(单元格中的System.Web.UI.Control ctl.Controls)
{
if(ctl.GetType().ToString()包含(“DataControlInkButton”))
{
字符串头文本=cell.Text;
cell.Attributes.Add(“title”,headerTooltips[headerText]);
}
}
}
}
} 
因此,本质上我希望为gridview中的每一列指定一个工具提示,即将鼠标悬停在单元上以显示“文本在此显示”

但是,当我知道试图填充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>

在没有看到design view 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>

在我看来,问题是您试图读取单元格的文本,而不是控件的文本

嘿,Josh,给我一分钟,我会贴上上面的gridview代码。看起来你的gridview标记/声明的顶部被切断了。你能加上它吗?谢谢看看更新2。我想这会解决你的第二个问题。我回家后会尝试一下,但我会提前说声谢谢!谢谢-只是一个快速的复制和粘贴显示了一个找不到的密钥,所以我将插入'isnull或empty'并查看如何进行。
    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]);
                }

            }
        }
    }