【问题标题】:Usercontrol data gridview not loading with data用户控件数据网格视图未加载数据
【发布时间】:2019-11-29 01:38:43
【问题描述】:

我正在创建一个带有DataGridView 的自定义用户控件,并且我正在将此用户控件添加到我的表单中。在Form1_Load 事件中,我通过调用用户控件的构造函数来初始化用户控件。它是一个参数化的构造函数,它有一个List 作为参数,并且该列表被用作用户控件中DataGridView 的DataSource。

问题是:DataGridView 没有加载数据。

任何人都可以弄清楚。

表单加载事件中的代码是,

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

namespace usercontrol
{
    public partial class Form1 : Form
    {
         public Form1()
         {
            InitializeComponent();
         }

         private void Form1_Load(object sender, EventArgs e)
         {
             List<Car> cars = new List<Car>();
             cars.Add(new Car("Ford", "Mustang", "1967"));
             cars.Add(new Car("Shelby AC", "Cobra", "1965"));
             cars.Add(new Car("Chevrolet", "Corvette Sting Ray", "1965"));                 

             ucSample uc = new ucSample(cars);
         }            
    }

    public class Car
    {
        private string company;
        private string color;
        private string year;
        public Car(string com,string col,string yea)
        {
            this.Company = com;
            this.Color = col;
            this.Year = yea;
        }

        public string Company { get; set; }
        public string Color { get; set; }
        public string Year { get; set; }   
    }       
}

用户控件中的代码是

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

namespace usercontrol
{
    public partial class ucSample : UserControl
    {
        public ucSample()
        {
            InitializeComponent();
        }

        public ucSample(List<Car> listString)
        {
            InitializeComponent();
            DataSource = listString;
        }

        public object DataSource
        {
            get { return dgvSample.DataSource; }
            set { dgvSample.DataSource = value; }
        }
    }
}

【问题讨论】:

  • DataGridView 是否将属性AutoGenerateColumns 设置为true?
  • 是的,我将其设置为 true...

标签: c# winforms datagridview user-controls


【解决方案1】:

您的问题是您在一个额外的类中创建了一个自定义控件,但从未将该控件添加到要显示的表单中

一条简单的线将解决您的问题:

this.Controls.Add(uc);

把它放在构造函数中。这将确保您的自定义控件被添加到 Form 以进行显示

编辑:

当然也有手动方式:这里有截图How do I add my new User Control to the Toolbox or a new Winform?的答案

这是另一个:add user control to a form

【讨论】:

  • 谢谢。我想从工具箱和droppng拖动到表单时,它会被添加到表单中。因此,每当我想使用自定义用户控件时,我都必须使用相同的方法,或者有其他方法可以做到这一点
  • @VipinJacob 我放了 2 个链接来说明如何操作,请检查我的编辑
【解决方案2】:

datagridview 受用户控制。 这就是为什么您的 Datagrid 没有在表单中加载用户控件的原因。

  1. 转到 userControl.cs

  2. 在构造函数中粘贴以下代码

    "this.studentTableAdapter.Fill(this.universityDataSet.Student);"

在我的例子中,数据集是大学数据集,表是学生

【讨论】:

    猜你喜欢
    • 2013-08-23
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多