| 发表于:2007-01-08 01:11:22 楼主 |
用多线程执行datagrid,动态的加入一行,为什么记录超出datagrid的在窗口的高度,就会有两个滚动条呢?我代码哪有错呢? using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.threading; namespace windowsapplication4 { /// <summary> /// form1 的摘要说明。 /// </summary> public class form1 : system.windows.forms.form { private system.windows.forms.button button1; private system.windows.forms.datagrid datagrid1; private system.componentmodel.icontainer components; private arraylist aa=new arraylist(); private datatable dt = new datatable(); private datarow dr; public form1() { // // windows 窗体设计器支持所必需的 // initializecomponent(); // // todo: 在 initializecomponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void initializecomponent() { this.button1 = new system.windows.forms.button(); this.datagrid1 = new system.windows.forms.datagrid(); ((system.componentmodel.isupportinitialize)(this.datagrid1)).begininit(); this.suspendlayout(); // // button1 // this.button1.location = new system.drawing.point(376, 16); this.button1.name = "button1 "; this.button1.tabindex = 0; this.button1.text = "button1 "; this.button1.click += new system.eventhandler(this.button1_click); // // datagrid1 // this.datagrid1.datamember = " "; this.datagrid1.headerforecolor = system.drawing.systemcolors.controltext; this.datagrid1.location = new system.drawing.point(16, 8); this.datagrid1.name = "datagrid1 "; this.datagrid1.size = new system.drawing.size(280, 288); this.datagrid1.tabindex = 1; // // form1 // this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(488, 325); this.controls.add(this.datagrid1); this.controls.add(this.button1); this.name = "form1 "; this.text = "form1 "; this.load += new system.eventhandler(this.form1_load); ((system.componentmodel.isupportinitialize)(this.datagrid1)).endinit(); this.resumelayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [stathread] static void main() { application.run(new form1()); } private void form1_load(object sender, system.eventargs e) { dt.columns.add(new datacolumn( "编号 ", typeof(int32))); dr = dt.newrow(); dr[0] = 1; dt.rows.add(dr); dataview dv = new dataview(dt); datagrid1.datasource=dt; } private void button1_click(object sender, system.eventargs e) { thread t=new thread(new threadstart(showrandom)); t.isbackground=true; t.priority=threadpriority.belownormal; t.start(); } public void showrandom() { int temp_a; for(int temp_s=2;(temp_s <50);temp_s++) { random random = new system.random(); temp_a=random.next(0,100); thread.sleep(10); dr = dt.newrow(); dr[0] = temp_s.tostring(); dt.rows.add(dr); } } } } |
|
|
|
|