DataGridView在绑定了数据并显示出来,这时候每一列的宽度是默认的宽度,现在通过鼠标对其列进行操作,拖动改变了其中某几列的宽度,这时候列的宽度是你拖动之后的。然后DataGridView的DataSource改变了,数据重新显示出来的时候,每一列的宽度又变成了默认的宽度。
问:能不能在DataSource改变之后,让DataGridView的列宽度依然是你通过鼠标拖动之后的列的宽度?

解决方案 »

  1.   

    运行看看吧,就是这么个思路现在只能保存一列的列宽,你把help改成help[]的方式就可以满足你的需求了
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication12
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.button1 = new System.Windows.Forms.Button();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Location = new System.Drawing.Point(28, 30);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(240, 150);
                this.dataGridView1.TabIndex = 0;
                this.dataGridView1.ColumnWidthChanged += new System.Windows.Forms.DataGridViewColumnEventHandler(this.dataGridView1_ColumnWidthChanged);
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(97, 219);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);        }
            Help h;
            private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
            {
                h = new Help();
                h.Width = e.Column.Width;
                h.ColName= e.Column.Name;
            }        private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");
                for (int i = 0; i < 10; i++)
                {
                    DataRow row = dt.NewRow();
                    row["1"] = i.ToString();
                    row["2"] = i.ToString() + "2";
                    dt.Rows.Add(row);
                }
                Bind(dt);
            }        private void Bind(DataTable dt)
            {
                
                this.dataGridView1.DataSource = dt.DefaultView;
                if (h!=null)
                {
                    this.dataGridView1.Columns[h.ColName].Width = h.Width;
                }
            }        private void button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");
                for (int i = 0; i < 10; i++)
                {
                    DataRow row = dt.NewRow();
                    row["1"] = i.ToString();
                    row["2"] = i.ToString() + "3";
                    dt.Rows.Add(row);
                }
                Bind(dt);
            }
        }
        class Help
        {
            private int _width;        public int Width
            {
                get { return _width; }
                set { _width = value; }
            }        private string _colName;        public string ColName
            {
                get { return _colName; }
                set { _colName = value; }
            }    }
    }
      

  2.   

    wzp144650
     非常感谢你的思路,我把MSDN快翻烂了,总想找个属性设置一下就可以搞定,忘记了可以通过辅助类来保存设定。受教了。以后在开发的时候,不能再一棵树上吊死啊!呵呵