新接触vs2005,关于gridview控件有太多的不明白。
需要解决的问题如下:
一、在gridview绑定字段的时候,如果字段太长,如何截取该字段的前十五个字符显示?且在运行时当鼠标处在该字段的某行位置时,用title属性绑定并显示该行的完整内容!!
二、如何实现鼠标滑过时的行变色?
三、在数据库中有一个bool型的字段,绑定时只能true或false,如何把true或false显示为"成功"和“失败”!!麻烦大家给点提示!!

解决方案 »

  1.   

    ASP.NET 2.0中Gridview控件高级技巧
    http://dev.yesky.com/msdn/453/2078453.shtml
      

  2.   

    1.截取字符串
     protected   string   GetStr(string   s)   
      {   
            if(s.Length>15)return   s.SubString(0,15)+"...";   
            return   s;   
      } 
    2.变色
    void changeRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    //如果是数据项并且是交替项
    if(e.Item.ItemType == ListItemType.Item  || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    //添加自定义属性,当鼠标移过来时设置该行的背景色为"6699ff",并保存原背景色
    e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
    //添加自定义属性,当鼠标移走时还原该行的背景色
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor"); } 
    }
      

  3.   

    1。用substring:select SUBSTRING(字段名,1,15)from 表名
    将tooptip属性绑定到title字段
    2。没试过,应该和datagrid相同吧
    3。在dataBinding中判断
      

  4.   

    很多方法跟DATAGrid没有多大区别的。
      

  5.   

    1:
     #region 截取字符
            static public string Left(string str, int L)
            {
                string tmpStr;
                tmpStr = str;
                if (str.Length > L)
                {                tmpStr = str.Substring(0, L) + "...";
                }
                return tmpStr;
            }        #endregion
    引用:
    <%#Left(Convert.ToString(Eval("字段名")),8)%>
    这里的8你可以改成15或任意的你喜欢的长度
    3:
    在sql里写:
    (case bool字段 when 'true' then '成功' when 'false' then '失败' End) as ??
      

  6.   

    GridView的几个事件(如实现: 行的双击/单击/捕捉键盘按键/鼠标悬浮/移出效果)(示例代码下载)
    http://dev.csdn.net/author/ChengKing/1dd69a7460004eb8aa03eb0a599c74d2.html
      

  7.   

    1.通过CSS的text-overflow属性。例如某一列的单元格应用了这个属性,同时设置了CSS的width属性和overflow属性,那么这列的单元格将以此width为最大宽度,超出了的自动剪裁。详细自己搜索text-overflow吧。3.<%# ((bool)Eval("Column")) ? "成功" : "失败" %>
      

  8.   

    第二个问题已解决!!谢谢楼上的代码参考
    未解决的:
    1、
    第一个问题:根据楼上的sql函数substring()在select语句中报错
    报错提示:IErrorInfo.GetDescription 因 E_FAIL(0x80004005) 而失败。 我的语句:string sql = "select id,adminName,adminPwd,LoginTime,loginIp,substring(userInfo,1,15) as userInfo,isLogin from adminLog";如果根据asp.net中的substirng,不知道该如何绑定到gridview,还请麻烦再提示一下!!而且gridivew中也好像没有tooltip属性2、case bool字段 when 'true' then '成功' when 'false' then '失败' End) as ??
    这句语句该怎么写?该不会用存储过程吧?我只是上面一个语句?
    还请大家帮忙!!
      

  9.   

    变色:
    在你的GridView的RowDataBound事件中编写:
    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
    e.Row.Attributes.Add
    ("onmouseout", "this.style.backgroundColor=c");
      

  10.   

    如果要把光标移上去还要显示全部内容你可以加一个span
    比如你写的:select aa from ??
    如果aa内容太长就要以...代替,而光标移上去又得把aa的内容全部显示,则:<ItemTemplate>
                                            <span title='<%#Eval("aa")%>'>
                                                <%#Left(Convert.ToString(Eval("aa")),8)%>
                                            </span>
                                        </ItemTemplate>
      

  11.   

    第三个问,那你在gridview的rowdatabound里实现了
    在某个模板列里放一个隐藏控件HiddenField
    前台:
     <ItemTemplate>
        <asp:Label ID="labTip" runat="server"></asp:Label>                                                                    <asp:HiddenField ID="hiduserid" runat="server" Value='<%#Eval("bool字段")%>' />
                                    </ItemTemplate>后台:
     protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    Label labtip = (Label)e.Row.Cells[1].FindControl("labTip");
                HiddenField hid_userid = (HiddenField)e.Row.Cells[1].FindControl("hiduserid");
                if (hid_userid.Value.ToString() == "true") //            {
                                    labtip.Text = "成功";
                }
                else
    {
    labtip.Text = "失败";}
            }
        }
      

  12.   

    1.截取字符串
     protected   string   GetStr(string   s)   
      {   
            if(s.Length>15)return   s.SubString(0,15)+"...";   
            return   s;   
      } 
    2.变色
    void changeRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    //如果是数据项并且是交替项
    if(e.Item.ItemType == ListItemType.Item  || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    //添加自定义属性,当鼠标移过来时设置该行的背景色为"6699ff",并保存原背景色
    e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
    //添加自定义属性,当鼠标移走时还原该行的背景色
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor"); } 
    }