DevExpress数据绑定,添加新记录

本文关键字:新记录 添加 数据绑定 DevExpress | 更新日期: 2023-09-27 18:14:58

我在我的winform应用程序中使用DevExpress,我有一个gridview,数据输入表单,数据导航器,所有绑定到数据集。

我想添加新记录,如果使用数据导航器"添加"效果很好,如何使用"新记录"按钮做同样的事情?

BindingSource.AddNew() 

不工作,通常是这样,但是对于devexpress,它不工作

DevExpress数据绑定,添加新记录

如果你想使用绑定,那就使用带有绑定源的对象。

,使用绑定列表.AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);事件添加新的实体对象。

参见MSDN上的BindingList示例

void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
        }

DevExpress WinForm控件与绑定源相比,与类型化数据源等相比工作得如此之快…你可以使用这些例子来实现bindingSources ..

设置gridview和关联的控件数据源为你已经创建的bindsource…用这个MSDN示例处理表单。

看一下这个代码片段。也许你会从中得到一些启示。

private void BindingLIstDemo_Load(object sender, EventArgs e)
        {
            InitializeListOfEmployees();
            BindlstEmp();
            listofEmp.AddingNew += new AddingNewEventHandler(listOfEmp_AddingNew);
            listofEmp.ListChanged += new ListChangedEventHandler(listofEmp_ListChanged);
        }
        private void BindlstEmp()
        {
            lstEmpList.Items.Clear();
            lstEmpList.DataSource = listofEmp;
            lstEmpList.DisplayMember = "Name";
        }
        void listofEmp_ListChanged(object sender, ListChangedEventArgs e)
        {
            MessageBox.Show(e.ListChangedType.ToString());
                //throw new NotImplementedException();
        }
        //declare list of employees
        BindingList<Emp> listofEmp;
        private void InitializeListOfEmployees()
        {
            //throw new NotImplementedException();
            // Create the new BindingList of Employees.
            listofEmp = new BindingList<Emp>();
            // Allow new Employee to be added, but not removed once committed.
            listofEmp.AllowNew = true;
            listofEmp.AllowRemove = true;
            // Raise ListChanged events when new Employees are added.
            listofEmp.RaiseListChangedEvents = true;
            // Do not allow Employee to be edited.
            listofEmp.AllowEdit = false;
            listofEmp.Add(new Emp(1, "Niranjan", 10000));
            listofEmp .Add (new Emp (2,"Jai", 8000));
          }

        // Create a new Employee from the text in the two text boxes.
        void listOfEmp_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Emp (Convert.ToInt32(txtId.Text), txtName.Text,Convert.ToInt32(txtSalary.Text));
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Emp empItem = listofEmp.AddNew();
            txtId.Text = txtName.Text = txtSalary.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 obj = new Form1();
            obj.Show();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            var sg = (from sc in listofEmp.ToList<Emp>() where sc.Name == ((Emp)lstEmpList.SelectedValue).Name select sc);


        }
        private void lstEmpList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Emp se = listofEmp[lstEmpList.SelectedIndex];
            txtId.Text = se.Id.ToString();
            txtName.Text = se.Name;
            txtSalary.Text = se.Salary.ToString();
        }

在这里,我使用BindingList作为数据源BindingList<Emp> listofEmp;,并在列表框控件中显示记录的网格列表的位置。但都一样……