标题:如何使datagrid默认滚动条在最上方
只看楼主
jasonatchina
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-3-24
 问题点数:0 回复次数:10 
如何使datagrid默认滚动条在最上方
各位大神好。

一个datagrid,动态增加行数,数据从上到下倒序排列,右边有一个垂直滚动条。可以最小化到托盘。

点击托盘图标恢复的时候滚动条始终停靠在最下方(显示的是最早一条数据),如何实现每次点出界面时默认滚动在最上方,即显示最新数据?

谢谢。

[ 本帖最后由 jasonatchina 于 2013-3-25 08:34 编辑 ]
搜索更多相关主题的帖子: 托盘 如何 动态 
2013-03-24 20:39
jasonatchina
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-3-24
得分:0 


我试了DataGridView.FirstDisplayedScrollingRowIndex = 0仍然不起作用。最早数据行(下面第一行)第一列的序号0单元格默认处于选择状态。
2013-03-25 12:02
jasonatchina
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-3-24
得分:0 
完整程序代码:

程序代码:
using System;
using System.Collections.Generic;
using using System.Diagnostics;
using System.Drawing;
using System.Timers;
using System.Windows.Forms;
namespace 网页更新检测程序
{
    public class Form1 : Form
    {
        private delegate void _showdata(string a, string b, string c);
        private IContainer components;
        private Button button1;
        private NotifyIcon notifyIcon1;
        private DataGridView dataGridView1;
        private TextBox textBox1;
        private Label label1;
        private DataGridViewTextBoxColumn Column3;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column4;
        private List<task> CacheTask;
        private List<Uri> url = new List<Uri>();
        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(Form1));
            DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
            this.button1 = new Button();
            this.notifyIcon1 = new NotifyIcon(this.components);
            this.dataGridView1 = new DataGridView();
            this.textBox1 = new TextBox();
            this.label1 = new Label();
            this.Column3 = new DataGridViewTextBoxColumn();
            this.Column1 = new DataGridViewTextBoxColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column4 = new DataGridViewTextBoxColumn();
            ((ISupportInitialize)this.dataGridView1).BeginInit();
            base.SuspendLayout();
            this.button1.Location = new Point(229, 8);
            this.button1.Name = "button1";
            this.button1.Size = new Size(127, 39);
            this.button1.TabIndex = 0;
            this.button1.Text = "启动监测";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new EventHandler(this.button1_Click);
            this.notifyIcon1.Icon = (Icon)componentResourceManager.GetObject("notifyIcon1.Icon");
            this.notifyIcon1.Text = "网页更新工具";
            this.notifyIcon1.Visible = true;
            this.notifyIcon1.MouseClick += new MouseEventHandler(this.notifyIcon1_MouseClick);
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            this.dataGridView1.BackgroundColor = SystemColors.ActiveBorder;
            this.dataGridView1.ColumnHeadersVisible = true;
            this.dataGridView1.Columns.AddRange(new DataGridViewColumn[]
            {
                this.Column3,
                this.Column1,
                this.Column2,
                this.Column4    
            });
            dataGridViewCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            
            dataGridViewCellStyle.BackColor = Color.White; 
            dataGridViewCellStyle.Font = new Font("Microsoft Yahei", 9f, FontStyle.Regular, GraphicsUnit.Point, 134);
            dataGridViewCellStyle.ForeColor = Color.Black;
            dataGridViewCellStyle.SelectionBackColor = Color.LightGray; 
            dataGridViewCellStyle.SelectionForeColor = Color.Black; 
            dataGridViewCellStyle.WrapMode = DataGridViewTriState.False;
            this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle;
            this.dataGridView1.GridColor = SystemColors.ActiveBorder;
            this.dataGridView1.Location = new Point(0, 57);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.RowTemplate.Height = 25;
            this.dataGridView1.ScrollBars = ScrollBars.Vertical;
            this.dataGridView1.Size = new Size(760, 446);
            this.dataGridView1.TabIndex = 1;
            this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
            this.textBox1.Location = new Point(147, 17);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new Size(48, 21);
            this.textBox1.TabIndex = 2;
            this.textBox1.Text = "30";
            this.label1.AutoSize = true;
            this.label1.Location = new Point(201, 22);
            this.label1.Name = "label1";
            this.label1.Size = new Size(17, 12);
            this.label1.TabIndex = 3;
            this.label1.Text = "";
            this.Column3.HeaderText = "序号";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.Width = 60;
            this.Column1.HeaderText = "ID";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Width = 80;
            this.Column2.HeaderText = "标题";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 560;
            this.Column4.HeaderText = "简介";        
            this.Column4.Name = "Column4";            
            this.Column4.ReadOnly = true;            
            this.Column4.Width = 70;                
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(368, 503);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.textBox1);
            base.Controls.Add(this.dataGridView1);
            base.Controls.Add(this.button1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
            base.MaximizeBox = true;
            base.Name = "Form1";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "网页更新检测工具";
            base.Load += new EventHandler(this.Form1_Load);
            base.SizeChanged += new EventHandler(this.Form1_SizeChanged);
            ((ISupportInitialize)this.dataGridView1).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }
        public Form1()
        {
            this.InitializeComponent();
            this.Width = 1030;    
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.url.Add(new Uri("http://www."));
            this.CacheTask = my.getHTML(this.url);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Text = "更新检测中.";
            this.button1.Enabled = false;
            new System.Timers.Timer((double)(Convert.ToInt64(this.textBox1.Text) * 1000L))
            {
                Enabled = true,
                AutoReset = true
            }.Elapsed += new ElapsedEventHandler(this.tr_Elapsed);
        }
        private void tr_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.check();
        }
        private void check()
        {
            bool flag = false;
            List<task> hTML = my.getHTML(this.url);
            foreach (task current in hTML)
            {
                if (!this.CacheTask.Contains(current))
                {
                    base.Invoke(new Form1._showdata(this.showdata), new object[]
                    {
                        current.Id,
                        current.Title,
                        current.Offer        
                    });
                    flag = true;
                }
            }
            if (flag)
            {
                this.CacheTask = hTML;
            }
        }
        private void showdata(string a, string b, string c)
        {
            int num = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[num].Cells[0].Value = num;
            this.dataGridView1.Rows[num].Cells[1].Value = a;
            this.dataGridView1.Rows[num].Cells[2].Value = b;
            this.dataGridView1.Rows[num].Cells[3].Value = c;
            this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Descending); 
            this.dataGridView1.FirstDisplayedScrollingRowIndex = 0;                                        //控制默认显示最上方新数据
            this.notifyIcon1.ShowBalloonTip(5000, "网页新内容", b, ToolTipIcon.Info);
            this.notifyIcon1.BalloonTipClicked += new EventHandler(this.notifyIcon1_BalloonTipClicked);
        }
        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {
            if (base.WindowState == FormWindowState.Minimized)
            {
                base.WindowState = FormWindowState.Normal;
                this.notifyIcon1.Visible = false;
                base.Activate();
                base.ShowInTaskbar = true;
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Process.Start("http://www." + this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() + "/"); //单击标题打开URL
        }
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (base.WindowState == FormWindowState.Minimized)
            {
                this.notifyIcon1.Visible = true;
                base.ShowInTaskbar = false;
            }
        }
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (base.WindowState == FormWindowState.Minimized)
            {
                base.WindowState = FormWindowState.Normal;
                this.notifyIcon1.Visible = false;
                base.Activate();
                base.ShowInTaskbar = true;
            }
        }
    }
}
2013-03-25 12:06
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 
//修改notifyIcon1_MouseClick事件代码
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
    if (base.WindowState == FormWindowState.Minimized)
    {
        base.WindowState = FormWindowState.Normal;
        this.notifyIcon1.Visible = false;
        base.Activate();
        base.ShowInTaskbar = true;
        //加上如下两行代码
        this.dataGridView1.FirstDisplayedScrollingRowIndex = 0;
        this.dataGridView1.Rows[0].Cells[0].Selected = true;
    }
}
收到的鲜花
  • jasonatchina2013-03-25 21:34 送鲜花  3朵   附言:非常感谢,试了好多方法都不成功,现在终于 ...
2013-03-25 19:15
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 
也许你用到这两句的地方并不只这一处,其它的地方自己添呗
this.dataGridView1.FirstDisplayedScrollingRowIndex = 0;//控制滚动条
this.dataGridView1.Rows[0].Cells[0].Selected = true;//控制选定单元格
2013-03-25 19:19
jasonatchina
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-3-24
得分:0 
回复 5楼 yhlvht
感谢yhlvht大侠,用了你的代码起作用了!我在private void notifyIcon1_BalloonTipClicked里面也加了这两句。

现在有一个小问题,程序运行时有一个异常提示:指定的参数已超出有效值的范围。参数名:value。
在 System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingRowIndex(Int32 value)
这个有办法能解决吗。

打算散10分给你的,无奈系统不允许,只能先散3分。
2013-03-25 21:39
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 
你看 System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingRowIndex(Int32 value)这个方法里面,传进去的这个参数Int32 value的这个值,用在哪里了
异常提示就是说传进去的这个值大了,比如我用在一个数组上,数组长度只有3,传进去的这个值是3以上的,那我把这个值用作数组下标,就超出有效值范围了.
这并不是一个难题,很容易解决的
收到的鲜花
  • jasonatchina2013-03-26 15:56 送鲜花  3朵   附言:我很赞同
2013-03-25 21:55
jasonatchina
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-3-24
得分:0 
回复 7楼 yhlvht
谢谢,整个程序定义数组的只有一处:int num = this.dataGridView1.Rows.Add();
另一个程序有.Add相关语句:list.Add(new task(match.Groups[1].ToString(), match.Groups[2].ToString(), match.Groups[7].ToString()));
没找到修改数组长度的方法。
暂时先这样用着,异常提示可以选择继续不影响程序使用。
2013-03-26 15:55
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 
不一定是数组啊,只是举例说明而已
set_FirstDisplayedScrollingRowIndex(Int32 value)这个应该是你自己写的方法吧
假如你是这么写
public void set_FirstDisplayedScrollingRowIndex(Int32 value)
{
   this.dataGridView1.FirstDisplayedScrollingRowIndex = value;
}
那么这个value超出可设置的最大行数,就会报超出有效值范围了
2013-03-26 19:35
jasonatchina
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-3-24
得分:0 
回复 9楼 yhlvht
上面的信息是我程序异常窗口中复制出来的,程序中并没有set_FirstDisplayedScrollingRowIndex的定义。
下面是完整的异常信息。

************** 异常文本 **************
System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: value
   在 System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingRowIndex(Int32 value)
   在 网页更新检测.Form1.notifyIcon1_MouseClick(Object sender, MouseEventArgs e) 位置 d:\source\Form1.cs:行号 233
   在 System.Windows.Forms.NotifyIcon.OnMouseClick(MouseEventArgs mea)
   在 System.Windows.Forms.NotifyIcon.WmMouseUp(Message& m, MouseButtons button)
   在 System.Windows.Forms.NotifyIcon.WndProc(Message& msg)
   在 System.Windows.Forms.NotifyIcon.NotifyIconNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** 已加载的程序集 **************
.....

[ 本帖最后由 jasonatchina 于 2013-3-26 20:48 编辑 ]
2013-03-26 20:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-401797-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.182747 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved