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

查看:85
本文介绍了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设置为False和DateTimePicker的值放入电池。默认情况下,直到我需要它DateTimePicker控件的可见性设置为False。我也用这个,以便在常规电池,用户无法输入自定义值,并有权使用的项目列表从设置屏幕组合框控件。这种技术是伟大的作假。我没有代码一应俱全,但它是更少的代码,更易于维护恕我直言。

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 DataGridView控件窗体2.0

The above and following technique was taken from Faking alternative controls within a DataGridView control in Win Forms 2.0

编辑:这是代码 -

Here is the code -

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天全站免登陆