当前位置:编程学习 > C#/ASP.NET >>

如果用鼠标来调整GridView的列宽,像EXCEL那样可任意调整

在网上找了很多,基本都是照抄孟子e章的代码,鼠标拖动不灵活,且列宽不能小于列字符串长度,基本不可用。

有没有靠普点的,好用的代码方法,请赐教!! --------------------编程问答-------------------- datagridview的view自带拖拽列宽功能!
--------------------编程问答-------------------- 我用的是VS2008,没有datagridview控件,只有Gridview --------------------编程问答-------------------- FineUI --------------------编程问答-------------------- //JS代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated" AutoGenerateColumns="true">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

<script language="javascript" type="text/javascript">
        //设置Datagrid列宽可以被拖动的函数
        function SyDG_moveOnTd(td) {
            if (event.offsetX > td.offsetWidth - 10)
                td.style.cursor = 'w-resize';
            else
                td.style.cursor = 'default';
            if (td.mouseDown != null && td.mouseDown == true) {
                if (td.oldWidth + (event.x - td.oldX) > 0)
                    td.width = td.oldWidth + (event.x - td.oldX);
                td.style.width = td.width;
                td.style.cursor = 'w-resize';

                table = td;
                while (table.tagName != 'TABLE') table = table.parentElement;
                table.width = td.tableWidth + (td.offsetWidth - td.oldWidth); table.style.width = table.width;
            }
        }
        function SyDG_downOnTd(td) {
            if (event.offsetX > td.offsetWidth - 10) {
                td.mouseDown = true;
                td.oldX = event.x;
                td.oldWidth = td.offsetWidth;
                table = td; while (table.tagName != 'TABLE') table = table.parentElement;
                td.tableWidth = table.offsetWidth;
            }
        }
    </script>

CS(C#)代码
protected void Page_Load(object sender, EventArgs e)
    {      
        //将DataTable绑定到GridView
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    e.Row.Cells[i].Attributes.Add("onmousemove", "SyDG_moveOnTd(this)");
                    e.Row.Cells[i].Attributes.Add("onmousedown", "SyDG_downOnTd(this)");
                    e.Row.Cells[i].Attributes.Add("onmouseup", "this.mouseDown=false");
                    e.Row.Cells[i].Attributes.Add("onmouseout", "this.mouseDown=false");
                }
            
        }
    }

以上是网上找来的代码,但是怎么也调整不了列宽,怎么回事?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,