自动填充有界datagrid视图的datetimestamp [英] auto populate datetimestamp of bounded datagrid view

查看:70
本文介绍了自动填充有界datagrid视图的datetimestamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用数据集填充窗口窗体的datagridview.数据集连接到数据库的sql server.在数据库中,我有一张销售表.表格中有datetime字段.我已经使用拖放技术从数据集中制作窗口形式.当用户在datagrid视图中输入新记录时,我想用当前日期自动填充日期字段.
以下是我正在尝试的代码.它不会更新网格视图中最近添加的行中的记录,但会填充我正在处理的记录的日期时间字段.

Hi,
I am using dataset to populate datagridview of my window form. dataset connect to sql server for the database. in the database I have a table of sales. there is datetime field in the table. I have used drag drop technology to make my window form from dataset. I want to auto populate date field with current date as the user enters new record in datagrid view.
following is the code I am trying. it does not update record in the recently added row in grid view yet it populate date time field of the record where I was working on.

private void saleslogTableBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            try
            {
                ((DataRowView)((BindingSource)sender).Current).Row[1] = DateTime.Now.ToString();
            }
            catch (Exception ex)
            {
                System.IO.File.AppendAllText("ErrorLog.txt", ex.Message);
            }
        }



谢谢您的协助.
关于



Thank you for your assistance.
Regards

推荐答案

使用需要的 ^ ],以将当前DateTime提供给新添加的行.

看起来像(伪代码):
Use the DefaultValuesNeeded event[^] to supply the current DateTime to the newly added row.

It would look something like (pseudo code):
private void dataGridView1_DefaultValuesNeeded(object sender,
    System.Windows.Forms.DataGridViewRowEventArgs e)
{
    e.Row.Cells["DateTime Field name"].Value = DateTime.Now;
}


这篇关于自动填充有界datagrid视图的datetimestamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆