当鼠标停留在DataGrid某一行时,能显示其相应的某些注释信息,怎么搞呢? RT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 摘自:http://blog.csdn.net/windowsboy/archive/2004/10/18/141650.aspx 在使用DataGrid时经常遇到单元格中的内容过长而导致文本的换行,这样使本来简洁的页面看上去非常乱。下面的方法可以解决这个问题。当单元格的内容超出指定的长度后,截去多余的字,然后在鼠标停留在某个单元格上时,就显示全部的内容。此方法有个缺点:每个单位格都是指定长度的。 //某个datagrid的ItemDataBound事件。 //上半部分设置鼠标悬停时的背景色 //下半部分才起作用 public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem) { Color color = this.DataGrid1.SelectedItemStyle.ForeColor; string foreColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B); color = this.DataGrid1.SelectedItemStyle.BackColor; string backColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B); // 如果没有设置选中项的颜色,则不设置鼠标效果 if(foreColor != "#000000" || backColor != "#000000") { e.Item.Attributes.Add("onmouseover", string.Format("oldItemForeColor=this.style.color;this.style.color='{0}';oldItemBackColor=this.style.backgroundColor;this.style.backgroundColor='{1}'", foreColor, backColor)); e.Item.Attributes.Add("onmouseout", "this.style.color=oldItemForeColor;this.style.backgroundColor=oldItemBackColor;"); } e.Item.Cells[1].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[1].Text+"'><nobr>" + e.Item.Cells[1].Text + "</nobr></div>"; e.Item.Cells[2].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[2].Text+"'><nobr><a class=lan href='"+this.Request.ApplicationPath+@"/UpLoadFile/"+System.IO.Path.GetFileName(e.Item.Cells[7].Text)+"'>" + e.Item.Cells[2].Text + "</a></nobr></div>"; e.Item.Cells[3].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[3].Text+"'><nobr>" + e.Item.Cells[3].Text + "</nobr></div>"; e.Item.Cells[4].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[4].Text+"'><nobr>" + e.Item.Cells[4].Text + "</nobr></div>"; e.Item.Cells[5].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:90px;' title='"+e.Item.Cells[5].Text+"'><nobr>" + e.Item.Cells[5].Text + "</nobr></div>"; e.Item.Cells[6].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:90px;' title='"+e.Item.Cells[6].Text+"'><nobr>" + e.Item.Cells[6].Text + "</nobr></div>"; } } 用模板列,然后用Label显示数据,用一个ToolTip也可以啊,不过效果没楼上 的那么好咯 方法很多,我用超连接title,要是每一列都要这样,用ToolTip,一段html,供参考<asp:datagrid id="DataGrid3" runat="server" GridLines="Horizontal" CellPadding="3" BorderStyle="None" BorderColor="#E7E7FF" AutoGenerateColumns="False" ShowHeader="False" Width="376px" BorderWidth="0px" BackColor="White"> <SelectedItemStyle Font-Bold="True"></SelectedItemStyle> <AlternatingItemStyle BackColor="White"></AlternatingItemStyle> <ItemStyle BackColor="#E9FEEC"></ItemStyle> <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle> <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle> <Columns> <asp:TemplateColumn> <ItemStyle HorizontalAlign="Left"></ItemStyle> <ItemTemplate> <asp:Image id="Image15" runat="server" ImageUrl="./images/d.gif" /> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn> <ItemTemplate> <a href='love/person/pmdetail.aspx?id=2&personid=<%# DataBinder.Eval(Container,"DataItem.personid") %>' target =_blank title='<%# "婚姻状况:"+ DataBinder.Eval(Container,"DataItem.marriage")+", 交友意向:" +DataBinder.Eval(Container,"DataItem.friendmeant")+", 意向性别:"+DataBinder.Eval(Container,"DataItem.hopesex")%>'> <%# DataBinder.Eval(Container,"DataItem.name") %> </a> </ItemTemplate> </asp:TemplateColumn> </Columns> <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle> </asp:datagrid> http://blog.csdn.net/shoutor/archive/2004/08/06/66742.aspx url 怎么加密? 目前使用FCKeditor控件,但是这个控件中好像不支持在页面中加入视频的操作?? 定义弹出框标题 最近要做一个CRM的系统,谁有相关的资料给上一些参考啊. 有谁知道身份证的真伪运算? 谁能推荐本ASP。NET入门的好书?书名还有出版社请告知,谢谢。 为什么显示的记录会少一条?? asp.net masterpage 程序动态生成linkbutton后的点击问题 关于焦点的获取与定位 我感觉用Asp.net开发的效率比较低!!! 有没有办法加速datagird的显示? 关于博客(blog)的问题,请高手解答
在使用DataGrid时经常遇到单元格中的内容过长而导致文本的换行,这样使本来简洁的页面看上去非常乱。下面的方法可以解决这个问题。
当单元格的内容超出指定的长度后,截去多余的字,然后在鼠标停留在某个单元格上时,就显示全部的内容。
此方法有个缺点:每个单位格都是指定长度的。
//某个datagrid的ItemDataBound事件。
//上半部分设置鼠标悬停时的背景色
//下半部分才起作用
public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
Color color = this.DataGrid1.SelectedItemStyle.ForeColor;
string foreColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
color = this.DataGrid1.SelectedItemStyle.BackColor;
string backColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
// 如果没有设置选中项的颜色,则不设置鼠标效果
if(foreColor != "#000000" || backColor != "#000000")
{
e.Item.Attributes.Add("onmouseover", string.Format("oldItemForeColor=this.style.color;this.style.color='{0}';oldItemBackColor=this.style.backgroundColor;this.style.backgroundColor='{1}'", foreColor, backColor));
e.Item.Attributes.Add("onmouseout", "this.style.color=oldItemForeColor;this.style.backgroundColor=oldItemBackColor;");
}
e.Item.Cells[1].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[1].Text+"'><nobr>" + e.Item.Cells[1].Text + "</nobr></div>";
e.Item.Cells[2].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[2].Text+"'><nobr><a class=lan href='"+this.Request.ApplicationPath+@"/UpLoadFile/"+System.IO.Path.GetFileName(e.Item.Cells[7].Text)+"'>" + e.Item.Cells[2].Text + "</a></nobr></div>";
e.Item.Cells[3].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[3].Text+"'><nobr>" + e.Item.Cells[3].Text + "</nobr></div>";
e.Item.Cells[4].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[4].Text+"'><nobr>" + e.Item.Cells[4].Text + "</nobr></div>";
e.Item.Cells[5].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:90px;' title='"+e.Item.Cells[5].Text+"'><nobr>" + e.Item.Cells[5].Text + "</nobr></div>";
e.Item.Cells[6].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:90px;' title='"+e.Item.Cells[6].Text+"'><nobr>" + e.Item.Cells[6].Text + "</nobr></div>";
}
}
一段html,供参考
<asp:datagrid id="DataGrid3" runat="server" GridLines="Horizontal" CellPadding="3" BorderStyle="None"
BorderColor="#E7E7FF" AutoGenerateColumns="False" ShowHeader="False" Width="376px" BorderWidth="0px" BackColor="White">
<SelectedItemStyle Font-Bold="True"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#E9FEEC"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:Image id="Image15" runat="server" ImageUrl="./images/d.gif" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<a href='love/person/pmdetail.aspx?id=2&personid=<%# DataBinder.Eval(Container,"DataItem.personid") %>' target =_blank
title='<%# "婚姻状况:"+ DataBinder.Eval(Container,"DataItem.marriage")+", 交友意向:" +DataBinder.Eval(Container,"DataItem.friendmeant")+", 意向性别:"+DataBinder.Eval(Container,"DataItem.hopesex")%>'>
<%# DataBinder.Eval(Container,"DataItem.name") %>
</a>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:datagrid>