首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用字典的Gridview编程工具提示

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

Stack Overflow用户
提问于 2010-01-13 22:32:17
回答 1查看 1.3K关注 0票数 0

我有以下代码,我认为应该可以工作--然而,我的想法和实际工作是完全不同的!它不工作的事实证明了这一点!

代码语言:javascript
复制
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]);
                    }

                }
            }
        }
    } 

所以从本质上说,我希望为我的网格视图中的每一列都分配一个工具提示,例如,将鼠标悬停在Unit上以显示"text So“

然而,当我知道试图填充网格视图时,我收到一个"KeyNotFoundException“--”给定键不存在于字典中“错误。

有人能指出我在这方面出了什么问题吗?

Gridview代码:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-01-13 23:01:30

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

代码语言:javascript
复制
    if (e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            String headerText = cell.Text;
            cell.Attributes.Add("title", headerTooltips[cell.Text]);
        }
    }

如果你能发布设计视图代码,我也许能搞清楚到底是怎么回事。这是我在设置中使用的:

代码语言:javascript
复制
       <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:试一试:

代码语言:javascript
复制
    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]);
                }

            }
        }
    }

在think中,问题是您试图读取单元格的文本,而不是控件的文本。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2057314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档