Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#GUI可编辑DataGridView_C#_.net_Winforms_Entity Framework_Datagridview - Fatal编程技术网

C#GUI可编辑DataGridView

C#GUI可编辑DataGridView,c#,.net,winforms,entity-framework,datagridview,C#,.net,Winforms,Entity Framework,Datagridview,用户通过GUI编辑DataGridView必须满足哪些条件?例如按F2键进行修改、选择要删除的行或添加新行 当我将DataGridView.DataSource绑定到本地集合对象(如List)时,我能够执行所有三个操作 当我将DataGridView.DataSource绑定到DataTable或DataView时,我也能够以图形方式完成这三项工作 但是当我将DataGridView.DataSource绑定到DbSet.ToList()或DbSet.ToArray()(Entity Frame

用户通过GUI编辑
DataGridView
必须满足哪些条件?例如按F2键进行修改、选择要删除的行或添加新行

当我将
DataGridView.DataSource
绑定到本地集合对象(如
List
)时,我能够执行所有三个操作

当我将
DataGridView.DataSource
绑定到
DataTable
DataView
时,我也能够以图形方式完成这三项工作

但是当我将
DataGridView.DataSource
绑定到
DbSet.ToList()
DbSet.ToArray()
Entity Framework
)时,我只能修改现有行的非主键值,即使我已经通过DataGridView向导启用了删除和添加功能,并专门将
allowUserToAddress
AllowUserToDeleteRows
设置为
true
。运行时,应用程序不会显示表示可以添加新行的星号。删除一行也是不可能的

但是,数据显示正确

所以,我很困惑。上述数据源的哪些特征可能导致GUI中的不同行为


感谢

DataGridView
如果
allowUserToAddress
都设置为true,并且实现的基础数据源将
AllowNew
返回为true,则控件允许用户添加行。对于删除也有类似的规则

您可以查看和内部方法的源代码

总之,这些是基于数据源的允许操作:

  • 列表
    :编辑
  • BindingList
    :添加、编辑、删除(对于添加,
    T
    应具有无参数构造函数)
  • 数组
    :编辑
  • DataTable
    :添加、编辑、删除
  • BindingSource
    :取决于
    BindingSource
    的底层数据源。如果它是
    IBindingList
    的一个实现,它会向它发出请求,否则如果列表不是
    FixedSize
    则允许所有操作,否则只允许编辑。因此,例如,如果将
    列表设置为绑定源的数据源,然后将绑定源设置为数据网格视图的数据源,则所有操作都将允许该列表
  • IBindingList
    :请求执行
  • 填充dataTable并将其用作datagridview的Bing源
  • 1.1是否在datagridview中进行更改

    public void DAL_UpdateStudentsTable(DataTable table) //DAL represents 3-tyer architecture (so data access layer)
    {
      using (SqlConnection sqlConn = new SqlConnection(connString))
      {
        using (SqlCommand cmd = new SqlCommand())
        {
          cmd.CommandText = @"UPDATE Students SET " +
                    "StudentID = @id, " +
                    "FirstName = @first, " +
                    "LastName = @last, " +
                    "Birthday = @birthday, " +
                    "PersonalNo = @personal " +
                    "WHERE StudentID = @oldId";
          cmd.Parameters.Add("@id", SqlDbType.Int, 5, "StudentID");
          cmd.Parameters.Add("@first", SqlDbType.VarChar, 50, "FirstName");
          cmd.Parameters.Add("@last", SqlDbType.VarChar, 50, "LastName");
          cmd.Parameters.Add("@birthday", SqlDbType.DateTime, 1, "Birthday");
          cmd.Parameters.Add("@personal", SqlDbType.VarChar, 50, "PersonalNo");
          SqlParameter param = cmd.Parameters.Add("@oldId", SqlDbType.Int, 5, "StudentID");
          param.SourceVersion = DataRowVersion.Original;
          cmd.Connection = sqlConn;
          using (SqlDataAdapter da = new SqlDataAdapter())
          {
            da.UpdateCommand = cmd;
            da.Update(table);
          }
        }
      }
    }
    
  • 当您想要更新数据库时,只需创建一个update命令,并按照上面看到的方式执行

  • 谢谢对于诸如List之类的数据源,它根本不是IBindingList的实现,这是否意味着AllowUserToAddress或AllowUserToDeleteRows属性就足以确定是否允许添加或删除行?列表似乎还支持在结论中添加、编辑和删除。感谢在
    DataGridView
    中编辑
    列表的唯一方法是使用
    BindingSource
    。根据绑定源规则,如果您将
    列表
    设置为
    绑定源
    的数据源,然后将
    绑定源
    设置为
    DataGridView
    的数据源,则您可以添加、编辑和删除
    列表
    的项。除了支持添加的上述条件外,别忘了使用公共的无参数构造函数(通常在普通类中使用)。不客气。当您将datagridview绑定到datatable时,实际上它绑定到了它的默认视图,即实现接口的dataview。Reza,非常感谢。我现在有了更好的理解。你太棒了。