简体   繁体   English

使用实体框架将 Datagridview 行插入数据库

[英]Inserting Datagridview rows into database using Entity Framework

If I want to add records to datagridview, it is okay, but I want to add records to the database which rows in datagridview.如果我想向datagridview添加记录,没关系,但是我想向数据库中添加datagridview中的哪些行的记录。

There is a method - how am I adding Datagridview rows .有一种方法 - 我如何添加 Datagridview

public void SepeteEkle()
{
    decimal? tutar;
    decimal? toplamtutar = 0;
    int miktarsum = 0;

    DataRow urun = dt.NewRow();

    tutar = Convert.ToDecimal(lblFiyat.Text) * Convert.ToInt32(txtMiktar.Text);
    bool itemfound = false;

    for (int i = 0; i < dgvSepet.Rows.Count; i++)
    {
        if (Convert.ToString(dgvSepet.Rows[i].Cells[0].Value) == lblStokID.Text)
        {
            miktarsum += Convert.ToInt32(dgvSepet.Rows[i].Cells[4].Value) + Convert.ToInt32(txtMiktar.Text);
            toplamtutar += Convert.ToDecimal(dgvSepet.Rows[i].Cells[5].Value) + tutar;

            dgvSepet.Rows[i].Cells[4].Value = miktarsum;
            dgvSepet.Rows[i].Cells[5].Value = toplamtutar; 

            itemfound = true;
        }
    }

    if (itemfound == false)
    {
        urun["Id"] = lblStokID.Text;
        urun["StokKodu"] = txtStokKodu.Text;
        urun["Ürün"] = txtStokAdi.Text;
        urun["Fiyat"] = lblFiyat.Text;
        urun["Miktar"] = txtMiktar.Text;
        urun["Toplam"] = tutar;

        dt.Rows.Add(urun);
        dgvSepet.DataSource = dt;
    }
}

I want to add database this all created rows using Entity Framework.我想使用 Entity Framework 将所有创建的添加到数据库中。

What I tried:我尝试了什么:

public void DatabaseSepetAktar()
{
    xstHar stokHareketModel = new xstHar();
    stokHareketModel.stharCariKod = txtCariKodu.Text;
    stokHareketModel.stharTarih = DateTime.Now;
    stokHareketModel.stharDovFiyat = Convert.ToDecimal(lblTotal.Text);

    for (int i = 0; i < dgvSepet.Rows.Count -1 ; i++)
    {
        stokHareketModel.masterId = Convert.ToInt32(dgvSepet.Rows[i].Cells[0].Value);
        stokHareketModel.stokKodu = dgvSepet.Rows[i].Cells[1].Value.ToString();
        stokHareketModel.stharGcMik1 = Convert.ToDecimal(dgvSepet.Rows[i].Cells[4].Value);

        using (fastCellDb db = new fastCellDb())
        {
            db.xstHars.Add(stokHareketModel);
            db.SaveChanges();
        }
    }
}

But I get an error但我得到一个错误

Validation exception验证异常

from Entity Framework.来自实体框架。

How can I solve this problem?我怎么解决这个问题?

Validation exception raises when you try to convert value with wrong type, that is due to wrong indexing.当您尝试转换具有错误类型的值时会引发验证异常,这是由于错误的索引。 As you add a row to your DataGridView dt.Rows.Add(urun) using text indexing, change the indexing way from number to text everywhere: Cells[0] → Cells["Id"] etc.当您使用文本索引向 DataGridView dt.Rows.Add(urun)添加一行时,将索引方式从数字更改为文本:Cells[0] → Cells["Id"] 等。

And look to the line: for (int i = 0; i < dgvSepet.Rows.Count -1; i++) , it seems -1 is excess there.看看这条线: for (int i = 0; i < dgvSepet.Rows.Count -1; i++) ,似乎 -1 在那里多余。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM