简体   繁体   中英

C# Winform Datagrid CRUD

i use 3-Tiers Application. I have created Data acces layer and Business Layer. How can i bind this with my Datagrid on View layer?

public partial class Magasinier : Form
{
    Magasinier_BL oMag_BL = new Magasinier_BL();

    public Magasinier()
    {
        InitializeComponent();
        GetDataOrdre();
    }

    public void GetDataOrdre()
    {            
        dataGridView_Mag.DataSource = oMag_BL.Get_All_Magasinier_BL();
        dataGridView_Mag.DataMember = "MagTable";
    }


}

  private void Del_Mag(object sender, DataGridViewRowCancelEventArgs e)
    {
        if (!e.Row.IsNewRow)
        {
            DialogResult res = MessageBox.Show("Etes-vous sûr de vouloir supprimer cette ligne ?", "confirmation suppression",
                     MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                oMag_BL.DelMag_BL(e.Row.Cells["CODE_MAG"].Value.ToString());
            }

        }
    }

i have done the Read and Delete, but not Create and Update.

How can i cable these Create and Update to my businnes layer, by the way, how can i retrive NEW row value or CHANGE row value.

these what i did in ASP.NET: Create

 protected void Insrting_Obj_ClientMag(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
    Pers_Magasinier oPersMag = new Pers_Magasinier();
    oPersMag.NoClient = Id;
    oPersMag.CodeMag = e.NewValues["CODE_MAG"].ToString();
    oPersMag.NomUsr = e.NewValues["NOM"].ToString();
    oPersMag.PrenomUsr = e.NewValues["PRENOM"].ToString();
    oPersMag.MemoMag = e.NewValues["MEMO"].ToString();

    oMag_BL.InstUpdtMag_BL(oPersMag, true);       

    //To Stop processing Gridview
    ASPxGridView_Mag.CancelEdit();
    e.Cancel = true;

    //Rebind donne
    GetDataMags();
}

and Update:

protected void Updting_Obj_ClientMag(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
    Pers_Magasinier oPersMag = new Pers_Magasinier();
    oPersMag.NoClient = Id;
    oPersMag.CodeMag = e.NewValues["CODE_MAG"].ToString();
    oPersMag.NomUsr = e.NewValues["NOM"].ToString();
    oPersMag.PrenomUsr = e.NewValues["PRENOM"].ToString();
    oPersMag.MemoMag = e.NewValues["MEMO"].ToString();

    oMag_BL.InstUpdtMag_BL(oPersMag, false);      

    ASPxGridView_Mag.CancelEdit();
    e.Cancel = true;
    GetDataMags();
}

Now how can i do that in WinForm?

Thanks you in advance

Update: To get Create/Update working, you usually use Grid RowEditTemplate control (which basically is another WinForm which allows you to do extra validation and create/update existing values). Infragistics got good one.

  1. You will need to make sure you are using BindingSource with your Grid so that when you update the item in your Collection, it automatically gets updated in the Grid
  2. You will also need to make sure your BusinessObject/DomainModel/CollectionItem implements INotifyPropertyChanged, so that when you update the values within your business layer, it gets updated in the grid automatically
  3. When you click on Create/Update button, it will load existing values (if updating, otherwise new default values) into Row Edit Template Control
  4. When hit Submit Create, it will create new BusinessObject and add this into your Collection. as you collection is bound to the grid, Grid will automatically get refreshed.
  5. Same with Submit Update, if you updating the same references object in the collection, any update should get reflected in the Grid

you might need to fix your tags, because you are talking about ASP and not WinForms.

I am using Telerik's Grid with ASP MVC 2 atm. Helps a lot with serializing your data into the ViewModel and you work with ViewModel in your Controllers (dont have to mess around with Request.Params most of the time).

See Telerik Grid and ASP MVC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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