View Full Version : C# MySQL and Datagrids...

06-04-2008, 10:34 PM
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.