Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Jan 2007
    Thanked 0 Times in 0 Posts

    C# MySQL and Datagrids...

    I am having a awful time trying to figure out how to update/insert/delete rows from the datasource.

    My main program is employee clock in/clock out timesheet (which is done) I am now working on the managers applications which I'm allowing them to insert/update/delete the times the employee clocked in or out. I am using the DataGrid to try and accomplish this but having no luck.

    Here is my hours form which I grab the employee ID from the parent form from a combobox.

    namespace MJFinal
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using MySql.Data.MySqlClient;
        using CSConnector;
        using Time;
        public partial class Hours : Form
            public Hours(int intEid)
                // Connect to the database
                DbWrapper myWrapper = new DbWrapper();
                // Populate datagrid with data in the new dataset
                MySqlDataAdapter MyDA = new MySqlDataAdapter();
                DataSet dsDept = new DataSet();
                string sqlDept = "SELECT FROM_UNIXTIME(time_in), FROM_UNIXTIME(time_out) FROM records WHERE eid = " + intEid + " AND FROM_UNIXTIME(time_in) >= (CONCAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(DATE_SUB(CURDATE(), INTERVAL 4 DAY)) DAY), ' 00:00:00'))";
                MyDA.SelectCommand = new MySqlCommand(sqlDept, myWrapper.getSqlConn());
                // Populate a new data table and bind it to the BindingSource.
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dbBindSource.DataSource = table;
                // finally bind the data to the grid
                dataGridView.DataSource = dbBindSource;
                dataGridView.Columns[0].HeaderText = "Time In";
                dataGridView.Columns[0].Width = 150;
                dataGridView.Columns[1].HeaderText = "Time Out";
                dataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                // disconnect from the database
            // close this form
            private void btnClose_Click(object sender, EventArgs e)
    I would like to know how I can update/delete/insert rows using the DataGrid. I have searched and read a lot throughout google/msdn docs with no success and hoping fellow programmers here could help me out. Also I store the times they clock in/out in a unix timestamp and was looking for a way of validating or converting from datetime to a unix timestamp before it inserts/updates the table.

    Thanks for any help!

    Also if I am going about this the wrong way and there is another way to do it I am open for suggestions.
    Last edited by devinkray; 06-04-2008 at 10:38 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts