Recently I am working on a small WPF project. I have to write a function because I need to process some data. It is a small algorithm. I want to write it here to help me learn new things. At the same time, I hope the cool guys will give me some advice and find out some shortcomings.
Simplified requirements:
Create a datatable table DT. The number of records in the DT table is M. Create able tables dt1, dt2, dt1, and dt2 to display the data in DT cyclically. Add a timer to regularly refresh the data in dt1 and dt2, and dt1 and dt2 can only display n rows at a time.
1. Declare Variables
Private dispatchertimer timer; datatable dt = new datatable (); datatable dt1 = new datatable (); datatable dt2 = new datatable (); public static int currentrows = 0; // The current row public static int currentrows2 = 0; // intermediate variable
View code
Timer:
timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 5); timer.Tick += new EventHandler(timer_Tick); timer.Start();
View code
2. Add a control. Here, add two DataGrid controls to display data in dt1 and 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. Create a DT able table DT and initialize dt1 and 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))); }
Call at initialization:
Initialize (11 );
4. Compile Functions
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. Regularly refresh data in dt1 and dt2
void timer_Tick(object sender, EventArgs e) { dt1.Clear(); dt2.Clear(); Update(3); this.dataGrid1.ItemsSource = dt1.DefaultView; this.dataGrid2.ItemsSource = dt2.DefaultView; }
Small application Algorithms