简体   繁体   English

DataGrid DataBindings Winforms

[英]DataGrid DataBindings Winforms

DataGrid DataBindings Error DataGrid DataBindings错误

class Test1
{
   public DataTable table1 = new DataTable();
   public BindingSource sr = new BindingSource();
}

class Test2
{

    Test1 ta =new Test1();

    DataTable table1 = new DataTable();
    table1.Columns.Add("Dosage", typeof(int));
    table1.Columns.Add("Drug", typeof(string));
    table1.Columns.Add("Patient", typeof(string));
    table1.Columns.Add("Date", typeof(DateTime));

    table1.Rows.Add(25, "Indocin", "David", DateTime.Now);
    table1.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    table1.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    table1.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    table1.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

    ta.table1 = table1 ;

   datagridview dgv = new datagridview();
   dgv.AutoGenerateColumns = true ;
   dgv.DataBindings.Add("DataSource",ta,"table1");
}

the above code giving me "Cannot bind to the property or column table1 on the DataSource.Parameter name: dataMember." 上面的代码给我“无法绑定到DataSource.Parameter名称:dataMember的属性或列table1”。 . What mistake i ma doing here ,i did not get it .Can any one help me ? 我在这里犯了什么错误,我没有理解。有人可以帮助我吗?

Alternatively, you could always create an object list and bind the list. 或者,您始终可以创建一个对象列表并绑定该列表。 Create a "Patient" class with the parameters you posted (Dosage, DrugName, PatientName, and Time). 使用您发布的参数(剂量,DrugName,PatientName和Time)创建“ Patient”类。 Then create objects and add them to a list and finally set the DGV datasource equal to that list. 然后创建对象并将它们添加到列表中,最后将DGV数据源设置为与该列表相等。 Here's some sample code that could help you: 以下是一些可以帮助您的示例代码:

DataGridView dgvPatient = new DataGridView();      //Create DGV
List<Patient> patientList = new List<Patient>();   //Create list
//Create and populate your object patient
Patient p1 = new Patient(###, "DrugName", "PatientName", DateTime);
patientList.Add(p1);                              //Add to list

dgvPatient.DataSource = typeof(Patient);
dgvPatient.DataSource = patientList;              //Assign to DGV

This is a different way to go about doing it but has worked pretty well for me in the past. 这是执行此操作的另一种方法,但过去对我来说效果很好。 Hope this helps 希望这可以帮助

Use the DataSource property directly to bind your data source like 直接使用DataSource属性来绑定您的数据源,例如

dgv.AutoGenerateColumns = false;
dgv.DataSource = ta.table1;

Well per your posted code the object data source you are passing is wrong, it has to be ta.table1 instead of just ta 根据您发布的代码,您传递的对象数据源是错误的,它必须是ta.table1而不是ta

   dgv.DataBindings.Clear();
   dgv.AutoGenerateColumns = false;
   dgv.DataBindings.Add("DataSource",ta.table1,"DataSource");

Also change the below line as 还将以下行更改为

DataTable table1 = new DataTable("table1");

See MSDN documentation https://msdn.microsoft.com/en-us/library/b6y3aby2(v=vs.110).aspx#Examples for more information 有关更多信息,请参见MSDN文档https://msdn.microsoft.com/zh-cn/library/b6y3aby2(v=vs.110).aspx#Examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM