Image is not Set Initially when Data Loaded

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Uses: Windows XP, .NET 2.0; VS 2005; Oracle;
Hi,
I have this code which first loads the Data to DataGridView and then Loops each row and assigns image to DataGridViewImage Column. What I noted is after 2nd Time Loading of Data, then the Image is shown instead of the 1st.
The Code which Runs in the Form_Load Event, At this point afterwards the Drid displays the Data but without Images being displayed though initGrid() function is called. initGrid() function assigns Images.
<pre class="prettyprint private void loadPaymentData()
{
DataSet oDs = null;
DataSet oDsPmode = null;
DataSet oDsBank = null;
DateTime dSettleDate = DateTime.Today;

dataGridView1.AutoGenerateColumns = false;

try
{
Init(null);

oDsBank = ExecuteDataSet("SELECT * FROM JKSBSCHEMA.BANKACCOUNT WHERE BANKACCOUNTNO IS NOT NULL", CommandType.Text, null);
bindingSource3.DataSource = oDsBank;
bindingSource3.DataMember = oDsBank.Tables[0].TableName;

oDsPmode = ExecuteDataSet("SELECT * FROM JKSBSCHEMA.PMODE", CommandType.Text, null);
bindingSource2.DataSource = oDsPmode;
bindingSource2.DataMember = oDsPmode.Tables[0].TableName;

oDs = ExecuteDataSet("SELECT * FROM JKSBSCHEMA.AUTOPAYMENT_VIEW", CommandType.Text, null);

try
{
dSettleDate = Convert.ToDateTime(oDs.Tables[0].Rows[0]["XSACTDATE"].ToString());
label3.Text = string.Format("Data for Settlement Date {0}", dSettleDate.ToString("dd-MMM-yyyy"));
//this.Text = string.Format("Data for Settlement Date {0}; {1} Records Loaded", dSettleDate.ToString("dd-MMM-yyyy"), oDs.Tables[0].Rows.Count.ToString());
}
catch (Exception er)
{
throw er;
}

bindingSource1.DataSource = oDs;
bindingSource1.DataMember = oDs.Tables[0].TableName;

PMODEID.DataSource = oDsPmode.Tables[0];
PMODEID.ValueMember = "PMODEID";
PMODEID.DisplayMember = "PMODEDESC";

BANKACCOUNTID.DataSource = oDsBank.Tables[0];
BANKACCOUNTID.ValueMember = "BANKACCOUNTID";
BANKACCOUNTID.DisplayMember = "BANKACCOUNTNAME";

dataGridView1.DataSource = oDs.Tables[0];

updateStat();

initGrid();

label1.Text = string.Format("No. of Records to be processed {0}", oDs.Tables[0].Rows.Count.ToString("N0"));
}
catch (Exception er)
{
throw er;
}
}[/code]
<br/>

Then afterwards, Data May be loaded again when the User clicks the "Save" button which calls the below code. At this point the Image is displayed.
<pre class="prettyprint private void updatePayment()
{
string sSql = string.Empty;

foreach (DataGridViewRow oDrv in dataGridView1.Rows)
{
if (oDrv.Cells[0].Value != null)
{
sSql = string.Format("UPDATE JKSBSCHEMA.AUTOPAYMENT SET PMODEID = {0}, ISPAY ={1}, BANKACCOUNTID ={2} WHERE REFNO = {3}",
oDrv.Cells["PMODEID"].Value, oDrv.Cells[0].Value, oDrv.Cells["BANKACCOUNTID"].Value, oDrv.Cells["REFNO"].Value);
ExecuteNonQuery(sSql, CommandType.Text, null);

}
}

sSql = "UPDATE AUTOPAYMENT SET ISUPDATED = 1";
ExecuteNonQuery(sSql, CommandType.Text, null);

loadPaymentData();
MessageBox.Show("SAVED");
}

private void updateStat()
{
string sSql = "SELECT " +
"(SELECT PMODEDESC FROM JKSBSCHEMA.PMODE WHERE PMODEID = AUTOPAYMENT.PMODEID) PMODE, "+
"SUM((CASE WHEN ISPAY = 1 THEN 1 ELSE 0 END)*1) PAYING,"+
//"SUM((CASE WHEN ISPAY = 0 THEN 1 ELSE 0 END)*1) NOT_PAYING,"+
"SUM(FINALPAYABLE) AMOUNT "+
"FROM JKSBSCHEMA.AUTOPAYMENT "+
"GROUP BY PMODEID";

Init(null);
DataSet oDs = ExecuteDataSet(sSql, CommandType.Text, null);
dataGridView2.AutoGenerateColumns = false;
dataGridView2.DataSource = oDs.Tables[0];
}[/code]
<br/>

Assigning of Image is done here:
<pre class="prettyprint private void initGrid()
{
Double dContractValue = 0;
Double dFinalPayValue = 0;
Bitmap bitmap = null;

foreach (DataGridViewRow dr in dataGridView1.Rows)
{
try
{
dContractValue = Convert.ToDouble(dr.Cells["CONTRACT_VALUE"].Value.ToString());
dFinalPayValue = Convert.ToDouble(dr.Cells["FINALPAYABLE"].Value.ToString());
}
catch (Exception)
{
dContractValue = 0; dFinalPayValue = 0;
}

//Condition Check
if (dContractValue != dFinalPayValue)
{
bitmap = new Bitmap(EquityBroker_Resource.WarningHS as Bitmap);
dr.Cells["STATUS"].Value = bitmap;
}
else
{
//bitmap = new Bitmap(20, 20);
//Graphics graphics = Graphics.FromImage(bitmap);
//graphics.FillRectangle(Brushes.Transparent, 0, 0, 20, 20);
//dr.Cells["STATUS"].Value = bitmap;
//graphics.Dispose();

bitmap = new Bitmap(EquityBroker_Resource.blank as Bitmap);
dr.Cells["STATUS"].Value = bitmap;
}

//Trace Check
string sTrace = getTraceLog(dr.Cells["REFNO"].Value.ToString());

if (sTrace != string.Empty)
{
dr.DefaultCellStyle.BackColor = Color.NavajoWhite;
dr.Cells["STATUS"].ToolTipText = sTrace;
}
}
}
[/code]
My DataGridView looks like this:
<pre class="prettyprint //
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.dataGridView1.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.PAY,
this.ACCOUNTID,
this.ACCOUNTNAME,
this.STATUS,
this.PAYINSTRUCTIONLINE1,
this.PAYINSTRUCTIONLINE2,
this.ADDITIONAL_PAY_INSTRUCTION,
this.XSACTDATE,
this.NORMALPAYABLE,
this.SOPAYABLE,
this.FINALPAYABLE,
this.PMODEID,
this.REFNO,
this.BANKACCOUNTID,
this.CONTRACT_VALUE});
dataGridViewCellStyle11.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle11.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle11.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle11.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle11.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle11.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle11.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle11;
this.dataGridView1.GridColor = System.Drawing.Color.DarkKhaki;
this.dataGridView1.Location = new System.Drawing.Point(12, 115);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 40;
this.dataGridView1.RowTemplate.Height = 25;
this.dataGridView1.ShowCellToolTips = false;
this.dataGridView1.Size = new System.Drawing.Size(936, 458);
this.dataGridView1.TabIndex = 4;
this.dataGridView1.CellMouseLeave += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellMouseLeave);
this.dataGridView1.CellMouseMove += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridView1_CellMouseMove);
this.dataGridView1.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.dataGridView1_CellValidating);
this.dataGridView1.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView1_DataError);
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
//
// PAY
//
this.PAY.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
this.PAY.DataPropertyName = "ISPAY";
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.White;
dataGridViewCellStyle1.NullValue = false;
this.PAY.DefaultCellStyle = dataGridViewCellStyle1;
this.PAY.FalseValue = "0";
this.PAY.Frozen = true;
this.PAY.HeaderText = "PAY";
this.PAY.IndeterminateValue = "0";
this.PAY.Name = "PAY";
this.PAY.TrueValue = "1";
this.PAY.Width = 40;
//
// ACCOUNTID
//
this.ACCOUNTID.DataPropertyName = "ACCOUNTID";
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Yellow;
this.ACCOUNTID.DefaultCellStyle = dataGridViewCellStyle2;
this.ACCOUNTID.Frozen = true;
this.ACCOUNTID.HeaderText = "ACCOUNTID";
this.ACCOUNTID.Name = "ACCOUNTID";
this.ACCOUNTID.Width = 90;
//
// ACCOUNTNAME
//
this.ACCOUNTNAME.DataPropertyName = "ACCOUNTNAME";
this.ACCOUNTNAME.HeaderText = "ACCOUNTNAME";
this.ACCOUNTNAME.Name = "ACCOUNTNAME";
this.ACCOUNTNAME.Width = 150;
//
// STATUS
//
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle3.NullValue = "System.Drawing.Bitmap";
//dataGridViewCellStyle3.NullValue = null;
this.STATUS.DefaultCellStyle = dataGridViewCellStyle3;
this.STATUS.FillWeight = 30F;
this.STATUS.HeaderText = "";
this.STATUS.Name = "STATUS";
this.STATUS.Width = 30;
//
// PAYINSTRUCTIONLINE1
//
this.PAYINSTRUCTIONLINE1.DataPropertyName = "PAYINSTRUCTIONLINE1";
dataGridViewCellStyle4.SelectionForeColor = System.Drawing.Color.Yellow;
this.PAYINSTRUCTIONLINE1.DefaultCellStyle = dataGridViewCellStyle4;
this.PAYINSTRUCTIONLINE1.HeaderText = "PAYINSTRUCTIONLINE1";
this.PAYINSTRUCTIONLINE1.Name = "PAYINSTRUCTIONLINE1";
this.PAYINSTRUCTIONLINE1.Visible = false;
//
// PAYINSTRUCTIONLINE2
//
this.PAYINSTRUCTIONLINE2.DataPropertyName = "PAYINSTRUCTIONLINE2";
this.PAYINSTRUCTIONLINE2.HeaderText = "PAYINSTRUCTIONLINE2";
this.PAYINSTRUCTIONLINE2.Name = "PAYINSTRUCTIONLINE2";
this.PAYINSTRUCTIONLINE2.Visible = false;
//
// ADDITIONAL_PAY_INSTRUCTION
//
this.ADDITIONAL_PAY_INSTRUCTION.ContextMenuStrip = this.contextMenuStrip1;
this.ADDITIONAL_PAY_INSTRUCTION.DataPropertyName = "ADDITIONAL_PAY_INSTRUCTION";
this.ADDITIONAL_PAY_INSTRUCTION.HeaderText = "PAY INSTRUCTION";
this.ADDITIONAL_PAY_INSTRUCTION.Name = "ADDITIONAL_PAY_INSTRUCTION";
this.ADDITIONAL_PAY_INSTRUCTION.Width = 140;
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.changePaymentInstructionToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(212, 26);
//
// changePaymentInstructionToolStripMenuItem
//
this.changePaymentInstructionToolStripMenuItem.Name = "changePaymentInstructionToolStripMenuItem";
this.changePaymentInstructionToolStripMenuItem.Size = new System.Drawing.Size(211, 22);
this.changePaymentInstructionToolStripMenuItem.Text = "Change Payment Instruction";
this.changePaymentInstructionToolStripMenuItem.Click += new System.EventHandler(this.changePaymentInstructionToolStripMenuItem_Click);
//
// XSACTDATE
//
this.XSACTDATE.DataPropertyName = "XSACTDATE";
dataGridViewCellStyle5.SelectionForeColor = System.Drawing.Color.Yellow;
this.XSACTDATE.DefaultCellStyle = dataGridViewCellStyle5;
this.XSACTDATE.HeaderText = "XSACTDATE";
this.XSACTDATE.Name = "XSACTDATE";
this.XSACTDATE.Visible = false;
this.XSACTDATE.Width = 90;
//
// NORMALPAYABLE
//
this.NORMALPAYABLE.DataPropertyName = "NORMALPAYABLE";
dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
dataGridViewCellStyle6.Format = "N2";
this.NORMALPAYABLE.DefaultCellStyle = dataGridViewCellStyle6;
this.NORMALPAYABLE.HeaderText = "NET PAYABLE";
this.NORMALPAYABLE.Name = "NORMALPAYABLE";
this.NORMALPAYABLE.Visible = false;
this.NORMALPAYABLE.Width = 80;
//
// SOPAYABLE
//
this.SOPAYABLE.DataPropertyName = "SOPAYABLE";
dataGridViewCellStyle7.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
dataGridViewCellStyle7.Format = "N2";
dataGridViewCellStyle7.NullValue = "0";
this.SOPAYABLE.DefaultCellStyle = dataGridViewCellStyle7;
this.SOPAYABLE.HeaderText = "PLEDGE PAYABLE";
this.SOPAYABLE.Name = "SOPAYABLE";
this.SOPAYABLE.Visible = false;
this.SOPAYABLE.Width = 80;
//
// FINALPAYABLE
//
this.FINALPAYABLE.DataPropertyName = "FINALPAYABLE";
dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
dataGridViewCellStyle8.Format = "N2";
dataGridViewCellStyle8.NullValue = "0";
dataGridViewCellStyle8.SelectionForeColor = System.Drawing.Color.Yellow;
this.FINALPAYABLE.DefaultCellStyle = dataGridViewCellStyle8;
this.FINALPAYABLE.HeaderText = "PAYMENT VALUE";
this.FINALPAYABLE.Name = "FINALPAYABLE";
this.FINALPAYABLE.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.FINALPAYABLE.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.FINALPAYABLE.Width = 80;
//
// PMODEID
//
this.PMODEID.DataPropertyName = "PMODEID";
dataGridViewCellStyle9.SelectionForeColor = System.Drawing.Color.Yellow;
this.PMODEID.DefaultCellStyle = dataGridViewCellStyle9;
this.PMODEID.HeaderText = "PAYMENT VIA";
this.PMODEID.Name = "PMODEID";
this.PMODEID.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.PMODEID.Width = 80;
//
// REFNO
//
this.REFNO.DataPropertyName = "REFNO";
dataGridViewCellStyle10.SelectionForeColor = System.Drawing.Color.Yellow;
this.REFNO.DefaultCellStyle = dataGridViewCellStyle10;
this.REFNO.HeaderText = "REFNO";
this.REFNO.Name = "REFNO";
this.REFNO.Width = 50;
//
// BANKACCOUNTID
//
this.BANKACCOUNTID.DataPropertyName = "BANKACCOUNTID";
this.BANKACCOUNTID.HeaderText = "DRAW BANK";
this.BANKACCOUNTID.Name = "BANKACCOUNTID";
this.BANKACCOUNTID.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.BANKACCOUNTID.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
//
// CONTRACT_VALUE
//
this.CONTRACT_VALUE.DataPropertyName = "CONTRACT_VALUE";
this.CONTRACT_VALUE.HeaderText = "CONTRACT_VALUE";
this.CONTRACT_VALUE.Name = "CONTRACT_VALUE";
this.CONTRACT_VALUE.Visible = false;[/code]
<br/>
Can Anybody help me on this? I cant figure it out why this happens.

<hr class="sig Hifni Shahzard Nazeer M.

View the full article
 
Back
Top