C# Winforms DataGridView 时间列 [英] C# Winforms DataGridView Time Column

查看:38
本文介绍了C# Winforms DataGridView 时间列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 DataGridView 中显示时间选择器列?

How to show a Time-picker column in DataGridView?

我不需要选择日期.我只需要时间被选中.

I don't need to pick Date. I only need time to be selected.

推荐答案

实际上,还有比创建自定义 DataGridView 列更好的方法.我目前为我的应用程序做的是当我的 DataGridView 的 TIMESTAMP 列被输入时,我将一个 DateTimePicker 控件直接放置在单元格上.当用户单击单元格外(从而确认他的选择)时,DateTimePicker Visible 设置为 False,并且 DateTimePicker 的值被放入单元格中.默认情况下,DateTimePicker 控件 Visibility 设置为 False,直到我需要它为止.我还将它用于常规单元格上的 ComboBox 控件,在这些单元格中,用户无法输入自定义值,而必须使用设置屏幕中的项目列表.这种技术非常适合伪造.我没有现成的代码,但恕我直言,代码更少且更易于维护.

Actually, there is a better way than creating a custom DataGridView column. What I currently do for my application is when the TIMESTAMP column of my DataGridView is entered, I position a DateTimePicker control directly over the cell. When the user has clicked out of the cell (thus confirming his selection), the DateTimePicker Visible is set to False and the DateTimePicker's value is put into the cell. By default, the DateTimePicker control Visibility is set to False until I need it. I also use this for ComboBox controls on regular cells where the user cannot enter a custom value and has to use the list of items from the Setup Screen. This technique is great for faking it. I don't have the code readily available, but it is less code and easier to maintain IMHO.

以上和以下技术取自 在 Win Forms 2.0 中的 DataGridView 控件中伪造替代控件

这是代码 -

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
            if (selectAllToolStripMenuItem.Checked)
                selectAllToolStripMenuItem.Checked = false;

            if (dtPicker.Visible)
                dtPicker.Visible = false;

            if (e.ColumnIndex >= 0)
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name == "Delete")
                {
                    if (adminIsLoggedIn)
                    {
                        removeRow(e);
                    }
                    else
                    {
                        MessageBox.Show("You must be logged in as an Administrator in order to change the facility configuration.", "Delete Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (dataGridView1.Columns[e.ColumnIndex].Name == "TIMESTAMP")
                {
                    if (adminIsLoggedIn)
                    {
                        setNewCellDate(e);
                    }
                }
                .....
            }
            // ---

}

private void setNewCellDate(DataGridViewCellEventArgs e)
{
            dtPicker.Size = dataGridView1.CurrentCell.Size;
            dtPicker.Top = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top + dataGridView1.Top;
            dtPicker.Left = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left + dataGridView1.Left;
            if (!(object.Equals(Convert.ToString(dataGridView1.CurrentCell.Value), "")))
                dtPicker.Value = Convert.ToDateTime(dataGridView1.CurrentCell.Value);
            dtPicker.Visible = true;
}

这篇关于C# Winforms DataGridView 时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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