DataGridView
이있는 사용자 정의 usercontrol을 만들고이 양식을 양식에 추가하고 있습니다. Form1_Load
이벤트에서 나는 사용자 정의 컨트롤의 생성자를 호출하여 사용자 정의 컨트롤을 초기화하려고합니다. 이 매개 변수가있는 생성자는 인수로 List
을 가지며 목록은 사용자 정의 컨트롤의 DataGridView
에 대해 DataSource
으로 사용됩니다.Usercontrol data gridview가 데이터로로드되지 않습니다.
문제는 다음과 같습니다. 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'로 설정되어 있습니까? – Equalsk
예 사실로 설정 ... –