How to improve performance of datagridview

Hi All,

In my project i have made one monitor for showing current price and all market,its related to share bazar, for that i used datagridview and i m binding data with datagridview every second.

but i faced problem with flikering in datagridview.

so plz help me who to remove flikering.

I used Datasource property for binding data

[371 byte] By [Tejas34] at [2007-12-24]
# 1
There are so many things that can affect performance. Could you post some code.
KenTucker at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Hi Ken Tucker

at a time i am tring to bind 200 to 300 records

I have called following method every 1 second

private void BindRecords()

{

try

{

//Call method for get records

dstOrderRecords = objOrderMonitor.GetRecord();

if (dstOrderRecords.Tables.Count != 0)

{

this.MonitorRowCount = dstOrderRecords.Tables[0].Rows.Count;

dstOrderRecords.Tables[0].Merge(dstOrderRecords.Tables[1]);

DataView dvwOrderRecords = new DataView(dstOrderRecords.Tables[0]);

if (IsOnlyViewHitOrders) // Check whether to view only Hit Orders or all Orders

{

dvwOrderRecords.RowFilter = "OOB_ORDER_STATUS = " + ApplicationBase.ORDERSTATUS_AWAITING_EXECUTION.ToString();

}

dgvOrderMonitor.DataSource = dvwOrderRecords;

}

}

catch (System.Exception ex)

{

base.MsgWindow.AppendText(MessageConstants.EXCEPTION_UNKNOWN_MSG, this.Text, ex);

timerOrderMonitor.Stop();

}

}

and my DataSourceChange Event Code is

private void dgvOrderMonitor_DataSourceChanged(object sender, EventArgs e)

{

try

{

//Set boolen value for Alertbell.If any HitOrderExist then start belling else stop belling

//and aslo use for Enable and disable View AllOrders/View HitOrders button

bool blnisHitOrderExist = false;

//Use for Enable and disable ResetAll button

//If Order with OrderType New,Stop or Limit and OrderStatus is Awaiting Execution

//exist then n then ResetAll button enable

bool blnIsResetAllEnable = false;

//Loop for each rows of dgvOrderMonitor and set backcolor and forecolor of respective row(s), and cell(s)

foreach (DataGridViewRow rowObj in dgvOrderMonitor.Rows)

{

//If OrderId != null means if any data exist in datagridview then start

//process of changing color of hitorder rows and pending order rows

if (rowObj.Cells["OrderID"].Value != null)

{

if (Convert.ToInt32(rowObj.Cells["Status"].Value).CompareTo(ApplicationBase.ORDERSTATUS_PENDING) == 0)

{

//Call method for set style on current row

this.SetRowStyle(rowObj, this.MonitorForeColor, this.MonitorBackColor);

rowObj.Cells["Amend_Reset"].Value = "Amend";

}

else if (Convert.ToInt32(rowObj.Cells["Status"].Value).CompareTo(ApplicationBase.ORDERSTATUS_AWAITING_EXECUTION) == 0)

{

if (!blnisHitOrderExist)

blnisHitOrderExist = true;

//Call method for set style on current row

this.SetRowStyle(rowObj, this.HitOrderForeColor, this.HitOrderBackColor);

rowObj.Cells["Amend_Reset"].Value = "Reset";

//Check whether any HitOder exist with Order type is New,Stop or Limit

//if any order exist then enable ResetAll button

if ((string.Compare(rowObj.Cells["OrderType"].Value.ToString().ToUpper(), ApplicationBase.OrderType.New.ToString().ToUpper(), true) == 0) && !blnIsResetAllEnable ||

(string.Compare(rowObj.Cells["OrderType"].Value.ToString().ToUpper(), ApplicationBase.OrderType.Stop.ToString().ToUpper(), true) == 0) && !blnIsResetAllEnable ||

(string.Compare(rowObj.Cells["OrderType"].Value.ToString().ToUpper(), ApplicationBase.OrderType.Limit.ToString().ToUpper(), true) == 0) && !blnIsResetAllEnable

)

{

blnIsResetAllEnable = true;

}

}

//If Margin > Available Resources then change backcolor and forecolor of that two cells.

if (Convert.ToDecimal(rowObj.Cells["MarginRequired"].Value) > Convert.ToDecimal(rowObj.Cells["AvailableResource"].Value))

{

//Call method for set style on current cell

this.SetCellStyle(rowObj.Cells["MarginRequired"], this.MarginBreachForeColor, this.MarginBreachBackColor);

this.SetCellStyle(rowObj.Cells["AvailableResource"], this.MarginBreachForeColor, this.MarginBreachBackColor);

}

//If OrderType is Rollover or Market/Trade or Close then change backcolor and forecolor of OrderType cells.

//and disable reset button for this type of order(s)

if ((string.Compare(rowObj.Cells["OrderType"].Value.ToString().ToUpper(), ApplicationBase.OrderType.Rollover.ToString().ToUpper(), true) == 0) ||

(string.Compare(rowObj.Cells["OrderType"].Value.ToString().ToUpper(), ApplicationBase.OrderType.Close.ToString().ToUpper(), true) == 0) ||

(string.Compare(rowObj.Cells["OrderType"].Value.ToString().ToUpper(), ApplicationBase.OrderType.Trade.ToString().ToUpper(), true) == 0)

)

{

//Call method for set style on current cell

this.SetCellStyle(rowObj.Cells["OrderType"], this.OrderTypeForeColor, this.OrderTypeBackColor);

//disable reset button

DataGridViewDisableButtonCell buttonReset = (DataGridViewDisableButtonCell)rowObj.Cells["Amend_Reset"];

buttonReset.Enabled = false;

buttonReset.TextForeColor = Color.Black;

dgvOrderMonitor.InvalidateCell(buttonReset);

}

}

}

//Check whether HitOrder(Red Zone) Exist.

//If exist then start Alert bell till all hit order not execute.

//and if not Exist then stop Alert bell till new Hit order not come.

if (blnisHitOrderExist && objSoundPlayer.SoundLocation.Length > 0)

objSoundPlayer.PlayLooping();

else if (!blnisHitOrderExist)

objSoundPlayer.Stop();

//Enable or Disable ResetAll button

if (blnIsResetAllEnable)

btnResetAll.Enabled = true;

else

btnResetAll.Enabled = false;

}

catch (System.Exception ex)

{

base.MsgWindow.AppendText(MessageConstants.EXCEPTION_UNKNOWN_MSG, this.Text, ex);

}

}

Tejas34 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
You are changing the datagridviews data source every second of course it is going to flicker. Try and modify the data in the data source with out rebinding it to the grid every second.
KenTucker at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

Hi Ken,

It is not possible in myScenario.

What is my requirement is each and every second so many new data r inserted in to table, and i want all new and old data which satisfy some condition.

so how can it possible without rebinding.

My Project is related to Share Bazar. clients from different different places puts orders for purchasing or selling some market share(Its a live trading).so each n every second i fatch all records from DB which satisfy my condition and and display in datagridview in spacific Order.Ordering is also custom.

So there is no fix rows, the number of rows r different all times, so i think no possibility to bind one time and change individual rows or cell value each second.

Tejas34 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

i did not fully went though the code, but,

are you binding to one or multiple tables.

if it is only one table, the what you do it take out the part where you bind the dgv in the recursive code, then only update your table via adapter, the changes will reflect on the grid, you need not set the source again all the times.

if it is multiple tables, then you should find an alternative ways to bring all your data to one table , maybe in your database, try a view. , then same principle, adapter.update only code,.

hrubesh at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...