将动态ScrollBar转换为动态gridview

时间:2014-08-24 16:01:37

标签: c# gridview dynamic scrollbar

我是初学者到c#的windows表单 我有一个动态网格视图,其中包含一些行,我想创建一个动态滚动条,我希望它创建时,行超过表单大小,以便用户可以向下滚动,我不想要它只有少数几个行..

那是怎么回事?我已经达到了这个代码但是当我向下滚动没有任何变化时,gridview不会向下滚动,猜测某些东西丢失了。

VScrollBar vScroller = new VScrollBar();
vScroller.Height = 200;
vScroller.Width = 30;
vScroller.Dock = DockStyle.Right;
vScroller.Minimum = 0;
vScroller.Maximum = 100;
vScroller.Value = 40;

继承我的完整form1.cs代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DCS
{
public partial class Form1 : Form
{
public Form1()
{

        InitializeComponent();

    }
    Button b1 = new Button();
    Form2 f2 = new Form2();
    Button b2 = new Button();
    Form3 f3 = new Form3();
    VScrollBar vScroller1 = new VScrollBar();


    public partial class Form2 : Form
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code


        private void InitializeComponent()
        {
            DataGridView dg = new DataGridView();

        }
        #endregion

        public Form2()
        {


            this.Padding = new Padding(0, 0, 20, 20);
            this.StartPosition = FormStartPosition.CenterScreen;

        }
    }

    public partial class Form3 : Form
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code


        private void InitializeComponent()
        {
            DataGridView dg = new DataGridView();

        }
        #endregion

        public Form3()
        {


            this.Padding = new Padding(0, 0, 20, 20);
            this.StartPosition = FormStartPosition.CenterScreen;

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (comboBox1.Text == "KM Driven")
        {
            groupBox3.Show();



        }


        else if (comboBox1.Text == "Drivers Total Driving Time")
        {
            groupBox2.Show();

        }
    }



    private void button3_Click(object sender, EventArgs e)
    {

        if (f2.Visible == false)
        {


            vScroller1.Anchor =  ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Right))));
            vScroller1.Dock = DockStyle.Right;
            vScroller1.Minimum = 0;
            vScroller1.Maximum = 100;
            vScroller1.Value = 40;

            f2.Location = new System.Drawing.Point(102, 395);
            f2.Size = new System.Drawing.Size(401, 348);
            f2.Name = "Form2";
            f2.Text = "KM Driven";
            f2.Show();
            f2.MdiParent = this;
            f2.ControlBox = false;
            f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            f2.MaximizeBox = false;
            f2.ShowIcon = false;
            f2.ResumeLayout(false);
            f2.Controls.Add(vScroller1);
            f2.Controls.Add(dataGridView1);


            f2.Refresh();


            this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.Size = new System.Drawing.Size(401, 348);
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.DataSource = this.vlmApplicationDataEvents252BindingSource;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.TabIndex = 0;
            this.vlmApplicationDataEvents252BindingSource.DataSource = this.dataSet1.vlm_Application_Data_Events_252;
            this.dataGridView1.Show();





            SqlCommand myCommand = new SqlCommand();


            myCommand.Parameters.AddWithValue("@startdate", dateTimePicker1.Value);
            myCommand.Parameters.AddWithValue("@enddate", dateTimePicker2.Value);
            this.vlm_Application_Data_Events_252TableAdapter.Fill(this.dataSet1.vlm_Application_Data_Events_252, dateTimePicker1.Value, dateTimePicker2.Value);

            this.dataGridView1.Refresh();



            b1.Parent = groupBox3;
            b1.Location = new System.Drawing.Point(698, 59);
            b1.Size = new System.Drawing.Size(98, 33);
            b1.Name = "New Button";
            b1.Text = "Close Table";
            b1.Show();
            button4.Enabled = false;

            this.dataSet1 = new DCS.DataSet1();



        }



    }

    DataGridView datagridview1 = new DataGridView();
    private void vScroller1_Scroll(object sender, ScrollEventArgs e)
    {
        datagridview1.FirstDisplayedScrollingRowIndex = e.NewValue;
    }



    private void b1_Click(object sender, EventArgs e)
    {
        if (f2.Visible == true)
        {
            f2.Hide();
            b1.Hide();
            button4.Enabled = true;
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button4_Click(object sender, EventArgs e)
    {
        groupBox3.Hide();
    }

    private void button5_Click(object sender, EventArgs e)
    {
        groupBox2.Hide();
    }

    private void button2_Click(object sender, EventArgs e)
    {

        if (f3.Visible == false)
        {

            f3.Location = new System.Drawing.Point(102, 395);
            f3.Size = new System.Drawing.Size(401, 348);
            f3.Name = "From3";
            f3.Text = "Drivers Total Driving Time";
            f3.Show();
            f3.MdiParent = this;
            f3.ControlBox = false;
            f3.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            f3.MaximizeBox = false;
            f3.ShowIcon = false;
            f3.ResumeLayout(false);
            f3.Controls.Add(dataGridView2);


            this.dataGridView2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridView2.AllowUserToAddRows = true;
            this.dataGridView2.AllowUserToDeleteRows = false;
            this.dataGridView2.AutoGenerateColumns = true;
            this.dataGridView2.Size = new System.Drawing.Size(401, 348);
            this.dataGridView2.Columns.Clear();
            this.dataGridView2.Location = new System.Drawing.Point(0, 0);
            this.dataGridView2.DataSource = this.vlmApplicationDataEvents252BindingSource2;
            this.dataGridView2.Name = "dataGridView2";
            this.dataGridView2.ReadOnly = true;
            this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView2.TabIndex = 0;
            this.vlmApplicationDataEvents252BindingSource2.DataSource = this.dataSet2.vlm_Application_Data_Events_252;
            this.dataGridView2.Show();

            b2.Parent = groupBox2;
            b2.Location = new System.Drawing.Point(698, 57);
            b2.Size = new System.Drawing.Size(98, 33);
            b2.Name = "New Button1";
            b2.Text = "Close Table";
            b2.Show();
            button5.Enabled = false;

            SqlCommand myCommand = new SqlCommand();

            myCommand.Parameters.AddWithValue("@startdate2", dateTimePicker3.Value);
            myCommand.Parameters.AddWithValue("@enddate2", dateTimePicker4.Value);
            this.vlm_Application_Data_Events_252TableAdapter2.Fill(this.dataSet2.vlm_Application_Data_Events_252, dateTimePicker3.Value, dateTimePicker4.Value);

            this.dataGridView2.Refresh();




        }
    }
        private void b2_Click(object sender, EventArgs e)
        {
            if (f3.Visible == true)
            {
                f3.Hide();
                b2.Hide();
                button5.Enabled = true;
            }
        }
    }

partial class Form2
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    public void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }

    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.button1 = new System.Windows.Forms.Button();
        this.vlmApplicationDataEvents252BindingSource = new System.Windows.Forms.BindingSource(this.components);
        this.dataSet1 = new DCS.DataSet1();
        this.vlm_Application_Data_Events_252TableAdapter = new DCS.DataSet1TableAdapters.vlm_Application_Data_Events_252TableAdapter();
        this.vehiclePlateNumberDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.vehicleDescriptionDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.distanceDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.vlmApplicationDataEvents252BindingSource)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
        VScrollBar vScroller1 = new VScrollBar();
        vScroller1.Scroll += new ScrollEventHandler(vScroller_Scroll);
        vScroller1.Scroll += new System.Windows.Forms.ScrollEventHandler(vScroller_Scroll);
        this.dataGridView1.Controls.Add(vScroller1);



        // 
        //
        //
        //
        //


        // 
        // vlmApplicationDataEvents252BindingSource
        // 
        this.vlmApplicationDataEvents252BindingSource.DataMember = "vlm_Application_Data_Events_252";
        this.vlmApplicationDataEvents252BindingSource.DataSource = this.dataSet1;
        // 
        //
        //gridview

        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.vehiclePlateNumberDataGridViewTextBoxColumn,
        this.vehicleDescriptionDataGridViewTextBoxColumn,
        this.distanceDataGridViewTextBoxColumn});
        this.dataGridView1.AllowUserToDeleteRows = false;
        this.dataGridView1.AllowUserToAddRows = false;
        this.dataGridView1.AutoGenerateColumns = true;
        this.dataGridView1.DataSource = this.vlmApplicationDataEvents252BindingSource;
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.ReadOnly = true;
        this.dataGridView1.Size = new System.Drawing.Size(401, 348);
        this.dataGridView1.TabIndex = 0;
        this.vlmApplicationDataEvents252BindingSource.DataSource = this.dataSet1.vlm_Application_Data_Events_252;
        // dataSet1
        // 
        this.dataSet1.DataSetName = "DataSet1";
        this.dataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
        // 
        // vlm_Application_Data_Events_252TableAdapter
        // 
        this.vlm_Application_Data_Events_252TableAdapter.ClearBeforeFill = true;
        // 
        // vehiclePlateNumberDataGridViewTextBoxColumn
        // 
        this.vehiclePlateNumberDataGridViewTextBoxColumn.DataPropertyName = "VehiclePlateNumber";
        this.vehiclePlateNumberDataGridViewTextBoxColumn.HeaderText = "VehiclePlateNumber";
        this.vehiclePlateNumberDataGridViewTextBoxColumn.Name = "vehiclePlateNumberDataGridViewTextBoxColumn";
        this.vehiclePlateNumberDataGridViewTextBoxColumn.ReadOnly = true;
        // 
        // vehicleDescriptionDataGridViewTextBoxColumn
        // 
        this.vehicleDescriptionDataGridViewTextBoxColumn.DataPropertyName = "VehicleDescription";
        this.vehicleDescriptionDataGridViewTextBoxColumn.HeaderText = "VehicleDescription";
        this.vehicleDescriptionDataGridViewTextBoxColumn.Name = "vehicleDescriptionDataGridViewTextBoxColumn";
        this.vehicleDescriptionDataGridViewTextBoxColumn.ReadOnly = true;
        // 
        // distanceDataGridViewTextBoxColumn
        // 
        this.distanceDataGridViewTextBoxColumn.DataPropertyName = "Distance";
        this.distanceDataGridViewTextBoxColumn.HeaderText = "Distance";
        this.distanceDataGridViewTextBoxColumn.Name = "distanceDataGridViewTextBoxColumn";
        this.distanceDataGridViewTextBoxColumn.ReadOnly = true;
        // 
        // Form2
        // 
        this.Controls.Add(dataGridView1);
        ((System.ComponentModel.ISupportInitialize)(this.vlmApplicationDataEvents252BindingSource)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
        this.Load += new System.EventHandler(this.Form2_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.vlmApplicationDataEvents252BindingSource)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();



    }

    #endregion
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.DataGridViewTextBoxColumn vehiclePlateNumberDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn vehicleDescriptionDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn distanceDataGridViewTextBoxColumn;
    private DataSet1 dataSet1;
    private DataSet1TableAdapters.vlm_Application_Data_Events_252TableAdapter vlm_Application_Data_Events_252TableAdapter;
    private System.Windows.Forms.BindingSource vlmApplicationDataEvents252BindingSource;
    private System.Windows.Forms.VScrollBar vScrollBar1;


    public EventHandler Form2_Load { get; set; }

    public ScrollEventHandler vScroller_Scroll { get; set; }
}
public partial class Form2 : Form
{
    public Form2()
    {

        InitializeComponent();

    }
 }

partial class Form3
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    public void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }

    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.dataGridView2 = new System.Windows.Forms.DataGridView();
        this.vlmApplicationDataEvents252BindingSource2 = new System.Windows.Forms.BindingSource(this.components);
        this.dataSet2 = new DCS.DataSet2();
        this.vlm_Application_Data_Events_252TableAdapter = new DCS.DataSet2TableAdapters.vlm_Application_Data_Events_252TableAdapter();
        this.EventDurationDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.DriverNameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.vlmApplicationDataEvents252BindingSource2)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet2)).BeginInit();


        // 
        // button1
        //
        //
        //


        // 
        // vlmApplicationDataEvents252BindingSource2
        // 
        this.vlmApplicationDataEvents252BindingSource2.DataMember = "vlm_Application_Data_Events_252";
        this.vlmApplicationDataEvents252BindingSource2.DataSource = this.dataSet2;
        // 

        //gridview2


        this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.EventDurationDataGridViewTextBoxColumn,
        this.DriverNameDataGridViewTextBoxColumn});
        this.dataGridView2.AllowUserToDeleteRows = false;
        this.dataGridView2.AllowUserToAddRows = false;
        this.dataGridView2.AutoGenerateColumns = true;
        this.dataGridView2.DataSource = this.vlmApplicationDataEvents252BindingSource2;
        this.dataGridView2.Location = new System.Drawing.Point(0, 0);
        this.dataGridView2.Name = "dataGridView2";
        this.dataGridView2.ReadOnly = true;
        this.dataGridView2.Size = new System.Drawing.Size(401, 348);
        this.dataGridView2.TabIndex = 0;
        this.vlmApplicationDataEvents252BindingSource2.DataSource = this.dataSet2.vlm_Application_Data_Events_252;
        // dataSet2
        // 
        this.dataSet2.DataSetName = "DataSet2";
        this.dataSet2.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
        // 
        // vlm_Application_Data_Events_252TableAdapter
        // 
        this.vlm_Application_Data_Events_252TableAdapter.ClearBeforeFill = true;
        // 
        // 
        // EventDurationDataGridViewTextBoxColumn
        // 
        this.EventDurationDataGridViewTextBoxColumn.DataPropertyName = "EventDuration";
        this.EventDurationDataGridViewTextBoxColumn.HeaderText = "EventDuration";
        this.EventDurationDataGridViewTextBoxColumn.Name = "EventDurationDataGridViewTextBoxColumn";
        this.EventDurationDataGridViewTextBoxColumn.ReadOnly = true;
        // 
        // DriverNameDataGridViewTextBoxColumn
        // 
        this.DriverNameDataGridViewTextBoxColumn.DataPropertyName = "drivername";
        this.DriverNameDataGridViewTextBoxColumn.HeaderText = "DriverName";
        this.DriverNameDataGridViewTextBoxColumn.Name = "DriverNameDataGridViewTextBoxColumn";
        this.DriverNameDataGridViewTextBoxColumn.ReadOnly = true;
        // 
        // Form3
        // 
        this.Controls.Add(dataGridView2);
        ((System.ComponentModel.ISupportInitialize)(this.vlmApplicationDataEvents252BindingSource2)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet2)).EndInit();


    }

    #endregion
    private System.Windows.Forms.DataGridView dataGridView2;
    private System.Windows.Forms.DataGridViewTextBoxColumn DriverNameDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn EventDurationDataGridViewTextBoxColumn;
    private DataSet2 dataSet2;
    private DataSet2TableAdapters.vlm_Application_Data_Events_252TableAdapter vlm_Application_Data_Events_252TableAdapter;
    private System.Windows.Forms.BindingSource vlmApplicationDataEvents252BindingSource2;


}
public partial class Form3 : Form
{
    public Form3()
    {

        InitializeComponent();

}
}

}

1 个答案:

答案 0 :(得分:0)

您需要将VScrollBar控件添加到DataGridView控件集合并挂钩滚动事件。

vScroller.Scroll += new ScrollEventHandler(vScroller_Scroll);
this.dataGridView.Controls.Add(vScroller);

本文将详细介绍您需要创建的事件:http://www.codeproject.com/Articles/20979/Paging-in-DataGridView-using-VScrollbar

编辑: 事件监听器可能如下所示:

private void vScroller_Scroll(object sender, ScrollEventArgs e)
{
    dataGridView.FirstDisplayedScrollingRowIndex = e.NewValue;
}

这绝对不是最好的解决方案,但VerticalScrollingOffset是只读的,我失败了,试图通过反射绕过它。

另请注意,您的最小值为0,最大值为datagridview中的行数(或更少)。

EDIT2:这是测试的代码。

using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private DataGridView x = new DataGridView();

        public Form1()
        {
            InitializeComponent();
            Controls.Add(x);
            x.Columns.Add("name", "header");
            for (int i = 0; i < 100; i++)
            {
                x.Rows.Add(i.ToString());
            }
            VScrollBar sc = new VScrollBar();
            sc.Dock = DockStyle.Right;
            sc.Minimum = 0;
            sc.Maximum = x.Rows.Count;
            sc.Scroll += sc_Scroll;
            x.Controls.Add(sc);
        }

        private void sc_Scroll(object sender, ScrollEventArgs e)
        {
            x.FirstDisplayedScrollingRowIndex = e.NewValue;
        }
    }
}