向数据绑定的datagridview中添加行 [英] Add rows to a datagridview which is databound

查看:131
本文介绍了向数据绑定的datagridview中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在创建一个WinForm应用程序,一个窗体有一个dgv,它已数据绑定到SQL数据库.我几乎了解到了如何以编程方式向dgv添加更多鱼卵,但仍有一些障碍需要克服.下面的代码来自一切发生的事件.
它读取RefRate_Acc表,并按年,月,帐户过滤.如果找到任何记录,它们将显示在dgv中.
如果未找到,则读取ForexPairSelected表.唯一值存储在新的数据表中,并且用值填充dgv.在用户对数据进行任何更改之前,dgv可能仅填充一个新记录或多达25个新记录.
如果仅需要一条记录插入dgv,则此方法有效100%.
但是,我在for语句的后续循环中收到索引超出范围"错误消​​息.
我究竟做错了什么?

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?

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



Thanx

推荐答案



您是否按以下方式检查过NewRow-

Hi,

Have you checked NewRow as below-

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.




Nilesh Shah.


我将事件的最后一部分更改如下:

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;
}


这篇关于向数据绑定的datagridview中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆