1

I have a DataGridView that has binding with user class and text boxses the has a binding with each property of the class. My problem is that after clicking on cell the textboxes shows the row values but when I try to save the class has all the properties as null. this is my code:

User user = new User();
private void UcUsers_Load(object sender, EventArgs e)
{
    UserdataGridView.DataSource = LibraryManagment.getDBUsers();
    userBindingSource.DataSource = user;
}
private void UserdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    UserdataGridView.CurrentRow.Selected = true;
    userIdBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersIdDataGridViewTextBoxColumn"].Value.ToString();
    fnameBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersFirstNameDataGridViewTextBoxColumn"].Value.ToString();
    lnameBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersLastNameDataGridViewTextBoxColumn"].Value.ToString();
    unameBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersUserNameDataGridViewTextBoxColumn"].Value.ToString();
    passwordbox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersPasswordDataGridViewTextBoxColumn"].Value.ToString();
    enailBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersEmailDataGridViewTextBoxColumn"].Value.ToString();
    roleCmb.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersRoleDataGridViewTextBoxColumn"].Value.ToString();
    phoneBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersPhoneDataGridViewTextBoxColumn"].Value.ToString();
    tzBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["userTzIdDataGridViewTextBoxColumn"].Value.ToString();
    addressBox.Text = UserdataGridView.Rows[e.RowIndex].Cells["usersAddressDataGridViewTextBoxColumn"].Value.ToString();
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
den is
  • 11
  • 4
  • You don't really need to assign values manually or use another binding source, just bind the TextBox controls to the same binding source that you have for the DataGridView. Then by moving between rows, TextBox controls will show current record. – Reza Aghaei Feb 08 '22 at 14:42
  • I don't see how your are binding your data to your user class. – ChrisBD Feb 08 '22 at 14:44
  • Anyways, in your code example above, the default value for [DataSourceUpdateMode](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datasourceupdatemode?view=windowsdesktop-6.0&WT.mc_id=DT-MVP-5003235) is `OnValidation`, which means the data source will not be updated until after the validation event of the control happen. You may want to change the data source update mode to `OnPropertyChanged`. – Reza Aghaei Feb 08 '22 at 14:48
  • @ChrisBD i uploaded the binding code – den is Feb 08 '22 at 14:53
  • No problem, then I close as duplicate of the linked post. – Reza Aghaei Feb 08 '22 at 15:07
  • @RezaAghaei i have one more queston i have a combo box that i bind with a code how i make his data source update mode to be on property changed ? this is the code for example Cursor.Current = Cursors.WaitCursor; Author obj = authorCmb.SelectedItem as Author; if (obj != null) { book.AuthorId = obj.AuthorId; } Cursor.Current = Cursors.Default; – den is Feb 08 '22 at 15:29

0 Answers0