I have a Winform with a datagrid that has around 2 dozen calculation on button clicks. The grid has around 8000 or more rows and error out, I can run 500 rows in 30 sec. How can I increase the performance? I have set Double Buffer = True
How can I avoid performance leaks?
Error Message;
Additional information: The CLR has been unable to transition from COM context 0x5251248 to COM context 0x5251300 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
public partial class Form1 : Form
{
#region Form
public Form1()
{
InitializeComponent();
//Alternate grid color
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
Color.Beige;
//Sold grid color
//this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightYellow;
}
public static string SelectedTable = string.Empty;
#endregion
#region Import Button Click
private void button1_Click_1(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select file";
fdlg.InitialDirectory = @"c:\";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
Application.DoEvents();
}
}
#endregion
#region Data Table
public static DataTable GetDataTableExcel(string strFileName, string Table)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
conn.Open();
string strQuery = "SELECT * FROM [" + Table + "]";
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}
public static string[] GetTableExcel(string strFileName)
{
string[] strTables = new string[100];
Catalog oCatlog = new Catalog();
ADOX.Table oTable = new ADOX.Table();
ADODB.Connection oConn = new ADODB.Connection();
oConn.Open(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
oCatlog.ActiveConnection = oConn;
if (oCatlog.Tables.Count > 0)
{
int item = 0;
foreach (ADOX.Table tab in oCatlog.Tables)
{
if (tab.Type == "TABLE")
{
strTables[item] = tab.Name;
item++;
}
}
}
return strTables;
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow rows in dataGridView1.Rows)
{
//Part Gross ULD =(H2*I2)/L2
int H = Convert.ToInt32(rows.Cells[7].Value ?? 0);
decimal I = Convert.ToDecimal(rows.Cells[8].Value ?? 0);
int L = Convert.ToInt32(rows.Cells[11].Value ?? 0);
var J = H == 0 ? 0 : H * I / L;
rows.Cells[9].Value = J;
//ULD Stack =rounddown(106/S2,0)
decimal S = Convert.ToDecimal(rows.Cells[18].Value ?? 0);
int A = 106;
decimal T = A / S;
rows.Cells[19].Value = T;
//Stacks Wide =rounddown(96/R2,0)
decimal R = Convert.ToDecimal(rows.Cells[17].Value);
int B = 96;
decimal U = B / R;
rows.Cells[20].Value = U;
//Stacks Day =roundup(J2/T2,0)
double J2 = Convert.ToDouble(rows.Cells[9].Value ?? 0);
double t = Convert.ToDouble(rows.Cells[19].Value ?? 0);
int V = (int)Math.Ceiling(Convert.ToDouble(J2) / Convert.ToDouble(t));
rows.Cells[21].Value = V;
//Linear FT Stack =Q2/12
decimal Q = Convert.ToDecimal(rows.Cells[16].Value ?? 0);
int C = 12;
decimal W = (int)Math.Ceiling(Convert.ToDouble(Q) / Convert.ToDouble(C));
rows.Cells[22].Value = W;
//Raw Linear Ft Day =(V2*W2)/U2
int V21 = Convert.ToInt32(rows.Cells[21].Value ?? 0);
decimal W22 = Convert.ToDecimal(rows.Cells[22].Value ?? 0);
decimal U20 = Convert.ToDecimal(rows.Cells[20].Value ?? 0);
var X = V21 == 0 ? 0 : V21 * W22 / U20;
rows.Cells[23].Value = X;
//}
//catch
//{
// Totes =L2/K2
var L2 = Convert.ToInt32(rows.Cells[11].Value ?? 0);
var K = Convert.ToInt32(rows.Cells[10].Value ?? 0);
var AK = L / K;
rows.Cells[36].Value = AK;
//# 1 roundown tote lgth =ROUNDOWN(48 /M2,0)
decimal M2 = Convert.ToDecimal(rows.Cells[12].Value ?? 0);
var D = 48;
decimal AN = D / M2;
rows.Cells[39].Value = AN;
//# 1 roundown tote wdth =ROUNDOWN(45/N2,0)
decimal N2 = Convert.ToDecimal(rows.Cells[13].Value ?? 0);
var F = 48;
decimal AO = F / N2;
rows.Cells[40].Value = AO;
//= AN2 * AO2
decimal AN2 = Convert.ToDecimal(rows.Cells[39].Value ?? 0);
decimal AO2 = Convert.ToDecimal(rows.Cells[40].Value ?? 0);
var AP = AN2 * AO2;
rows.Cells[41].Value = AP;
//# 1 Totes Test =AK2/AP2
var AK4 = Convert.ToInt32(rows.Cells[36].Value ?? 0);
decimal AP2 = Convert.ToDecimal(rows.Cells[41].Value ?? 0);
var AQ = AK4 / AP2;
rows.Cells[42].Value = AQ;
//# 2 roundown tote wdth =ROUNDOWN(48 /N2,0)
Decimal N3 = Convert.ToInt32(rows.Cells[13].Value ?? 0);
var G = 48;
Decimal AS = G / N2;
rows.Cells[44].Value = AS;
//# 2 roundown tote lgth =ROUNDOWN(45/M2,0)
Decimal N4 = Convert.ToInt32(rows.Cells[13].Value ?? 0);
var D2 = 48;
Decimal AT = D2 / N2;
rows.Cells[45].Value = AT;
//=AS2*AT2
decimal AS2 = Convert.ToDecimal(rows.Cells[44].Value ?? 0);
decimal AT2 = Convert.ToDecimal(rows.Cells[45].Value ?? 0);
var AU = AN2 * AO2;
rows.Cells[46].Value = AU;
// Max Size = Max(AP2, AU2)
int AK3 = Convert.ToInt32(rows.Cells[36].Value ?? 0);
decimal d1 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[41].Value.ToString()) ? "0" : rows.Cells[41].Value.ToString());
decimal d2 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[46].Value.ToString()) ? "0" : rows.Cells[46].Value.ToString());
decimal d3 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[37].Value.ToString()) ? "0" : rows.Cells[37].Value.ToString());
Math.Max(d1, d2);
d3 = Math.Max(d1,d2);
rows.Cells[37].Value = d3;
var AM = d3 / AK3; //LAYERS =AK2/AL2
rows.Cells[38].Value = AM;
//# 2 Totes Test =AK2/AU2
var AK5 = Convert.ToInt32(rows.Cells[36].Value ?? 0);
decimal AU2 = Convert.ToDecimal(rows.Cells[45].Value ?? 0);
var AV = AK5 / AU2;
rows.Cells[47].Value = AV;
}
}
class CustomDataGridView : DataGridView
{
public CustomDataGridView()
{
DoubleBuffered = true;
}
}
private void btn_Adjust_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing; //or even better .DisableResizing. Most time consumption enum is DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeader
foreach (DataGridViewRow rows in dataGridView1.Rows)
{
for (int i = 0; i < dataGridView1.RowCount ; i++)
{
if (dataGridView1.Rows[i].Cells[24].Value.ToString() == "Yesmix")
{
dataGridView1.Rows[i].Cells[17].Value = "48";
dataGridView1.Rows[i].Cells[16].Value = "45";
var value3 = dataGridView1.Rows[i].Cells[14].Value.ToString();
dataGridView1.Rows[i].Cells[18].Value = value3;
// Add 7 to Totes Adjust
var O = Convert.ToInt32(rows.Cells[14].Value ?? 0);
var Z = 7;
var S2 = Z + O;
dataGridView1.Rows[i].Cells[18].Value = S2;
}
else
{
if (dataGridView1.Rows[i].Cells[24].Value.ToString() == "Yeshh")
{
var value1 = dataGridView1.Rows[i].Cells[13].Value.ToString();
dataGridView1.Rows[i].Cells[17].Value = value1;
var value2 = dataGridView1.Rows[i].Cells[12].Value.ToString();
dataGridView1.Rows[i].Cells[16].Value = value2;
var value3 = dataGridView1.Rows[i].Cells[14].Value.ToString();
dataGridView1.Rows[i].Cells[18].Value = value3;
}
}
}
}
}
}
}
Booney440