DataGrid带动另一个DataGrid 请问我有上下对应的两个DataGrid...一个是没有变滚动条的...可是因为我两个DataGrid字段是对应的...所以当我拉动有滚动条的DataGrid时...我希望另一个也会跟着移到对应的字段...请问要怎么做到?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要用 js 实现,理论上可以实现!主要是计算、操作 ScrollTop 写个小例子,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> 不好意思我忘了说,我是做WinForm的,请问WinForm的DataGrid要怎么定同时拉动滚动条?? 直接用服务器控件都可以实现。是个联动效果 ,以前一个很厉害的老师教过我 时间久了也忘的差不多了,好像是写在第一个DataGrid 的某个事件里面的 我回去找找,看例子还在不 ,在的话发给你.... Hello!! HooverHuang大你有找到吗??或是可以说仔细一点呀?? 使用datagrid的Scroll 事件,在第一个datagrid的Scroll 事件处理代码中让第二个datagrid的 CurrentCell移动到相应位置,移动的幅度可根据第一个datagrid的FirstVisibleColumn 属性值的变化来计算。 我在第一个DataGrid的Scroll事件中写入this. DataGrid1.CurrentCell = new DataGridCell( this. DataGrid1.CurrentRowIndex, this. DataGrid2.FirstVisibleColumn);我是会指定DataGrid2点到那一格…可是DataGrid2的ScrollBar不会移到…且那一格也不是变到第一个字段…不知我该怎么修改…请大大再教教我吧! 没做过WinForm蹭分来的,见高分帖就顶个 如果你是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不能是局部变量,因为它要保持数据持续存在。而局部变量每次函数调用后就清除掉了。 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; }请问我有哪里写错了吗?? qy300大大...不好意思…我误会你的意思了…你给的程序是上下的方向去带动另一个DataGrid….可是我要的是左右的方向去带动另一个DataGrid…所以我一直试不行…Sorry…请问它可以左右的方向去带动另一个DataGrid吗?? 我说的就是左右移动。下面的代码已经测试过了,但你还需要自己调整一下。 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); } } 我的最后写法为: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大大啰!! 如何一次性设置程序的所有的窗体上面的字体呢? wm6.5 如何用代码实现获得本机的短信中心号,急急急急急急!!! 求酒店管理系统 Panel面板添加UserControl问题 二啊 在线求助 c#中如何创建XMLHTTP对像 高手们看过来~~~ 100分求C#版聊天室源码(无刷新) B-S系统中 用C#写的代码在浏览器上看不到,在服务器上会看到源代码吗? 请教,老师留的任务 用winform做个小程序,现在想打开一段aaa.dat视频应该怎样做啊? 大家好,刚才我的回复都是红色24px的字体,大家觉得好看吗?
<!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>
请问WinForm的DataGrid要怎么定同时拉动滚动条??
或是可以说仔细一点呀??
this. DataGrid1.CurrentCell = new DataGridCell( this. DataGrid1.CurrentRowIndex, this. DataGrid2.FirstVisibleColumn);
我是会指定DataGrid2点到那一格…
可是DataGrid2的ScrollBar不会移到…
且那一格也不是变到第一个字段…
不知我该怎么修改…
请大大再教教我吧!
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不能是局部变量,因为它要保持数据持续存在。而局部变量每次函数调用后就清除掉了。
完全没反应耶!!
我的写法是:
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;
}
请问我有哪里写错了吗??
不好意思…我误会你的意思了…
你给的程序是上下的方向去带动另一个DataGrid….
可是我要的是左右的方向去带动另一个DataGrid…
所以我一直试不行…
Sorry…
请问它可以左右的方向去带动另一个DataGrid吗??
{
//
// 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);
}
}
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大大啰!!