如何根据时间选择或关注列 [英] How to select or focus on a column based on time of day

查看:79
本文介绍了如何根据时间选择或关注列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我导入了一个电子表格(列标题标记为时间,从上午9:00到上午8:30增加30分钟)到datagridview到winform。我想根据一天中的时间聚焦并突出显示(自动滚动或选择列)列。我怎么能做到这一点?在此先感谢。



我尝试过:



I imported a spreadsheet (column headers are label as time, 30min increment from 9:00AM to 8:30AM) into datagridview onto winform. I want to focus and highlight (auto scrolling or selecting column) a column based on the time of the day. How can I accomplish this? Thanks in advance.

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.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace lowcp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 1000;
            timer1.Tick += new System.EventHandler(timer1_Tick);
            timer1.Start();
            TimeLabel.Text = DateTime.Now.ToLongTimeString();//TIME
            DateLabel.Text = DateTime.Now.ToLongDateString();// DATE

            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\CooksProjection\CProjection.xls" + ";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";

            OleDbCommand command = new OleDbCommand
            ("SELECT * FROM [Display$]", conn);
            DataSet Form1 = new DataSet();
            OleDbDataAdapter adapter = new OleDbDataAdapter(command);
            adapter.Fill(Form1);
                       
            dataGridView1.DataSource = CPP.Tables["Display$"];

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.PaleVioletRed;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
            FreezeBand(dataGridView1.Columns["Prep"]);

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private static void FreezeBand(DataGridViewBand band)
        {
            band.Frozen = true;
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.BackColor = Color.WhiteSmoke;
            band.DefaultCellStyle = style;
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeLabel.Text = DateTime.Now.ToLongTimeString();
            timer1.Start();

        }

        private void TimeLabel_Click(object sender, EventArgs e)
        {

        }

        private void timenow(object sender, ScrollEventArgs e)
        {
            


        }
    }
}

推荐答案

,conn);
DataSet Form1 = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter .Fill(Form1);

dataGridView1.DataSource = CPP.Tables [Display
", conn); DataSet Form1 = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(command); adapter.Fill(Form1); dataGridView1.DataSource = CPP.Tables["Display


];

dataGridView1.RowsDefaultCellStyle。 BackColor = Color.PaleVioletRed;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
FreezeBand(dataGridView1.Columns [Prep]);

}

private void Form1_Load(object sender,EventArgs e)
{

}

private static void FreezeBand(DataGridViewBand band)
{
band .Frozen = true;
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.BackColor = Color.WhiteSmoke;
band.DefaultCellStyle = style;
}
private void label1_Click(object sender,EventArgs e)
{

}

private void timer1_Tick(object sender,EventArgs e )
{
TimeLabel.Text = DateTime.Now.ToLongTimeString();
timer1.Start();

}

private void TimeLabel_Click(object sender,EventArgs e)
{

}

private void timenow(object sender,ScrollEventArgs e)
{



}
}
}
"]; dataGridView1.RowsDefaultCellStyle.BackColor = Color.PaleVioletRed; dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White; dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None; FreezeBand(dataGridView1.Columns["Prep"]); } private void Form1_Load(object sender, EventArgs e) { } private static void FreezeBand(DataGridViewBand band) { band.Frozen = true; DataGridViewCellStyle style = new DataGridViewCellStyle(); style.BackColor = Color.WhiteSmoke; band.DefaultCellStyle = style; } private void label1_Click(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { TimeLabel.Text = DateTime.Now.ToLongTimeString(); timer1.Start(); } private void TimeLabel_Click(object sender, EventArgs e) { } private void timenow(object sender, ScrollEventArgs e) { } } }


这样的东西:

Something like this:
private void ScrollToRow(int rowIndex)
{
    this.dataGridView1.ClearSelection();
    this.dataGridView1.FirstDisplayedScrollingRowIndex = rowIndex;
    this.dataGridView1.Focus();
}

但当然你也需要使用 FirstDisplayedScrollingColumnIndex ......

见例子: c# - 如何在DataGridViews中同步水平滚动 - Stack Overflow [ ^ ]

But of course you will need to use FirstDisplayedScrollingColumnIndex too ...
See example here: c# - How to synchronise horizontal scrolling in DataGridViews - Stack Overflow[^]


这篇关于如何根据时间选择或关注列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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