首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个dataGridView应用程序的通用样式(一般设置)

多个dataGridView应用程序的通用样式(一般设置)
EN

Stack Overflow用户
提问于 2020-10-27 10:28:12
回答 1查看 67关注 0票数 0

这个应用程序有多个dataGridViews。

示例.

有: dataGridView-3段(dataGridView1,dataGridView2,dataGridView3)。

如何做:

  • dataGridView1-2 -有settings-1;
  • dataGridView3 -有设置-2.

换句话说,我设置了哪个"dataGridView“使用哪些设置。

问题.

如何为多个dataGridView应用程序创建通用样式?

我创造了一个解决方案。结果:我得到一个空的UserControl

图片-1

代码MyGrid

代码语言:javascript
复制
class MyGrid: DataGridView
    {
        public MyGrid ()
        {
             // this = new System.Windows.Forms.DataGridView ();
            // ((System.ComponentModel.ISupportInitialize) (this)). BeginInit ();
            //this.SuspendLayout ();
            //
            // dgw
            //
            this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            // this.dgv.Location = new System.Drawing.Point (20, 64);
            this.Name = "dgw";
            this.Size = new System.Drawing.Size (419, 173);
            // this.dgv.TabIndex = 0;
 
 
 
 
            // this.Columns ["Name"]. Width = 70; // Change the width. Field - "Name"
            // this.Columns ["Property"]. Width = 70; // Change the width. Field - "Property"
            // this.Columns [0] .Width = 70; // Change the width. Field - "Name"
            // this.Columns [1] .Width = 70; // Change the width. Field - "Property"
 
            this.RowHeadersWidth = 20; // Width "Title of rows"
            this.AllowUserToAddRows = false; // `false` - disable the line for adding a" new record "
            this.ReadOnly = true; // Editing
 
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; // Auto-fit width.
 
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // Select the entire line
            this.MultiSelect = false; // Multi-select
            this.Columns.Cast <DataGridViewColumn> () .ToList (). ForEach (f => f.SortMode = DataGridViewColumnSortMode.NotSortable); // Disable sorting
 
            // this.Size.Width = 400;
            // this.Size.Height = 170;
            
        }
 
    }

代码UserControl1

代码语言:javascript
复制
public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
 
            DataTable dt = new DataTable();
            
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Property", typeof(string));            
 
            dt.Rows.Add("Name_1", "Property_1");
            dt.Rows.Add("Name_2", "Property_2");
            dt.Rows.Add("Name_3", "Property_3");
                 
            //
            MyGrid myGrid = new MyGrid();                
            myGrid.Location = new Point(20, 64);
            myGrid.TabIndex = 0;
 
            myGrid.DataSource = dt;
        }
    }

代码MainForm

代码语言:javascript
复制
namespace WinForm
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            var userControl1 = new UserControl1();
            panel1.Controls.Clear();            
            panel1.Controls.Add(userControl1);           
 
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            var userControl2 = new UserControl2();
            panel1.Controls.Clear();
            panel1.Controls.Add(userControl2);
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-27 11:03:32

我认为最简单的方法是创建从DataGridView派生的类。将它们放入DLL中,您就可以从需要这些类的任何应用程序中访问它们。

代码语言:javascript
复制
public class DataGridView1 : System.Windows.Forms.DataGridView
{
    public DataGridView1() : base()
    {
        // Add columns, and set all styles specific for this kind of DataGridView
        this.BackColor = Colors.LightGreen;
        DataGridViewColumn columnId = new DataGridViewColumn()
        {
            Name = "columnId",
            Width = 20,
            ...
        this.Columns.Add(columnId);
        ...
    }
}

public class DataGridView1 : System.Windows.Forms.DataGridView
{
    public DataGridView2() : base()
    {
        // Add columns, and set all styles specific for this kind of DataGridView
        this.BackColor = Colors.Cyan;
        DataGridViewColumn columnId = new DataGridViewColumn()
        {
            Name = "columnId",
            Width = 50,
            ...
        this.Columns.Add(columnId);
        ...
    }
}

用法:

代码语言:javascript
复制
class MainForm : Form
{
    private readonly DataGridView1 dataGridView = new DataGridView1();
    ...
}
class OtherForm : Form
{
    private readonly DataGridView2 dataGridView = new DataGridView2();
    ...
}

简单的糖果!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64552476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档