我在winform应用程序中使用DevExpress,我有一个gridview,数据输入表单,datanavigator,都绑定到数据集。
我想添加新记录,如果使用datanavigator“添加”它运行良好,如何使用“新记录”按钮执行相同操作?
BindingSource.AddNew()
通常不起作用,但是使用devexpress它不起作用。
答案 0 :(得分:3)
如果要使用绑定,请将对象与绑定源一起使用..
并使用绑定列表.AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
event添加新的实体对象..
请参阅MSDN上的BindingList示例。
void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
}
DevExpress WinForm控件与绑定源相比如此快速,与类型化数据源等相比......你可以使用这些示例实现bindingSources ..
将gridview和关联控件数据源设置为bindsouce您已创建的... 使用此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;
,并且在列表框控件中显示记录网格列表的位置..但是所有相同的...尝试使用gridview .. < / p>