Reputation:
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
Upvotes: 4
Views: 530
Reputation: 20302
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);
}
}
}
}
Upvotes: 0
Reputation: 125197
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>
: EditBindingList<T>
: Add, Edit, Delete (For Add, T
should have parameterless constructor)Array
: EditDataTable
: Add, Edit, DeleteBindingSource
: 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.Upvotes: 2