单击复选框时,C#datagridview添加日期

编程入门 行业动态 更新时间:2024-10-17 00:29:29
本文介绍了单击复选框时,C#datagridview添加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好! i希望在我点击复选框时将当前日期和时间放在单元格Date_Pec中的选定行上。 并自然更新sql数据库。 /> 感谢您的帮助 我尝试了什么:

hi everyone! i want to put current date and time on selected row in cell Date_Pec when i cick the checkedbox. and naturely update the sql database. thank for your help What I have tried:

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 System.Data.SqlClient; namespace WindowsFormsApplication3 { public partial class Form1 : Form { SqlConnection con; SqlDataAdapter da; DataSet ds; SqlCommandBuilder builder; private int i; public Form1() { InitializeComponent(); con = new SqlConnection(CONNECTION.CONNECTION_STRING_1); } private void Form1_Load(object sender, EventArgs e) { // TODO: cette ligne de code charge les données dans la table 'isothermicDataSet.USR_ProdMonitor'. Vous pouvez la déplacer ou la supprimer selon les besoins. this.uSR_ProdMonitorTableAdapter2.Fill(this.isothermicDataSet.USR_ProdMonitor); string sql = "SELECT date_pec,scandate,Poste,NoSeq,Couleur,ProductionLot FROM USR_ProdMonitor WHERE (Poste = N'Parclose')"; ds=new DataSet(); da=new SqlDataAdapter(sql,con); da.Fill(ds); builder=new SqlCommandBuilder(da); dataGridView1.DataSource=ds.Tables[0]; } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { bool selected = !Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["check"].Value); dataGridView1.Rows[1].Selected = true; if (selected) { SqlConnection con = new SqlConnection(CONNECTION.CONNECTION_STRING_1); string SqlCmdText = "INSERT INTO USR_ProdMonitor(Date_Pec)" + DateTime.Now (); int next = this.dataGridView1.CurrentRow.Index + 1; dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red; this.dataGridView1.CurrentCell = this.dataGridView1.Rows[next].Cells[this.dataGridView1.CurrentCell.ColumnIndex]; } else { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen; dataGridView1.Rows[e.RowIndex].Cells["check"].Value = !Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["check"].Value); } { } } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } } }

推荐答案

第一次选择数据源时,添加一个额外的日期列(SELECT GETDATE()AS ...),并使用该字段将您的日期存储在SELECTED ITEM CHANGED事件中的SELECTED ITEM中(而不是与网格本身混合)。 您现在有一个记录(对象)可用于更新数据库。 When you SELECT your "data source" the first time, add an extra "date column" (SELECT GETDATE() AS ...), and use that field to store your "date" in the SELECTED ITEM in the SELECTED ITEM CHANGED event (instead of mucking with the grid itself). You now have a "record" (object) that you can use to update your db.

更多推荐

单击复选框时,C#datagridview添加日期

本文发布于:2023-11-10 16:21:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   复选框   日期   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!