Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I am creating a WinForm app and one form I have a dgv which is databound to a SQL database. I have almost got it figured out how to programatically add more roes to the dgv, but have some obstacles to overcome. The code below is from the event where everything happens.
It reads the RefRate_Acc table, filtered by Year, Month, Account. If any records are found, they are displayed in the dgv.
If none are found, then the ForexPairSelected table is read. Unique values are stored in a new datatable, and the dgv is populated with the values. The dgv may be populated with only one new record or as many as 25 new records, before the user makes any changes to the data.
This works 100% if only one record needs to ne inserted into the dgv.
However, I get an Index out of range error message on subsequent loops of the for statement.
What am I doing wrong?

C#
private void btnSearch_Click(object sender, EventArgs e)
{
    try
    {
        this.RefYear = int.Parse(this.cmbYear.Text.ToString());
        this.RefMonth = int.Parse(this.cmbMonth.Text.ToString());
        this.AccID = int.Parse(this.txtAccID.Text);

        // TODO: This line of code loads data into the 'dsPortMan.RefRate_Acc' table.
        // You can move, or remove it, as needed.
        this.taRefRate_Acc.FillByAcc(this.dsPortMan.RefRate_Acc, this.RefYear, this.RefMonth, this.AccID);
        this.rc = this.dsPortMan.RefRate_Acc.Count;

        if (this.rc > 0)
        {
            this.taRefRate_Acc.FillByAcc(this.dsPortMan.RefRate_Acc, this.RefYear, this.RefMonth, this.AccID);
        }
        else
        {
            this.taForexPairSel.FillByAcc(this.dsPortMan.ForexPairSelection, this.AccID);

            DataTable dtUniqueForex = this.dsPortMan.Tables["ForexPairSelection"].DefaultView.ToTable(true, "ForexPairID", "ForexPairCode");

            this.cmbForexPairSelID.DataSource = dtUniqueForex;
            this.cmbForexPairSelID.DisplayMember = "ForexPairID";
            this.cmbForexPairSel.DataSource = dtUniqueForex;
            this.cmbForexPairSel.DisplayMember = "ForexPairCode";

            this.fp = dtUniqueForex.Rows.Count;

            for (int i = 0; i < fp; i++)
            {
                this.dgvAccRefRate[1, i].Value = this.cmbYear.Text;
                this.dgvAccRefRate[2, i].Value = this.cmbMonth.Text;
                this.dgvAccRefRate[3, i].Value = this.txtAccID.Text;
                this.dgvAccRefRate[4, i].Value = this.cmbAccount.Text;
                this.dgvAccRefRate[5, i].Value = this.cmbForexPairSelID.Text;
                this.dgvAccRefRate[6, i].Value = this.cmbForexPairSel.Text;
                if (fp > 1)
                {
                    this.dsPortMan.RefRate_Acc.NewRow();
                    this.dgvAccRefRate.Refresh();
                }
            }
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "An Error has occured", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
}


Thanx
Posted

Hi,

Have you checked NewRow as below-

C#
for (int i = 0; i < fp; i++)
{
    this.dgvAccRefRate[0, i].Value = this.cmbYear.Text;
    this.dgvAccRefRate[1, i].Value = this.cmbMonth.Text;
    this.dgvAccRefRate[2, i].Value = this.txtAccID.Text;
    this.dgvAccRefRate[3, i].Value = this.cmbAccount.Text;
    this.dgvAccRefRate[4, i].Value = this.cmbForexPairSelID.Text;
    this.dgvAccRefRate[5, i].Value = this.cmbForexPairSel.Text;
    if (fp > 1)
    {
        this.dsPortMan.RefRate_Acc.NewRow();
        this.dgvAccRefRate.Refresh();
    }
}



Nilesh Shah.
 
Share this answer
 
I have changed the last portion of the event as follows:

DataRow drARR;
this.fp = dtUniqueForex.Rows.Count;
for (int i = 0; i < fp; i++)
{
    this.cmbForexPairSelID.SelectedIndex = i;
    this.cmbForexPairSel.SelectedIndex = i;
    drARR = this.dsPortMan.RefRate_Acc.NewRow();
    drARR[1] = this.RefYear;
    drARR[2] = this.RefMonth;
    drARR[3] = this.AccID;
    drARR[4] = this.cmbForexPairSelID.Text;
    drARR[5] = 0;
    drARR[6] = "A_RR";
    this.dsPortMan.RefRate_Acc.BeginInit();
    this.dsPortMan.RefRate_Acc.Rows.Add(drARR);
    this.dsPortMan.RefRate_Acc.EndInit();
    this.dgvAccRefRate.Refresh();
    this.dgvAccRefRate[4, i].Value = this.cmbAccount.Text;
    this.dgvAccRefRate[6, i].Value = this.cmbForexPairSel.Text;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900