请问我有上下对应的两个DataGrid...
一个是没有变滚动条的...
可是因为我两个DataGrid字段是对应的...
所以当我拉动有滚动条的DataGrid时...
我希望另一个也会跟着移到对应的字段...
请问要怎么做到??

解决方案 »

  1.   

    要用 js 实现,理论上可以实现!主要是计算、操作 ScrollTop
      

  2.   

    写个小例子,ie7 下测试 ok,ff3 下 fail L@_@K
    <!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>
      <title> new document </title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
      <style type="text/css">
    td {
    width: 100px;
    }
    #divTop {
    border: solid 1px red;
    height: 60px;
    overflow-y: scroll;
    }
    #divBottom {
    border: solid 1px red;
    height: 60px;
    overflow-y: hidden;
    }
      </style>
     </head> <body>
    <div id="divTop">
    <table border=1>
    <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    </tr>
    <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    </tr>
    <tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    </tr>
    <tr>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    </tr>
    </table>
    </div>
    <div id="divBottom">
    <table border=1>
    <tr>
    <td>1.1</td>
    <td>2.1</td>
    <td>3.1</td>
    </tr>
    <tr>
    <td>4.1</td>
    <td>5.1</td>
    <td>6.1</td>
    </tr>
    <tr>
    <td>7.1</td>
    <td>8.1</td>
    <td>9.1</td>
    </tr>
    <tr>
    <td>10.1</td>
    <td>11.1</td>
    <td>12.1</td>
    </tr>
    </table>
    </div>
     </body>
     <script type="text/javascript">
     <!--
    var $ = document.getElementById;
    $("divTop").onscroll = function() {
    $("divBottom").scrollTop = this.scrollTop;
    };
     //-->
     </script>
    </html>
      

  3.   

    不好意思我忘了说,我是做WinForm的,
    请问WinForm的DataGrid要怎么定同时拉动滚动条??
      

  4.   

    直接用服务器控件都可以实现。是个联动效果 ,以前一个很厉害的老师教过我 时间久了也忘的差不多了,好像是写在第一个DataGrid 的某个事件里面的 我回去找找,看例子还在不 ,在的话发给你....
      

  5.   

    Hello!! HooverHuang大你有找到吗??
    或是可以说仔细一点呀??
      

  6.   

    使用datagrid的Scroll 事件,在第一个datagrid的Scroll 事件处理代码中让第二个datagrid的 CurrentCell移动到相应位置,移动的幅度可根据第一个datagrid的FirstVisibleColumn 属性值的变化来计算。
      

  7.   

    我在第一个DataGrid的Scroll事件中写入
    this. DataGrid1.CurrentCell = new DataGridCell( this. DataGrid1.CurrentRowIndex, this. DataGrid2.FirstVisibleColumn);
    我是会指定DataGrid2点到那一格…
    可是DataGrid2的ScrollBar不会移到…
    且那一格也不是变到第一个字段…
    不知我该怎么修改…
    请大大再教教我吧!
      

  8.   

    没做过WinForm蹭分来的,见高分帖就顶个
      

  9.   

    如果你是datagrid1滚动时带动datagrid2,那么要在datagrid1的Scroll事件中写:
    if(old_FirstVisibleColumn <= 0)
    {
    old_FirstVisibleColumn=this. DataGrid1.FirstVisibleColumn;
    }
    this. DataGrid2.CurrentCell = new DataGridCell( this. DataGrid2.CurrentRowIndex, this. DataGrid2.FirstVisibleColumn + (this. DataGrid1.FirstVisibleColumn - old_FirstVisibleColumn)); 
    old_FirstVisibleColumn = this. DataGrid1.FirstVisibleColumn;如果你是datagrid2滚动时带动datagrid1,那么要在datagrid2的Scroll事件中写:
    if(old_FirstVisibleColumn <= 0)
    {
    old_FirstVisibleColumn=this. DataGrid2.FirstVisibleColumn;
    }
    this. DataGrid1.CurrentCell = new DataGridCell( this. DataGrid1.CurrentRowIndex, this. DataGrid1.FirstVisibleColumn + (this. DataGrid2.FirstVisibleColumn - old_FirstVisibleColumn)); 
    old_FirstVisibleColumn = this. DataGrid2.FirstVisibleColumn;old_FirstVisibleColumn保留上次的FirstVisibleColumn值。用当前的FirstVisibleColumn值减old_FirstVisibleColumn,可知道Column左右移动的幅度,另外两个Datagrid的列数要相等,否则还要检查不要越界。old_FirstVisibleColumn不能是局部变量,因为它要保持数据持续存在。而局部变量每次函数调用后就清除掉了。
      

  10.   

    qy300大大:
    完全没反应耶!!
    我的写法是:
    private int old_FirstVisibleColumn = 0;
    private void dataGrid1_Scroll(object sender, System.EventArgs e)
    {
    if(old_FirstVisibleColumn <= 0) 

    old_FirstVisibleColumn = this.dataGrid1.FirstVisibleColumn; 

    this.dataGrid2.CurrentCell = new DataGridCell( this.dataGrid2.CurrentRowIndex, this.dataGrid2.FirstVisibleColumn + (this.dataGrid1.FirstVisibleColumn - old_FirstVisibleColumn)); 
    old_FirstVisibleColumn = this.dataGrid1.FirstVisibleColumn; 
    }
    请问我有哪里写错了吗??
      

  11.   

    qy300大大...
    不好意思…我误会你的意思了…
    你给的程序是上下的方向去带动另一个DataGrid….
    可是我要的是左右的方向去带动另一个DataGrid…
    所以我一直试不行…
    Sorry…
    请问它可以左右的方向去带动另一个DataGrid吗??
      

  12.   

    我说的就是左右移动。下面的代码已经测试过了,但你还需要自己调整一下。       private int old_FirstVisibleColumn = 0; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); this.dataGrid1.Scroll += new EventHandler(dataGrid1_Scroll); } private void dataGrid1_Scroll(object sender, System.EventArgs e)
    {
    int diff=this.dataGrid1.FirstVisibleColumn - old_FirstVisibleColumn;
    if( diff> 0 ) //如果往右移
    {
    old_FirstVisibleColumn = this.dataGrid1.FirstVisibleColumn;
    this.dataGrid2.CurrentCell = new DataGridCell( this.dataGrid2.CurrentRowIndex, this.dataGrid2.FirstVisibleColumn + this.dataGrid2.VisibleColumnCount + diff);
    }
    else if(diff <0) //如果往左移
    {
    old_FirstVisibleColumn = this.dataGrid1.FirstVisibleColumn;
    this.dataGrid2.CurrentCell = new DataGridCell( this.dataGrid2.CurrentRowIndex, this.dataGrid2.FirstVisibleColumn + diff);
    }
      

  13.   

    我的最后写法为:private int old_FirstVisibleColumn = 0;
    private void dataGrid1_Scroll(object sender, System.EventArgs e)
    {
    int diff=this.dataGrid1.FirstVisibleColumn - old_FirstVisibleColumn; 
    old_FirstVisibleColumn = this.dataGrid1.FirstVisibleColumn; 
    if(diff > 0 ) //如果往右移 

    this.dataGrid2.CurrentCell = new DataGridCell( this.dataGrid2.CurrentRowIndex, this.dataGrid1.FirstVisibleColumn + this.dataGrid2.VisibleColumnCount - 1);
    this.dataGrid2.CurrentCell = new DataGridCell( this.dataGrid2.CurrentRowIndex, this.dataGrid2.FirstVisibleColumn);

    else if(diff < 0) //如果往左移 
    this.dataGrid2.CurrentCell = new DataGridCell( this.dataGrid2.CurrentRowIndex, this.dataGrid1.FirstVisibleColumn);
    }感谢qy300大大啰!!