Emm

Emm

  • NA
  • 1
  • 1.5k

Need Help with a simple data entry form using C#

Mar 11 2014 10:13 AM
Hi Guys,
I am busy teaching myself C#. I have gone the through all the console applications learning syntax and now I want to do a data entry form.
This is what I have done so far. I have a form that has a datagridview on it with textboxes for each item in the grid. I have the grid and the text boxes bound to a SQL database table and loaded as follows:
command = new SqlCommand();
adapter = new SqlDataAdapter();
dataset = new DataSet();
bindingSource = new BindingSource();
command.Connection = connection;
command.CommandText = "SELECT * FROM customer";
adapter.SelectCommand = command;
try
{
adapter.Fill(dataset, "customer");
bindingSource.DataSource = dataset;
bindingSource.DataMember = "customer";
dgvCustomers.DataSource = bindingSource;
//txtCustNo.DataBindings.Add("Text", bindingSource.DataSource, "Customer.CustNo");
//txtName.DataBindings.Add("Text", dataset, "Customer.CustName");
txtCustNo.DataBindings.Add("Text", bindingSource.DataSource, "Customer.CustNo");
txtName.DataBindings.Add("Text", bindingSource.DataSource, "Customer.CustName");
txtEmail.DataBindings.Add("Text", bindingSource.DataSource, "Customer.email");
txtTelNo.DataBindings.Add("Text", bindingSource.DataSource, "Customer.TelNo");
cboGender.DataBindings.Add("Text", bindingSource.DataSource, "Customer.Gender");
cboTerms.DataBindings.Add("Text", bindingSource.DataSource, "Customer.PaymentTerm");

}
catch (SqlException)
{
MessageBox.Show("Error occured while connecting to database.");
Application.Exit();
}
To refresh the data when I select a record on the grid I use a currency Manager as follows.
CurrencyManager cm = (CurrencyManager)this.BindingContext[dataset, "Customer"];
long rowPosition = (long)cm.Position;
cm.Position = dgvCustomers.CurrentRow.Index;

What I would like to know is:
1) How do I use the currency manager of the binding source? As I have read using the CurrencyManager is an old way of doing it.
2) When I click on the add button I clear the textboxes of the data they have in. If I select Cancel but on I use the following but this does not restore the values to the textboxes and instead clears my grid.
bindingSource.CancelEdit();
bindingSource.ResetBindings(false);
 


What do I need to do. 

Perhaps somebody has a simple data entry form with code they can refer me using datasource and bindingsource.

I know I can do this using "wizards" and so on. But I want to do it in code so I can understand how things work



 


Answers (1)