简体   繁体   中英

C# GUI Editable DataGridView

What conditions must be satisfied for a DataGridView to be editable by a user through GUI ? such as pressing F2 for modifying, selecting a row for deletion, or adding a new row?

When I bind DataGridView.DataSource to a local collection object such as List<T> , I am able to perform all three actions.

When I bind DataGridView.DataSource to a DataTable or DataView , I am also able to do all three graphically.

But when I bind DataGridView.DataSource to DbSet<T>.ToList<T>() or DbSet<T>.ToArray<T>() ( Entity Framework ), I can only modify non primary key values of existing rows, even though I have enabled delete and add function through DataGridView wizard, and specifically set AllowUserToAddRows and AllowUserToDeleteRows to true . When run, the application won't display the star symbol indicating the ability to add a new row. Deleting a row is not possible either.

The data, however, displays correctly.

So, I am puzzled. What characteristics of the above mentioned data sources could have caused the different behaviors in GUI?

Thanks

DataGridView control allows user to add row if both AllowUserToAddRow is set to true and the underlying data source implemented IBindingList returning AllowNew as true. Similar rules for remove as well.

You can take a look at AllowUserToAddRowsInternal and AllowUserToDeleteRowsInternal internal methods' source code.

As a conclusion, these are allowed operations based on data source:

  • List<T> : Edit
  • BindingList<T> : Add, Edit, Delete (For Add, T should have parameterless constructor)
  • Array : Edit
  • DataTable : Add, Edit, Delete
  • BindingSource : Depends to the underlying data source of the BindingSource . If it's an implementation of IBindingList it asks from it, otherwise if the list is not FixedSize then all operation are allowed, otherwise, just edit is allowed. So for example if you set a List<T> as data source of a binding source and then set the binding source as data source of data grid view, then the list will be allowed for all operation.
  • IBindingList : Asks from implementation.
  1. Fill dataTable and use it as a bining source to datagridview

1.1 Do changes in 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);
      }
    }
  }
}
  1. When you want to update database simply create an Update command and do it like what you see above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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