Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Shift : Form
- {
- private SqlConnection conn;
- private SqlDataAdapter daShift;
- private DataSet dsShift;
- private DataGrid dgShift;
- private const string tableName = "Shift";
- public Shift()
- {
- InitializeComponent();
- Initdata();
- //Create DataGridView
- dgShift = new DataGrid();
- dgShift.Location = new Point(5, 5);
- dgShift.Size = new Size(
- this.ClientRectangle.Size.Width - 10,
- this.ClientRectangle.Height - 50);
- dgShift.DataSource = dsShift;
- dgShift.DataMember = tableName;
- dgShift.RowHeadersVisible = false;
- //dgShift.Dock = DockStyle.Top;
- dgShift.Anchor = AnchorStyles.Top;
- // create update button
- Button btnUpdate = new Button();
- btnUpdate.Text = "Add Shift";
- btnUpdate.Location = new Point(
- this.ClientRectangle.Width / 2 - btnUpdate.Width / 2,
- this.ClientRectangle.Height - (btnUpdate.Height + 10));
- btnUpdate.Click += new EventHandler(btnUpdateClicked);
- btnUpdate.Anchor = AnchorStyles.Bottom;
- // make sure controls appear on form
- Controls.AddRange(new Control[] { dgShift, btnUpdate });
- }
- // Update button was clicked
- public void btnUpdateClicked(object sender, EventArgs e)
- {
- try
- {
- // write changes back to DataBase
- daShift.Update(dsShift, tableName);
- }
- catch (Exception)
- {
- MessageBox.Show("Error:Are there empty fields or inncorrect formats?","Error");
- return;
- }
- }
- // set up ADO.NET objects
- public void Initdata()
- {
- // instantiate the connection
- conn = new SqlConnection("Server=ROSS-PC; Database=ShiftSummary; Trusted_Connection = True");
- // 1. instantiate a new DataSet
- dsShift = new DataSet();
- // 2. init SqlDataAdapter with select command and connection
- daShift = new SqlDataAdapter(
- "select [ShiftDate],[ShiftStart],[ShiftEnd],[JobID], [RateType] from Shift", conn);
- // 3. fill in insert, update, and delete commands
- SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daShift);
- // 4. fill the dataset
- daShift.Fill(dsShift, tableName);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement