简体   繁体   English

C# Winform Datagrid CRUD

[英]C# Winform Datagrid CRUD

i use 3-Tiers Application.我使用 3 层应用程序。 I have created Data acces layer and Business Layer.我已经创建了数据访问层和业务层。 How can i bind this with my Datagrid on View layer?如何将它与视图层上的 Datagrid 绑定?

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这些我在 ASP.NET 中所做的:创建

 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?现在我如何在 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).更新:为了让创建/更新工作,您通常使用 Grid RowEditTemplate 控件(它基本上是另一个 WinForm,它允许您进行额外的验证和创建/更新现有值)。 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您需要确保在 Grid 中使用 BindingSource,这样当您更新 Collection 中的项目时,它会自动在 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您还需要确保您的 BusinessObject/DomainModel/CollectionItem 实现 INotifyPropertyChanged,以便在您更新业务层中的值时,它会自动在网格中更新
  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.当点击提交创建时,它将创建新的 BusinessObject 并将其添加到您的集合中。 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与提交更新相同,如果您更新集合中的相同引用 object,任何更新都应反映在网格中

you might need to fix your tags, because you are talking about ASP and not WinForms.您可能需要修复您的标签,因为您在谈论 ASP 而不是 WinForms。

I am using Telerik's Grid with ASP MVC 2 atm.我将 Telerik 的 Grid 与 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).有助于将数据序列化到 ViewModel 中,并且您可以在控制器中使用 ViewModel(大多数时候不必乱用 Request.Params)。

See Telerik Grid and ASP MVC请参阅 Telerik 网格ASP MVC

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

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