本文介绍了使用DataContext的数据绑定组合框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQLServer中有2个表的学生和部门

部门:具有以下字段
1. DepartmentID(PrimaryKey,AllowNull = false)
2. DepartmentName(字段,AllowNull = False)

学生:具有以下字段
1. StudentID [PrimaryKey,AllowNull = false]
2. StudentName [Field,AllowNull = False]
3. Department_ID [外键(部门表),AllowNull = False]

VS.Net Projects的addnewItem 中,我添加了LINQToSQl类,然后将其拖放到这2个表中,并创建了一个数据上下文.

1.现在我有了一个datagridview:
->数据源设置为学生表

2.我在datagridview( AutogenerateColumn = false )
中添加了2列
1.学生姓名(TextBoxColumn)
-> datapropertyname是Student表的StudentName字段

2.部门(组合框列)
->数据源是部门"表
-> ValueMember:"DepartmentID"(DepartmentTable的 PrimaryKey )
-> DisplayMember:部门名称"(部门表中的字段)
-> Datapropertyname是学生表的Department_ID字段

代码:

I have 2 tables student and department, in SQLServer

Departmet: with the Following fields
1. DepartmentID (PrimaryKey, AllowNull=false)
2. DepartmentName (Field, AllowNull=False)

Student: with the Following fields
1. StudentID [PrimaryKey, AllowNull=false]
2. StudentName [Field, AllowNull=False]
3. Department_ID [ForeignKey(Department Table), AllowNull=False]

In VS.Net Projects''s addnewItem i added LINQToSQl class, then drag n drop these 2 tables and It created a datacontext.

1. Now i have a datagridview:
-> datasource is set to student table

2. I added 2 columns in the datagridview(AutogenerateColumn= false)

1. Student Name(TextBoxColumn)
-> datapropertyname is the StudentName Field of the Student table

2. Department(Comboboxcolumn)
-> Datasource is the Department table
-> ValueMember : "DepartmentID"( PrimaryKey of the DepartmentTable)
-> DisplayMember : "DepartmentName"(Field in the Department table)
-> Datapropertyname is the Department_ID field of the Student Table

Code:

DataClasses1DataContext dc = new DataClasses1DataContext();
private void Form1_Load(object sender, EventArgs e)
        {
             dc.Log = Console.Out;

             BindingSource bsDepartment = new BindingSource();
             bsDepartment.DataSource = dc.GetTable<DepartmentTable>().GetNewBindingList();

             BindingSource bsStudent = new BindingSource();
             bsStudent.DataSource = dc.GetTable<StudentTable>().GetNewBindingList();

             dgvStaffMaster.AutoGenerateColumns = false;
             dgvStaffMaster.DataSource = bsStudent;

             DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
             colName.Name = "StudentName";
             colName.HeaderText = "Student Name";
             colName.DataPropertyName = "StudentName";
             dgvStaffMaster.Columns.Add(colName);

            DataGridViewComboBoxColumn coldept = new DataGridViewComboBoxColumn();
            coldept.Name = "Department_ID";
            coldept.HeaderText = "Department";
            coldept.DataSource = bsDepartment;
            coldept.ValueMember = "DepartmentID";
            coldept.DisplayMember = "DepartmentName";
            coldept.DataPropertyName = "Department_ID";
            dgvStaffMaster.Columns.Add(coldept);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            dc.SubmitChanges();  //I use this to to save the changes
        }



问题:
如果我编辑 datagridview的内容,一切都很好,
但是如果我尝试在Datagridiview的 NewRow 中添加 newItem ,则会抛出 InvalidComboboxCellValue 异常.
但是,如果我将学生表的 Department_ID 字段的 AllowNull 属性设置为 true ,那么所有事情都可以正常工作

3. Department_ID [ForeignKey(部门表), AllowNull = True ]
但是我需要它是 false ,请帮助!

过去一个星期我伤了脑筋!

请帮助我,



PROBLEM:
Every thing works fine if i Edit the Contents of the datagridview,
but if i try to add a newItem, in the NewRow of the Datagridiview, InvalidComboboxCellValue exception is thrown.
But if i set the AllowNull Property of the Department_ID field of the student table to true, Every thing Works Fine!

3. Department_ID [ForeignKey(Department Table), AllowNull=True]
But i need the it to be false, Please Help!

I''m breaking my head for the past one week!

Please Help Me,

推荐答案


这篇关于使用DataContext的数据绑定组合框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:54