更新按钮的C#方法 [英] C# method for the update button

查看:78
本文介绍了更新按钮的C#方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有一个与客户跟踪系统相关的WPF项目。

我需要一种方法来更新我的类别表的数据网格如下:



在我的用户界面中,我分别有3个文本框,分类ID,类别名称和说明以及保存,删除和更新按钮。



我正在通过ADO.NET开发这个项目,所以我的方法需要考虑ADO.NET。



我有一个名为CategoryManager的cs文件,我正在开发我的保存,删除和更新方法。请在下面找到我的CategoryManager.cs文件。我需要在CategoryManager.cs中为我的用户界面中的更新按钮提供更新方法。我更喜欢,当我点击datagrid中的相关行时,值会出现在文本框中并准备更新。



我请求你的帮助。



亲切的问候

Gokhan





Dear All,

I have a WPF Project which is related to Customer Tracking System.
I need a method for updating my datagrid for Category table as follows:

In my User Interface, i have 3 textboxes respectively, CategoryID, CategoryName and Description and Save, Delete and Update button.

I am developing this Project via ADO.NET, so my methods need to be written considering ADO.NET.

I have a cs file named CategoryManager and i am developing my Save, Delete and Update methods in there. Please find my CategoryManager.cs file below. I need an update method in CategoryManager.cs for the update button in my user interface. I prefer, when i click the related row in datagrid, the values appear in the textboxes and ready to update.

I kindly request for your helps.

Kind Regards
Gokhan


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyProjectSatis
{
    class CategoryManager
    {
        public static void AddNewItem(Category category)
        {
            FaturaEntities ent = new FaturaEntities();
            ent.Categories.Add(category);
            ent.SaveChanges();
        }

        public static List<Category> GetAllCategories()
        {
            FaturaEntities ent = new FaturaEntities();
            return ent.Categories.ToList();
        }

        public static void DeleteSelectedItem(Category category)
        {
            FaturaEntities ent = new FaturaEntities();
            Category c = ent.Categories.FirstOrDefault(x => x.CategoryID == category.CategoryID);

            ent.Categories.Remove(c);
            ent.SaveChanges();
        }
    }
}

推荐答案

首先,您需要从网格或从网格获取行数据方法并在文本框中绑定('show')这些数据,别忘了在变量或禁用文本框中保存ID



First you need to Get row data from grid or from method and bind('show') this data in your textboxs, dont forget to save ID in variable or disabled textbox

public static Category Get(int id )
       {
           FaturaEntities ent = new FaturaEntities();
           return ent.Categories.Where(x=>x.id==id).FirstOrDefault();
       }





然后更新你需要这种方法



then to update you need this method

public static void Update(Category category)
        {
            using (var dbCtx = new FaturaEntities ())
           {

              dbCtx.Entry(category).State = System.Data.Entity.EntityState.Modified;
              dbCtx.SaveChanges();
           }
        }


这篇关于更新按钮的C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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