国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

應用小算法

2019-11-14 16:27:04
字體:
來源:轉載
供稿:網友

最近在做一個WPF小項目,因為需要處理一些數據,迫不得已寫了一個函數,算是一個小算法吧,在此寫出以便溫故知新,同時希望牛人不吝賜教,點出其中不足之處。

 

簡化需求:

創建一個datatable表dt,dt表中的記錄行數是變量m。創建datatable表dt1、dt2,dt1和dt2循環顯示dt中的數據,添加一個計時器定時刷新dt1、dt2中的數據,且dt1和dt2每次只能顯示n行。

 

1、聲明變量

PRivate DispatcherTimer timer;        DataTable dt = new DataTable();        DataTable dt1 = new DataTable();        DataTable dt2 = new DataTable();        public static int currentRows = 0;//當前行        public static int currentRows2 = 0;//中間變量
View Code

定時器:

timer = new DispatcherTimer();            timer.Interval = new TimeSpan(0, 0, 5);            timer.Tick += new EventHandler(timer_Tick);            timer.Start();
View Code

 

2、添加控件,此處添加兩個datagrid控件(用以顯示dt1和dt2中的數據)

<DataGrid AutoGenerateColumns="False" Height="335" HorizontalAlignment="Left" Margin="220,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" >            <DataGrid.Columns >                <DataGridTextColumn Header="id" Binding="{Binding Path=id}"></DataGridTextColumn>                <DataGridTextColumn Header="name" Binding="{Binding Path=name}"></DataGridTextColumn>            </DataGrid.Columns>        </DataGrid>        <DataGrid AutoGenerateColumns="False" Height="305" HorizontalAlignment="Left" Margin="463,31,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="200" >            <DataGrid.Columns >                <DataGridTextColumn Header="id" Binding="{Binding Path=id}"></DataGridTextColumn>                <DataGridTextColumn Header="name" Binding="{Binding Path=name}"></DataGridTextColumn>            </DataGrid.Columns>        </DataGrid>
View Code

 

3、創建datatable表dt,并且初始化dt1和dt2

private void Initialize(int num)        {            dt.Columns.Add(new DataColumn("id", typeof(int)));            dt.Columns.Add(new DataColumn("name", typeof(string)));            for (int i = 0; i < num; i++)            {                DataRow dr = dt.NewRow();                dr["id"] = i;                dr["name"] = "tom" + i.ToString();                dt.Rows.Add(dr);            }            dt1.Columns.Add(new DataColumn("id", typeof(int)));            dt1.Columns.Add(new DataColumn("name", typeof(string)));            dt2.Columns.Add(new DataColumn("id", typeof(int)));            dt2.Columns.Add(new DataColumn("name", typeof(string)));        }

初始化時調用:

Initialize(11);

 

4、編寫函數

public void Update(int num)        {            int rows = dt.Rows.Count;            int num1 = 0;            int num2 = 0;            currentRows = currentRows2;            for (int i = currentRows; i < (currentRows + num); i++)            {                num1++;                currentRows2++;                DataRow dr = dt1.NewRow();                dr["id"] = Convert.ToInt32(dt.Rows[i][0].ToString());                dr["name"] = dt.Rows[i][1].ToString();                dt1.Rows.Add(dr);                if (i == rows - 1)                {                    i = -1;                    currentRows2 = 0;                }                if (num1 == num)                {                    break;                }            }            currentRows = currentRows2;            for (int i = currentRows; i < (currentRows + num); i++)            {                num2++;                currentRows2++;                DataRow dr = dt2.NewRow();                dr["id"] = Convert.ToInt32(dt.Rows[i][0].ToString());                dr["name"] = dt.Rows[i][1].ToString();                dt2.Rows.Add(dr);                if (i == rows - 1)                {                    i = -1;                    currentRows2 = 0;                }                if (num2 == num)                {                    break;                }            }        }

 

5、定時刷新dt1和dt2中的數據

void timer_Tick(object sender, EventArgs e)        {            dt1.Clear();            dt2.Clear();            Update(3);                    }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 界首市| 临高县| 东山县| 白山市| 崇州市| 青河县| 灯塔市| 女性| 沙坪坝区| 启东市| 满城县| 肇州县| 黄平县| 宜丰县| 防城港市| 沁源县| 阿尔山市| 武冈市| 孟津县| 沭阳县| 凤山市| 大新县| 龙海市| 屏边| 美姑县| 扎赉特旗| 合山市| 新乐市| 获嘉县| 延长县| 平遥县| 九江市| 丘北县| 囊谦县| 环江| 民勤县| 张家界市| 酒泉市| 酒泉市| 于田县| 吉木萨尔县|