【问题标题】:Unable to store model in Id column. The type was expected to be Int32无法将模型存储在 Id 列中。该类型应为 Int32
【发布时间】:2020-10-17 17:26:44
【问题描述】:

我收到错误:
“System.ArgumentException:”无法将对象类型“WinForm.Model.EntityModel”转换为类型“System.IConvertible”。无法在 Id 列中保存 。类型 Int32 应为“

问题。
如何解决错误?

代码EntityModel.cs

namespace WinForm.Model
{
    class EntityModel
    {
        public int Id { get; set; }
        public string String_Prop { get; set; }
        public int Int_Prop { get; set; }      
        
    }
}

代码Form1.cs

using System.Windows.Forms;
using WinForm.Model;
using System.Data;
 
namespace WinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            DataTable dt = new DataTable();
 
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("String", typeof(string));
            dt.Columns.Add("Int", typeof(int));
            
            dt.Rows.Add(new EntityModel { Id = 1, String_Prop = "String_1", Int_Prop = 11 });
            dt.Rows.Add(new EntityModel { Id = 2, String_Prop = "String_2", Int_Prop = 12 });
            dt.Rows.Add(new EntityModel { Id = 3, String_Prop = "String_3", Int_Prop = 13 });
        }
    }
}

图片1

【问题讨论】:

  • 您正在尝试将EntityModel 对象作为一行添加到DataTable 中。 Rows.Add 需要一个 DataRow 对象或可转换为一个的对象
  • @Flydog57 这样做更好吗? dt.Rows.Add (1," String_1 ", 11);
  • 该代码有效吗?

标签: c# datatable


【解决方案1】:

如果您想使用 EntityModel 类(例如,它是您在程序的其他地方使用的真实模型),您可以提供一种从 @ 的实例创建 DataRow 的方法987654323@

例如,您可以这样做:

public DataRow ToDataRow(DataTable dt)
{
    var row = dt.NewRow();
    row["Id"] = Id;
    row["String"] = String_Prop;
    row["Int"] = Int_Prop;
    return row;
}

或者,如果您非常确定行中的顺序,您可以跳过列名查找并执行以下操作:

public DataRow ToDataRow(DataTable dt)
{
    var row = dt.NewRow();
    row[0] = Id;
    row[1] = String_Prop;
    row[2] = Int_Prop;
    return row;
}

然后您可以在每个EntityModel 实例上调用.ToDataRow(dt)

我希望可能有一种非常清晰的方法来使用 C# deconstructor 方法来完成这项工作。它不像我想象的那么简单,但这应该可行:

首先给EntityModel添加一个解构器

public void Deconstruct(out int id, out string stringProp, out int intProp)
{
    id = Id;
    stringProp = String_Prop;
    intProp = Int_Prop;
}

然后,如果您正在处理一组实体,例如:

var entities = new List<EntityModel>
{
    new EntityModel {Id = 1, String_Prop = "String_1", Int_Prop = 11},
    new EntityModel {Id = 2, String_Prop = "String_2", Int_Prop = 12},
    new EntityModel {Id = 3, String_Prop = "String_3", Int_Prop = 13},
};

您可以编写如下所示的代码:

foreach (var entity in entities)
{
    (int id, string stringProp, int intProp) = entity;
    dt.Rows.Add(id, stringProp, intProp);
}

这很清楚。

【讨论】:

    【解决方案2】:

    要向 DataTable 添加行,必须首先使用NewRow 方法返回一个新的DataRow 对象。

    在您的情况下,示例代码 -

      List<EntityModel> list = new List<EntityModel>();
    
      list.Add(new EntityModel { Id = 1, String_Prop = "String_1", Int_Prop = 11 });
      list.Add(new EntityModel { Id = 2, String_Prop = "String_2", Int_Prop = 12 });
      list.Add(new EntityModel { Id = 3, String_Prop = "String_3", Int_Prop = 13 });
    
      DataTable dt = new DataTable();
    
       dt.Columns.Add("Id", typeof(int));
       dt.Columns.Add("String", typeof(string));
       dt.Columns.Add("Int", typeof(int));
            
       foreach (var item in list)
         {
          var row = dt.NewRow();
    
          row["Id"] = item.Id;
          row["String"] = item.String_Prop;
          row["Int"] = item.Int_Prop;
          dt.Rows.Add(row);
         }
    

    【讨论】:

      【解决方案3】:

      如果不想处理中间List:

      using System.Windows.Forms;
      using WinForm.Model;
      using System.Data;
       
      namespace WinForm
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
       
                  DataTable dt = new DataTable();
       
                  dt.Columns.Add("Id", typeof(int));
                  dt.Columns.Add("String", typeof(string));
                  dt.Columns.Add("Int", typeof(int));
                  
                  dt.Rows.Add(MkRow(1, "String_1", 11));
                  dt.Rows.Add(MkRow(2, "String_2", 12));
                  dt.Rows.Add(MkRow(3, "String_3", 13));
              }
      
              private static DataRow MkRow(DataTable dt, int id, string String_Prop, int Int_Prop)
              {
                  DataRow row = dt.NewRow();
                  row["Id"] = id;
                  row["String_Prop"] = String_Prop;
                  row["Int_Prop"] = Int_Prop;
                  return row;
              }
              
          }
      }
      

      正如 Flydog57 所说,关键是确保您拥有DataRow

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多