使用Nhibernate在C#中使用DatagridView绑定数据

时间:2017-04-14 13:18:15

标签: c# nhibernate datagridview

我想将数据添加到Datagridview。我的DNiveau和我的DCycle是:

class DNiveau : IDNiveau
{
    DParams dParams;
    ITransaction tx;
    ISession mySession;

    public Niveau onCreate(Niveau niveau)
    {
         Niveau nivo = null;
         dParams = new DParams();
         mySession = dParams.iSession();

         tx = mySession.BeginTransaction();
         mySession.Save(niveau);
         nivo = mySession.Get<Niveau>(niveau._IdNiveau);
         tx.Commit();

         mySession.Close();
         return null;
    }

    public List<Niveau> getAll()
    {            
         List<Niveau> lNiveau;
         dParams = new DParams();
         mySession = dParams.iSession();            

         tx = mySession.BeginTransaction();
         tx.Commit();

         lNiveau = (List<Niveau>)mySession.CreateCriteria<Niveau>().List<Niveau>();
         mySession.Close();

         return lNiveau;
    }
}

DCycle.cs

class DCycle : IDCycle
{
    protected static readonly ILog _log = LogManager.GetLogger(typeof(Program));

    DParams dParams;
    ITransaction tx;
    ISession mySession;

    public Cycle onCreat(Cycle cycle)
    {
        dParams = new DParams();
        mySession = dParams.iSession();

        tx = mySession.BeginTransaction();
        mySession.Save(cycle);
        tx.Commit();

        mySession.Close();
        return null;
    }

    public List<Cycle> getAll()
    {
         List<Cycle> lClis;
         dParams = new DParams();
         mySession = dParams.iSession();
         tx = mySession.BeginTransaction();
         lClis = (List<Cycle>)mySession.CreateCriteria<Cycle>().List<Cycle>();
         tx.Commit();
         //  mySession.Close();
         return lClis;
    }
}

这是我的对象Niveau和Cycle

public class Niveau
{
    public virtual int _IdNiveau { get; set; }
    public virtual string _designation { get; set; }
    public virtual Cycle _cycle { get; set; }

    public override string ToString()
    {
        return string.Format("_IdNiveau: {0}, _designation: {1}, _cycle:  {2}");
    }
}

public class Cycle
{
    public virtual  int _IdCycle { get; set; }
    public virtual  string _designation { get; set; } 
    public virtual Etablissement etablissement { get; set; }
}

这是我的hibernate映射文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
namespace="OpenSkull.Entites"   assembly="OpenSkull">
<class name="Cycle" table="cycle">
<id name="_IdCycle" column="IDCYCLE">
<generator class="native"/>
</id> 
<property name="_designation" column="DESIGNATION"/>
<many-to-one name="etablissement" class="Etablissement" 
column="IDETABLISSEMENT" cascade="save-update"/>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
namespace="OpenSkull.Entites"
assembly="OpenSkull">
<class name="Niveau" table="niveau">
<id name="_IdNiveau" column="IDNIVEAU">
<generator class="native"/>
</id>
<property name="_designation" column="DESIGNATION"/>
<many-to-one name="_cycle" class="Cycle" column="IDCYCLE" cascade="save-
update"/>
</class>
</hibernate-mapping>

这是我用来添加数据的代码

if (lNivo != null && iCycl != null) {
    Cursor.Current = Cursors.WaitCursor;
    //  dataGridView.DataSource = lNivo;
    niveauBindingSource4.DataSource = lNivo;
    cycleBindingSource3.DataSource = iCycl.getAll();                
    Cursor.Current = Cursors.Default;

当我编辑它时,单击该表单即可,但出现错误:

  

DataGridView中发生以下异常:   System.FormatException:DataGridViewComboBoxCell值无效。

     

要替换此默认对话框,请处理DataError事件。

之后我又收到了另一个错误:

enter image description here

感谢您的帮助,我被封锁了。我是这种语言的新手

0 个答案:

没有答案