Use DataGridView to query insert update delete sample
private void buttonQuery_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
InstTOdg(QueryData(textBoxEmpID.Text.Trim(), true), dataGridView1);
}
public string Sqlstr = " Data Source=192.168.1.1;Initial Catalog=CIM;User ID=admin;Password=admin;Persist Security Info=True";
private DataTable QueryData(string Employee, bool ck)
{
DataTable Dt = new DataTable();
using (SqlConnection SqlConn = new SqlConnection(Sqlstr))
{
SqlConn.Open();
if (ck)
{
Employee = @" and EmployeeNo='" + EmployeeNo + "' ";
}
SqlCommand SqlComm = new SqlCommand("select * from dailyreport where ([WorkDate] between '" + dateTimePicker1.Value.ToString("yyyy/MM/dd") + " 00:00:01' and '" + dateTimePicker1.Value.ToString("yyyy/MM/dd") + " 23:59:59') " + Employee + " order by WorkDate,employeeno ", SqlConn);
Dt.Load(SqlComm.ExecuteReader());
}
return Dt;
}
private void InstTOdg(DataTable Dtlist, DataGridView sDataGrid)
{
try
{
if (Dtlist.Rows.Count > 0)
{
DataGridViewRowCollection rows = sDataGrid.Rows;
foreach (DataRow dr in Dtlist.Rows)
{
rows.Add(new Object[] { dr[0], dr[1], Convert.ToDateTime(dr[2]).ToString("yyyy/MM/dd"), dr[3], dr[4], dr[5], dr[6], dr[7] });
}
}
}
catch (Exception ex)
{
MessageBox.Show("InstTOdg()" + ex.Message.ToString());
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
try
{
string SlqStr = "";
dataGridView1.CurrentCell = null;
if (dataGridView1.RowCount > 0)
{
using (SqlConnection SqlConn = new SqlConnection(Sqlstr))
{
SqlConn.Open();
SqlCommand SqlComm;
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (string.IsNullOrEmpty(dr.Cells[0].Value.ToString()))
{
SlqStr = @"INSERT INTO DailyReport
([EmployeeNo],[WorkDate],[ProjectNo],[Description],[UseTime],[Addtime],[Memo])
VALUES
(@EmployeeNo,@WorkDate,@ProjectNo,@Description,@UseTime,@Addtime,@Memo)";
}
else
{
SlqStr = @"UPDATE [CIM].[dbo].[DailyReport]
SET [WorkDate] = @WorkDate,[ProjectNo] = @ProjectNo,[Description] = @Description,[UseTime] = @UseTime,[Addtime] = @Addtime,[Memo]=@Memo
WHERE [EmployeeNo] = @EmployeeNo and autoindex=" + dr.Cells[0].Value.ToString();
}
using (SqlComm = new SqlCommand(SlqStr, SqlConn))
{
DateTime sDt = Convert.ToDateTime(dr.Cells[2].Value.ToString() + " 00:00:00");
SqlComm.Parameters.AddWithValue("EmployeeNo", dr.Cells[1].Value.ToString());
SqlComm.Parameters.AddWithValue("WorkDate", sDt);
SqlComm.Parameters.AddWithValue("ProjectNo", dr.Cells[3].Value.ToString());
SqlComm.Parameters.AddWithValue("Description", dr.Cells[4].Value.ToString());
SqlComm.Parameters.AddWithValue("UseTime", Convert.ToDouble(dr.Cells[5].Value.ToString() == "" ? 0 : Convert.ToDouble(dr.Cells[5].Value.ToString())));
SqlComm.Parameters.AddWithValue("Addtime", Convert.ToDouble(dr.Cells[6].Value.ToString() == "" ? 0 : Convert.ToDouble(dr.Cells[6].Value.ToString())));
SqlComm.Parameters.AddWithValue("Memo", dr.Cells[7].Value.ToString());
SqlComm.ExecuteNonQuery();
}
}
}
}
MessageBox.Show("資料儲存完成~");
button7.PerformClick();
}
catch (Exception ex)
{
MessageBox.Show("btnAllSave_Click()" + ex.Message.ToString());
}
}
private void buttonAddData_Click(object sender, EventArgs e)
{
try
{
if (textBoxEmpID.Text.Trim().Length > 0)
{
DataGridViewRow dgvr = new DataGridViewRow();
dgvr.CreateCells(this.dataGridView1);
dgvr.Cells[0].Value = "";
dgvr.Cells[1].Value = textBoxEmpID.Text.Trim();
dgvr.Cells[2].Value = dateTimePicker1.Value.ToString("yyyy/MM/dd");
dgvr.Cells[3].Value = comboBox1.Text.Trim();
dgvr.Cells[4].Value = "";
dgvr.Cells[5].Value = "";
dgvr.Cells[6].Value = "";
dgvr.Cells[7].Value = "";
dataGridView1.Rows.Add(dgvr);
}
}
catch (Exception ex)
{
MessageBox.Show("btnAdd_Click()" + ex.Message.ToString());
}
}
private void buttonDeleteData_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
int RowIndex = dataGridView1.CurrentRow.Index;
if (!string.IsNullOrEmpty(dataGridView1.Rows[RowIndex].Cells[0].ToString().Trim()))
{
using (SqlConnection SqlConn = new SqlConnection(Sqlstr))
{
SqlConn.Open();
SqlCommand SqlComm = new SqlCommand(@"DELETE FROM DailyReport WHERE EmployeeNo=@EmployeeNo and autoindex=@autoindex", SqlConn);
SqlComm.Parameters.AddWithValue("autoindex", dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString());
SqlComm.Parameters.AddWithValue("EmployeeNo", dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString());
SqlComm.ExecuteNonQuery();
}
}
dataGridView1.Rows.RemoveAt(RowIndex);
}
}
留言
張貼留言