遇到一个很棘手的问题,我是通过程序动态把数据库绑定DataGrid的,想要动态隐藏某列,怎么不起作用啊,还有为什么DataGrid不能拖动列的宽度啊,有时后字段的内容太多,把DataGrid拉伸的太难看了,能做到字显示一部分内容,鼠标放上区显示全部么?请各位大虾帮帮忙解决,非常感谢!

解决方案 »

  1.   

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
          If e.Item.Cells(0).Text.Length > 26 Then
            e.Item.Cells(0).Attributes.Add("Title", e.Item.Cells(0).Text)
            e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 26) + "…"
          End If
             End If
      End Sub
      

  2.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=9EBB267B-E69D-460F-B4C7-BE08CA290C0F
      

  3.   

    this.DataGrid1.Columns[6].Visible = false;
      

  4.   

    楼上的已经解决了
    完善一下,对于较长的字符串要截断时,可以动态添加一个HyperLink,设置链接,弹出新的页面进行显示,也可以设置ToolTip为截断前的字符串,这样当鼠标划过时也可以显示....
      

  5.   

    我是这么加的代码,为什么隐藏不了?
      SqlDataAdapter  sqlda=new SqlDataAdapter (strsql,sqlcnn);
    sqlcnn.Open ();
    DataSet ds=new DataSet ();  
    sqlda.Fill (ds);
    DataView dv=new DataView (ds.Tables[0]);
    DataGrid1.DataSource =dv;
    DataGrid1.DataBind ();
    DataGrid1.Columns[0].Visible=false;
    sqlcnn.Close ();
      

  6.   

    e.Item.Cells[9].ToolTip = e.Item.Cells[9].Text.Trim().ToString();
    if(e.Item.Cells[9].Text.Length>25)
    e.Item.Cells[9].Text = e.Item.Cells[9].Text.Trim().ToString().Substring(0,25);
    else
    e.Item.Cells[9].Text = e.Item.Cells[9].Text.Trim().ToString();
      

  7.   

    后一个问题解决了,谢谢,前一个问题隐藏列还是不行啊?
     SqlDataAdapter  sqlda=new SqlDataAdapter (strsql,sqlcnn);
    sqlcnn.Open ();
    DataSet ds=new DataSet ();  
    sqlda.Fill (ds);
    DataView dv=new DataView (ds.Tables[0]);
    DataGrid1.DataSource =dv;
    DataGrid1.DataBind ();
    DataGrid1.Columns[0].Visible=false;
    sqlcnn.Close ();
      

  8.   

    右键单击datagrid,选择属性生成器——列,将复选框在运行时自动创建列挑掉,然后在绑定datagrid时加入下列代码
    this.DataGrid1.DataBind();
    //将DateGrid的第三列设置为隐藏列。
    this.DataGrid1.Columns[2].Visible = false;
    获得隐藏列的值:
    private void DataGrid1_ItemCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    //获得DateGrd中隐藏列的值。
    TableCell myCell = e.Item.Cells[2];
    string row = myCell.Text.ToString();
    //判断点击的哪个按钮。
    if(e.CommandName=="Select")
    //将获得的隐藏列的值作为参数,传递到UPDATE页面。
    Response.Redirect("UPDATE.aspx?Cnt=" + row );
    }
    搞定。
      

  9.   

    右键单击datagrid,选择属性生成器——列,将复选框在运行时自动创建列挑掉,然后在绑定datagrid时加入下列代码
    this.DataGrid1.DataBind();
    //将DateGrid的第三列设置为隐藏列。
    this.DataGrid1.Columns[2].Visible = false;
    获得隐藏列的值:
    private void DataGrid1_ItemCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    //获得DateGrd中隐藏列的值。
    TableCell myCell = e.Item.Cells[2];
    string row = myCell.Text.ToString();
    //判断点击的哪个按钮。
    if(e.CommandName=="Select")
    //将获得的隐藏列的值作为参数,传递到UPDATE页面。
    Response.Redirect("UPDATE.aspx?Cnt=" + row );
    }不行啊,我的列全没了
      

  10.   

    需要在属性生成器中选泽绑列,然后点击箭头,在页眉文本中输入datagrid的表头字段。数据字段输入数据库列名。你如果想使用隐藏列,必需绑定,否则列不会隐藏