windows DevExpress 数据绑定,添加新记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8177896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DevExpress DataBinding, Add new Record
提问by Milan Solanki
I am using DevExpress in my winform application, I have a gridview, data entry form, datanavigator, all bound to dataset.
我在我的 winform 应用程序中使用 DevExpress,我有一个网格视图、数据输入表单、数据导航器,都绑定到数据集。
I want to add new record, if using datanavigator "Add" it works good, how to do the same using a "New Record" button?
我想添加新记录,如果使用数据导航器“添加”效果很好,如何使用“新记录”按钮来做同样的事情?
BindingSource.AddNew()
is not working, it usually does, but with devexpress its not working.
不起作用,它通常会起作用,但是 devexpress 不起作用。
回答by Niranjan Singh
If you want to use binding then use your objects with binding source..
如果要使用绑定,请使用带有绑定源的对象。
and use the binding list .AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
event to add new entity object ..
并使用绑定列表.AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
事件添加新的实体对象..
See the example of BindingList on MSDN.
请参阅MSDN上的 BindingList 示例。
void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
}
DevExpress WinForm Controls works so fast with binding sources as compare to typed datasources etc... YOu can implement bindingSources using these example..
与类型化数据源等相比,DevExpress WinForm 控件在绑定源方面的工作速度如此之快......您可以使用这些示例实现 bindingSources..
set gridview and the associcated controls datasource to bindsouce that you have created... process your form with the this MSDN example..
将 gridview 和关联的控件数据源设置为您创建的 bindsouce... 使用此 MSDN 示例处理您的表单..
have a look on this code snippet.. may be you will get some idea from this..
看看这个代码片段..也许你会从中得到一些想法..
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();
}
Here I am using BindingList
as datasouce BindingList<Emp> listofEmp;
and on the place of grid listing of records are shown in a listbox control.. but all same...try this with your gridview..
在这里,我使用BindingList
作为数据源,BindingList<Emp> listofEmp;
并且在列表框控件中显示记录的网格列表的位置.. 但都一样......用你的 gridview 试试这个..