Ok, I have spent some time today and created just a basic version of my actual program. Below is the code which allows to see initialy described problem with datagridview. It is a consoleApplication created in Visual Studio 2008. It should compile with no problem. If the effect I mention at the beginning of this whole post does not appear to happen, please modify the threshold between new threads. If you anyone would like to ask some questions, please send them here. I need to mention it once again - this issue is very important for me and time is very precious thus I will greatly appreciate any further help and obviously thanks very much for people who tried to help already.
Martin
P.S. Here is a pasted code, but as you can see, formating is not the best, therefore it is also available here: http://radlak.com/c-sharpExampleOfCrashingDataGridView.cs
using System.Text;
using System;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
RuntimeClass rt = new RuntimeClass();
new Thread(rt.StartAll).Start();
Console.ReadKey(false);
rt.OnStop();
Console.WriteLine("key pressed");
}
}
class RuntimeClass
{
public delegate void updateGridCallback(DataView tbl);
Strat.RunForm runForm;
Thread oThread;
bool keepGoing = true;
ValuesTable[] valuesTable = new ValuesTable[20];
double Column2 = 100.0;
double Column3 = 101.0;
Random r = new Random();
DataTable tbl = new DataTable();
DataView vtbl;
Ladder[] ladder = new Ladder[20];
DataRow t;
public RuntimeClass()
{
}
public void StartAll()
{
OnInitialization();
}
public void OnInitialization()
{
Console.WriteLine("On Initialization");
for (int i = 0; i < 20; i++)
{
valuesTable[i] = new ValuesTable(Column2 - i, r.Next(100, 1000), r.Next(100, 1000), Column3 + i);
Console.WriteLine(i + ": " + valuesTable[i].Column2Size + " - " + valuesTable[i].Column2 + " : " + valuesTable[i].Column3 + " - " + valuesTable[i].Column3Size);
}
runForm = new Strat.RunForm();
oThread = new Thread(new ThreadStart(runForm.startForm));
oThread.Start();
tbl.Columns.Add("Column 1", typeof(double));
tbl.Columns.Add("Column 2", typeof(int));
tbl.Columns.Add("ValueIndex", typeof(System.Decimal));
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = tbl.Columns["ValueIndex"];
tbl.Columns["ValueIndex"].AllowDBNull = true;
tbl.PrimaryKey = PrimaryKeyColumns;
tbl.Columns.Add("Column 4", typeof(int));
tbl.Columns.Add("Column 5", typeof(double));
for (int i = 0; i < 10; i++)
{
ladder[i] = new Ladder(valuesTable[i].Column2Size, valuesTable[i].Column2, valuesTable[i].Column3Size, valuesTable[i].Column3);
}
for (int i = 0; i < 41; i++)
{
Column2 = Column3;
DataRow tRow = tbl.NewRow();
tRow["ValueIndex"] = (double)(valuesTable[0].Column2 - 20 + i);
tbl.Rows.Add(tRow);
}
vtbl = new DataView(tbl);
vtbl.Sort = "ValueIndex DESC";
Thread.Sleep(2000);
runForm.sampleForm.Invoke(runForm.sampleForm.dDelegateInitializeGrid,
vtbl);
while (keepGoing == true)
{
Thread.Sleep(r.Next(0, 35));
new Thread(OnDataArrival).Start();
}
}
public void OnStop()
{
keepGoing = false;
Console.WriteLine("On Stop");
}
public void OnDataArrival()
{
lock (valuesTable)
{
for (int i = 0; i < 20; i++)
{
valuesTable[i].UpdateValues(Column2 - i, r.Next(100, 1000), r.Next(100, 1000), Column3 + i);
Console.WriteLine(i + ": " + valuesTable[i].Column2Size + " - " + valuesTable[i].Column2 + " : " + valuesTable[i].Column3 + " - " + valuesTable[i].Column3Size);
}
for (int i = 0; i < 20; i++)
{
t = tbl.Rows.Find(valuesTable[i].Column2);
if (t != null)
{
lock (t)
{
t["Column 2"] = valuesTable[i].Column2;
t["Column 1"] = valuesTable[i].Column2Size;
t["Column 4"] = DBNull.Value;
t["Column 5"] = DBNull.Value;
}
}
else
{
tbl.Rows.Add(valuesTable[i].Column2Size, valuesTable[i].Column2,
DBNull.Value, DBNull.Value);
}
}
for (int i = 0; i < 20; i++)
{
t = tbl.Rows.Find(valuesTable[i].Column3);
if (t != null)
{
lock (t)
{
t["Column 1"] = DBNull.Value;
t["Column 2"] = DBNull.Value;
t["Column 5"] = valuesTable[i].Column3Size;
t["Column 4"] = valuesTable[i].Column3;
}
}
else
{
tbl.Rows.Add(DBNull.Value, DBNull.Value,
valuesTable[i].Column3, valuesTable[i].Column3Size);
}
}
}
// runForm.sampleForm.Invoke(runForm.sampleForm.dDelegateUpdateTradeValueIndex);
Console.WriteLine("OnDataArrival");
Thread.CurrentThread.Abort();
Thread.CurrentThread.Abort();
}
}
public class ValuesTable
{
public double Column2 { get; set; }
public int Column2Size { get; set; }
public double Column3 { get; set; }
public int Column3Size { get; set; }
public ValuesTable(double Column2, int Column2Size, int Column3Size, double Column3)
{
this.Column2 = Column2;
this.Column2Size = Column2Size;
this.Column3 = Column3;
this.Column3Size = Column3Size;
}
public void UpdateValues(double Column2, int Column2Size, int Column3Size, double Column3)
{
this.Column2 = Column2;
this.Column2Size = Column2Size;
this.Column3 = Column3;
this.Column3Size = Column3Size;
}
}
public class Ladder
{
public int bidVol { get; set; }
public double bidValueIndex { get; set; }
public int askVol { get; set; }
public double askValueIndex { get; set; }
public Ladder(int Column2Vol, double Column2ValueIndex, int Column3Vol, double Column3ValueIndex)
{
bidVol = Column2Vol;
bidValueIndex = Column2ValueIndex;
askVol = Column3Vol;
askValueIndex = Column3ValueIndex;
}
public void ladderupdate(int Column2Vol, double Column2ValueIndex, int Column3Vol, double Column3ValueIndex)
{
bidVol = Column2Vol;
bidValueIndex = Column2ValueIndex;
askVol = Column3Vol;
askValueIndex = Column3ValueIndex;
}
public int[] addto(int Column2Vol, double Column2ValueIndex, int Column3Vol, double Column3ValueIndex)
{
int[] ret = new int[2] { 0, 0 };
//index 0: bid
//index 1: ask
if (Column2ValueIndex != 0 && Column2Vol != 0)
{
if (bidValueIndex == Column2ValueIndex)
{
if (bidVol != Column2Vol)
{
bidVol = Column2Vol;
ret[0] = -1;
}
}
else if (bidValueIndex != Column2ValueIndex)
{
bidValueIndex = Column2ValueIndex;
bidVol = Column2Vol;
ret[0] = -2;
}
}
if (Column3ValueIndex != 0 && Column3Vol != 0)
{
if (askValueIndex == Column3ValueIndex)
{
if (askVol != Column3Vol)
{
askVol = Column3Vol;
ret[1] = 1;
}
}
else if (askValueIndex != Column3ValueIndex)
{
askValueIndex = Column3ValueIndex;
askVol = Column3Vol;
ret[1] = 2;
}
}
return ret;
}
}
}
namespace Strat
{
public delegate void DelegateInitializeGrid(DataView tbl);
public delegate void DelegateUpdateGrid();
public class RunForm
{
public Form1 sampleForm = null;
public DataTable dtbl;
public void startForm()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
sampleForm = new Form1();
Application.Run(sampleForm);
}
public void stopForm()
{
sampleForm.Dispose();
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should bedisposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
//aTimer.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 =
new System.Windows.Forms.DataGridViewCellStyle();
//System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToResizeColumns = false;
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.AutoSizeColumnsMode =
System.Windows.Forms.DataGridViewAutoSizeColumnsMode.None;
this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.dataGridView1.ColumnHeadersHeight = 40;
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridViewCellStyle1.Alignment =
System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle1.Font = new System.Drawing.Font("MicrosoftSans Serif", 11F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(238)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle1.SelectionBackColor =
System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor =
System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode =
System.Windows.Forms.DataGridViewTriState.False;
this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.EnableHeadersVisualStyles = false;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersWidthSizeMode =
System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.dataGridView1.Size = new System.Drawing.Size(596, 413);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(585, 800);
this.Controls.Add(this.dataGridView1);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
Console.WriteLine("Form initialized");
}
#endregion
private void initializeGrid(DataView lst)
{
SetDoubleBuffered(dataGridView1);
this.dataGridView1.ReadOnly = true;
this.dataGridView1.DataSource = lst;
dataGridView1.RowHeadersVisible = false;
dataGridView1.Columns["ValueIndex"].DefaultCellStyle.BackColor = System.Drawing.Color.DarkGray;
dataGridView1.Columns["ValueIndex"].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns["ValueIndex"].DefaultCellStyle.ForeColor =
System.Drawing.Color.White;
dataGridView1.Columns["Column 2"].DefaultCellStyle.BackColor =
System.Drawing.Color.Blue;
dataGridView1.Columns["Column 2"].DefaultCellStyle.ForeColor =
System.Drawing.Color.White;
dataGridView1.Columns["Column 2"].DefaultCellStyle.Alignment =
System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
dataGridView1.Columns["Column 4"].DefaultCellStyle.BackColor =
System.Drawing.Color.Red;
dataGridView1.Columns["Column 4"].DefaultCellStyle.ForeColor =
System.Drawing.Color.White;
dataGridView1.Columns["Column 4"].DefaultCellStyle.Alignment =
System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
this.dataGridView1.MultiSelect = false;
}
private void updateGrid()
{
this.dataGridView1.Refresh();
}
public System.Windows.Forms.DataGridView dataGridView1;
}
public partial class Form1 : Form
{
public bool canGo = false;
public DelegateInitializeGrid dDelegateInitializeGrid;
public DelegateUpdateGrid dDelegateUpdateGrid;
public Form1()
{
InitializeComponent();
dataGridView1.AutoGenerateColumns = true;
dDelegateInitializeGrid = new DelegateInitializeGrid(this.initializeGrid);
dDelegateUpdateGrid = new DelegateUpdateGrid(this.updateGrid);
}
public static void SetDoubleBuffered(Control control)
{
// set instance non-public property with name "DoubleBuffered" to true
typeof(Control).InvokeMember("DoubleBuffered",
BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, control, new object[] { true });
}
}
}