C#中如何在GridListControl中随机生成单元格数据?
How to randomly generate cell data in GridListControl in C#?
我有两个 GridListControl Window,每个有 5 行和 3 列,我在每个单元格中放置了一些硬编码值,但我想通过使用 Random
和在每个单元格中动态更新它Timer
。
我从 Here: 经历过,但没有清除我的概念。
如何将 Random
和 Timer
与我的代码集成?
请帮忙。
我的部分代码如下:
namespace First_Form_Demo
{
public partial class Form1 : Form
{
List<Tuple<int, int, double>> list_Tuple_BuySideDepth = null;
List<Tuple<double, int, int>> list_Tuple_BuySideDepth1 = null;
public Form1()
{
InitializeComponent();
Init();
}
private void Init()
{
// For GridListControl1.
list_Tuple_BuySideDepth = new List<Tuple<int, int, double>>();
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(3, 451, 67.0050));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(9, 655, 67.0025));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(17, 2045, 67.0000));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(22, 2080, 66.9875));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(23, 1564, 66.9950));
// For GridListControl2.
list_Tuple_BuySideDepth1 = new List<Tuple<double, int, int>>();
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0075, 813, 10));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0100, 1255, 28));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0125, 715, 13));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0150, 1687, 19));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0175, 1612, 24));
}
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
gridListControl1.MultiColumn = true;
gridListControl1.ForeColor = Color.Red;
gridListControl1.DataSource = list_Tuple_BuySideDepth;
this.gridListControl1.Grid.HScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;//GridScrollbarMode.Disabled;
gridListControl2.MultiColumn = true;
gridListControl2.ForeColor = Color.Red;
gridListControl2.DataSource = list_Tuple_BuySideDepth;
this.gridListControl2.Grid.HScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;
}
}
只需使用计时器并使用新的随机值更新所有项目,例如
private System.Windows.Forms.Timer updateTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
updateTimer.Interval = 1000;
updateTimer.Tick += new EventHandler(update);
updateTimer.Start();
}
private Random rnd = new Random();
private void update(Object object, EventArgs eventArgs)
{
for (int i = 0; i < list_Tuple_BuySideDepth.Count; i++)
{
list_Tuple_BuySideDepth[i] = new Tuple<int, int, double>(rnd.Next(), rnd.Next(), rnd.NextDouble());
}
for (int i = 0; i < list_Tuple_BuySideDepth1.Count; i++)
{
list_Tuple_BuySideDepth1[i] = new Tuple<double, int, int>(rnd.NextDouble(), rnd.Next(), rnd.Next());
}
}
请使用 Timer 动态更新数据源并禁用垂直滚动条,可以使用 VScrollBehavior 属性。请参考随附的示例并使用以下代码,
Timer timer;
timer = new Timer();
timer.Tick += new EventHandler(timer_Tick);
void timer_Tick(object sender, EventArgs e)
{
Random rand = new Random();
int r = rand.Next(1000,10000);
for (int i = 0; i < 10; i++)
{
list1[i] = new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data");
list2[i] = new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data");
r = rand.Next(1000, 10000);
}
GridRangeInfo range1 = this.gridListControl1.Grid.ViewLayout.VisibleCellsRange;
GridRangeInfo range2 = this.gridListControl2.Grid.ViewLayout.VisibleCellsRange;
this.gridListControl1.Grid.RefreshRange(range1);
this.gridListControl2.Grid.RefreshRange(range2);
}
public void Init()
{
list1 = new List<Data>();
list2 = new List<Data>();
Random rand = new Random();
int r = rand.Next(100);
for (int i = 0; i < 10; i++)
{
list1.Add(new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data"));
r = rand.Next(100);
list2.Add(new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data"));
}
}
private void btn_Start_Click(object sender, EventArgs e)
{
timer.Interval = 1000;
timer.Start();
}
private void btn_Stop_Click(object sender, EventArgs e)
{
timer.Stop();
}
//To disable the VerticalScrollbar
this.gridListControl1.Grid.VScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;
样本link:
[https://drive.google.com/open?id=0B9MOGv1FOt-TcUlqQjJaQXdLSnc]
-普里蒂维
我有两个 GridListControl Window,每个有 5 行和 3 列,我在每个单元格中放置了一些硬编码值,但我想通过使用 Random
和在每个单元格中动态更新它Timer
。
我从 Here: 经历过,但没有清除我的概念。
如何将 Random
和 Timer
与我的代码集成?
请帮忙。
我的部分代码如下:
namespace First_Form_Demo
{
public partial class Form1 : Form
{
List<Tuple<int, int, double>> list_Tuple_BuySideDepth = null;
List<Tuple<double, int, int>> list_Tuple_BuySideDepth1 = null;
public Form1()
{
InitializeComponent();
Init();
}
private void Init()
{
// For GridListControl1.
list_Tuple_BuySideDepth = new List<Tuple<int, int, double>>();
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(3, 451, 67.0050));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(9, 655, 67.0025));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(17, 2045, 67.0000));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(22, 2080, 66.9875));
list_Tuple_BuySideDepth.Add(new Tuple<int, int, double>(23, 1564, 66.9950));
// For GridListControl2.
list_Tuple_BuySideDepth1 = new List<Tuple<double, int, int>>();
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0075, 813, 10));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0100, 1255, 28));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0125, 715, 13));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0150, 1687, 19));
list_Tuple_BuySideDepth1.Add(new Tuple<double, int, int>(67.0175, 1612, 24));
}
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
gridListControl1.MultiColumn = true;
gridListControl1.ForeColor = Color.Red;
gridListControl1.DataSource = list_Tuple_BuySideDepth;
this.gridListControl1.Grid.HScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;//GridScrollbarMode.Disabled;
gridListControl2.MultiColumn = true;
gridListControl2.ForeColor = Color.Red;
gridListControl2.DataSource = list_Tuple_BuySideDepth;
this.gridListControl2.Grid.HScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;
}
}
只需使用计时器并使用新的随机值更新所有项目,例如
private System.Windows.Forms.Timer updateTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
updateTimer.Interval = 1000;
updateTimer.Tick += new EventHandler(update);
updateTimer.Start();
}
private Random rnd = new Random();
private void update(Object object, EventArgs eventArgs)
{
for (int i = 0; i < list_Tuple_BuySideDepth.Count; i++)
{
list_Tuple_BuySideDepth[i] = new Tuple<int, int, double>(rnd.Next(), rnd.Next(), rnd.NextDouble());
}
for (int i = 0; i < list_Tuple_BuySideDepth1.Count; i++)
{
list_Tuple_BuySideDepth1[i] = new Tuple<double, int, int>(rnd.NextDouble(), rnd.Next(), rnd.Next());
}
}
请使用 Timer 动态更新数据源并禁用垂直滚动条,可以使用 VScrollBehavior 属性。请参考随附的示例并使用以下代码,
Timer timer;
timer = new Timer();
timer.Tick += new EventHandler(timer_Tick);
void timer_Tick(object sender, EventArgs e)
{
Random rand = new Random();
int r = rand.Next(1000,10000);
for (int i = 0; i < 10; i++)
{
list1[i] = new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data");
list2[i] = new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data");
r = rand.Next(1000, 10000);
}
GridRangeInfo range1 = this.gridListControl1.Grid.ViewLayout.VisibleCellsRange;
GridRangeInfo range2 = this.gridListControl2.Grid.ViewLayout.VisibleCellsRange;
this.gridListControl1.Grid.RefreshRange(range1);
this.gridListControl2.Grid.RefreshRange(range2);
}
public void Init()
{
list1 = new List<Data>();
list2 = new List<Data>();
Random rand = new Random();
int r = rand.Next(100);
for (int i = 0; i < 10; i++)
{
list1.Add(new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data"));
r = rand.Next(100);
list2.Add(new Data(r.ToString(), "Category" + r.ToString(), "Desc" + r.ToString(), r.ToString() + "Data"));
}
}
private void btn_Start_Click(object sender, EventArgs e)
{
timer.Interval = 1000;
timer.Start();
}
private void btn_Stop_Click(object sender, EventArgs e)
{
timer.Stop();
}
//To disable the VerticalScrollbar
this.gridListControl1.Grid.VScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;
样本link: [https://drive.google.com/open?id=0B9MOGv1FOt-TcUlqQjJaQXdLSnc]
-普里蒂维