如何使datagrid默认滚动条在最上方
各位大神好。一个datagrid,动态增加行数,数据从上到下倒序排列,右边有一个垂直滚动条。可以最小化到托盘。
点击托盘图标恢复的时候滚动条始终停靠在最下方(显示的是最早一条数据),如何实现每次点出界面时默认滚动在最上方,即显示最新数据?
谢谢。
[ 本帖最后由 jasonatchina 于 2013-3-25 08:34 编辑 ]
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; } } } }