1 year ago
#357482
M.Bouabdallah
Prevent BindingSource from adding two blank rows to gridview
I have BindingSource that attached to grid control like this
private BindingSource listDepartmentDto = new();
private async Task LoadDataAsync()
{
listDepartmentDto.DataSource = await departmentService.GetAllDepartmentsAsync();
gridControl1.DataSource = listDepartmentDto;
}
And I binding Controls like this
private void BindingControls()
{
var departmentDto = listDepartmentDto.Current as DepartmentDto;
txtID.DataBindings.Add(new Binding("EditValue", listDepartmentDto, nameof(departmentDto.IdDepartment)));
txtTitle.DataBindings.Add(new Binding("EditValue", listDepartmentDto, nameof(departmentDto.NameDepartment)));
txtDescription.DataBindings.Add(new Binding("EditValue", listDepartmentDto, nameof(departmentDto.DescriptionDepartment)));
}
And I use CurrentChanged event like this
listDepartmentDto.CurrentChanged += new EventHandler(listDepartmentDtoChanged);
private void listDepartmentDtoChanged(object sender,EventArgs e)
{
var departmentDto = listDepartmentDto.Current as DepartmentDto;
if (departmentDto.IdDepartment == 0)
{
btnSave.Text = Resources.Save;
btnNew.Enabled = false;
btnDelete.Enabled = false;
txtTitle.Focus();
return;
}
btnSave.Text = Resources.Edit;
btnNew.Enabled = true;
btnDelete.Enabled = true;
}
And in btnNew click event like this
private async void btnNew_Click(object sender, EventArgs e)
{
listDepartmentDto.AddNew();
await Permission();
}
The problem is when I click on btnNew it create blank row and when I select an other row and click again on btnNew it create new blank row and so on.
How can I prevent this behavior? and if the user move to an other row without entry any data the row removed automatically
The grid view edtiable has set to false.
the grid view (add,remove) row has set to false.
c#
winforms
bindingsource
0 Answers
Your Answer