Я хочу автоматически обновить оставшиеся дни из базы данных в datagridview в соответствии с текущей датой?
Я хочу обновить столбец Days_left автоматически в соответствии с текущей датой
Что я уже пробовал:
<pre>namespace WindowsFormsApplication3 { public partial class Form1 : Form { SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-F1TCIFJ;Initial Catalog=tempdb;Integrated Security=True"); private int selectedRow; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //// TODO: This line of code loads data into the 'tempdbDataSet1.khaas' table. You can move, or remove it, as needed. //this.khaasTableAdapter.Fill(this.tempdbDataSet1.khaas); alternateColorChange(dataGridViewKhaas); dataGridViewKhaas.MultiSelect = false; } public void alternateColorChange(DataGridView dgv) { dgv.RowsDefaultCellStyle.BackColor = Color.LightBlue; dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.White; } //...................................................KHAAS START....................................................// private void btnKSave_Click(object sender, EventArgs e) { if (txtKVNumber.Text == "") { string myStringVariable1 = string.Empty; MessageBox.Show("Vehicle Number is required"); } else if (cboKVColor.Text == "") { string myStringVariable2 = string.Empty; MessageBox.Show("Select Vehicle Color"); } //For combobox validation we should follow the below code. else if (cboKVBrand.Text == "") { string myStringVariable3 = string.Empty; MessageBox.Show("Select Vehicle Brand"); } else if (cboKVType.Text == "") { string myStringVariable12 = string.Empty; MessageBox.Show("Select Vehicle Type"); } //else if (dateTimePickerKhaas.Value.Date == "") //{ // string myStringVariable4 = string.Empty; // MessageBox.Show("Select Vehicle Color"); //} else if (txtKOName.Text == "") { string myStringVariable5 = string.Empty; MessageBox.Show("Owner Name is required"); } //else if (txtKChassisNum.Text == "") //{ // string myStringVariable6 = string.Empty; // MessageBox.Show("Chassis Number is required"); //} else if (txtKCivilID.Text == "") { string myStringVariable7 = string.Empty; MessageBox.Show("Civil ID is required"); } else if (txtKTelephone.Text == "") { string myStringVariable8 = string.Empty; MessageBox.Show("Telephone Number is required"); } else { con.Open(); String query = "INSERT INTO khaas (VNumber, VColor, VType, VBrand, VEDate, DaysLeft, OName, CivilID, Telephone) VALUES('" + txtKVNumber.Text + "', '" + cboKVColor.Text + "', '" + cboKVType.Text + "', '" + cboKVBrand.Text + "', '" + dateTimePickerKhaas.Value.ToString("MM/dd/yyyy") + "' ,'" + txtKDaysLeft.Text + "','" + txtKOName.Text + "', '" + txtKCivilID.Text + "', '" + txtKTelephone.Text + "' )"; SqlDataAdapter sda = new SqlDataAdapter(query, con); DataTable dt = new DataTable(); sda.Fill(dt); dataGridViewKhaas.DataSource = dt; //sda.SelectCommand.ExecuteNonQuery(); con.Close(); grd_fillKhaas(); MessageBox.Show("Inserted Successfully"); txtKVNumber.Text = ""; cboKVColor.Text = ""; cboKVType.Text = ""; cboKVBrand.Text = ""; dateTimePickerKhaas.Value = DateTime.Now; txtKDaysLeft.Text = ""; txtKOName.Text = ""; //txtKChassisNum.Text = ""; txtKCivilID.Text = ""; txtKTelephone.Text = ""; } } public void grd_fillKhaas() { //con.Open(); String query = "SELECT * FROM khaas"; SqlDataAdapter sda = new SqlDataAdapter(query, con); DataTable dt = new DataTable(); sda.Fill(dt); dataGridViewKhaas.DataSource = dt; //con.Close(); } private void dateTimePickerKhaas_ValueChanged(object sender, EventArgs e) { DateTime from = dateTimePickerKhaas.Value; DateTime to = DateTime.Now; TimeSpan Tspan = from - to; double days = Tspan.TotalDays; txtKDaysLeft.Text = days.ToString("0"); } private void btnKUpdate_Click(object sender, EventArgs e) { if ((txtKVNumber.Text == "") || (cboKVColor.Text == "") || (cboKVType.Text == "") || (cboKVBrand.Text == "") || (txtKDaysLeft.Text == "") || (txtKOName.Text == "") || (txtKCivilID.Text == "") || (txtKTelephone.Text == "")) { MessageBox.Show("Please select a recored to Update"); } else { DialogResult upd = MessageBox.Show("Are you Sure you want to Update?" + txtKVNumber.Text + "", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (upd == DialogResult.Yes) { con.Open(); SqlCommand cmd = new SqlCommand("UPDATE khaas SET VColor='" + cboKVColor.Text + "', VType='" + cboKVType.Text + "', VBrand='" + cboKVBrand.Text + "', VEDate='" + dateTimePickerKhaas.Value.ToString("MM/dd/yyyy") + "', DaysLeft='" + txtKDaysLeft.Text + "', OName='" + txtKOName.Text + "', CivilID='" + txtKCivilID.Text + "', Telephone='" + txtKTelephone.Text + "' WHERE VNumber='" + txtKVNumber.Text + "'", con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Updated Successfully!"); //displaydata(); grd_fillKhaas(); //clear(); txtKVNumber.Text = ""; cboKVColor.Text = ""; cboKVType.Text = ""; cboKVBrand.Text = ""; dateTimePickerKhaas.Value = DateTime.Now; txtKDaysLeft.Text = ""; txtKOName.Text = ""; //txtKChassisNum.Text = ""; txtKCivilID.Text = ""; txtKTelephone.Text = ""; btnKSave.Enabled = true; } else { txtKVNumber.Text = ""; cboKVColor.Text = ""; cboKVType.Text = ""; cboKVBrand.Text = ""; dateTimePickerKhaas.Value = DateTime.Now; txtKDaysLeft.Text = ""; txtKOName.Text = ""; //txtKChassisNum.Text = ""; txtKCivilID.Text = ""; txtKTelephone.Text = ""; btnKSave.Enabled = true; this.Show(); } } } private void dataGridViewKhaas_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) return; selectedRow = e.RowIndex; DataGridViewRow row = dataGridViewKhaas.Rows[selectedRow]; txtKVNumber.Text = row.Cells[0].Value.ToString(); cboKVColor.Text = row.Cells[1].Value.ToString(); cboKVType.Text = row.Cells[2].Value.ToString(); cboKVBrand.Text = row.Cells[3].Value.ToString(); //dateTimePicker1.Value = row.Cells[4].Value.ToString(""); txtKDaysLeft.Text = row.Cells[5].Value.ToString(); txtKOName.Text = row.Cells[6].Value.ToString(); //txtKChassisNum.Text = row.Cells[7].Value.ToString(); txtKCivilID.Text = row.Cells[7].Value.ToString(); txtKTelephone.Text = row.Cells[8].Value.ToString(); btnKSave.Enabled = false; } private void btnKDelete_Click(object sender, EventArgs e) { if ((txtKVNumber.Text == "") || (cboKVColor.Text == "") || (cboKVType.Text == "") || (cboKVBrand.Text == "") || (txtKDaysLeft.Text == "") || (txtKOName.Text == "") || (txtKCivilID.Text == "") || (txtKTelephone.Text == "")) { MessageBox.Show("Please select a recored to Delete"); } else { DialogResult del = MessageBox.Show("Are you Sure you want to Delete?" + txtKVNumber.Text + "", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (del == DialogResult.Yes) { con.Open(); SqlCommand cmd = new SqlCommand("DELETE from khaas WHERE (VNumber='" + txtKVNumber.Text + "')", con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Deleted Successfully!"); //displaydata(); grd_fillKhaas(); //clear(); txtKVNumber.Text = ""; cboKVColor.Text = ""; cboKVType.Text = ""; cboKVBrand.Text = ""; dateTimePickerKhaas.Value = DateTime.Now; txtKDaysLeft.Text = ""; txtKOName.Text = ""; //txtKChassisNum.Text = ""; txtKCivilID.Text = ""; txtKTelephone.Text = ""; btnKSave.Enabled = true; } else { txtKVNumber.Text = ""; cboKVColor.Text = ""; cboKVType.Text = ""; cboKVBrand.Text = ""; dateTimePickerKhaas.Value = DateTime.Now; txtKDaysLeft.Text = ""; txtKOName.Text = ""; //txtKChassisNum.Text = ""; txtKCivilID.Text = ""; txtKTelephone.Text = ""; btnKSave.Enabled = true; this.Show(); } } } private void btnKClear_Click(object sender, EventArgs e) { txtKVNumber.Text = ""; cboKVColor.Text = ""; cboKVType.Text = ""; cboKVBrand.Text = ""; dateTimePickerKhaas.Value = DateTime.Now; txtKDaysLeft.Text = ""; txtKOName.Text = ""; //txtKChassisNum.Text = ""; txtKCivilID.Text = ""; txtKTelephone.Text = ""; btnKSave.Enabled = true; this.Show(); } private void txtKSearch_TextChanged(object sender, EventArgs e) { con.Open(); String query = "SELECT * FROM khaas WHERE VNumber like'" + txtKSearch.Text + "%'"; SqlDataAdapter sda = new SqlDataAdapter(query, con); DataTable dt = new DataTable(); sda.Fill(dt); dataGridViewKhaas.DataSource = dt; con.Close(); } private void btnKPrint_Click(object sender, EventArgs e) { DGVPrinter printer = new DGVPrinter(); printer.Title = "NAQAL KHAAS"; //Header printer.SubTitle = string.Format("Date {0}", DateTime.Now.Date.ToString("MM/dd/yyyy")); printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip; printer.PageNumbers = true; printer.PageNumberInHeader = false; printer.PorportionalColumns = true; printer.HeaderCellAlignment = StringAlignment.Near; printer.Footer = "Future International Transport Company"; //Footer printer.FooterSpacing = 15; printer.PrintDataGridView(dataGridViewKhaas); } //......................................................KHAAS END.......................................................//
Richard MacCutchan
Вы перестраиваете приложение и перезаписываете свою живую базу данных с помощью шаблона?