When the data source list is DataGridView, the list is re-bound and the data is not updated
Using BindingList is a good solution to this problem (framework2.0 new)
For example, using the list time code
/// <summary>
/// 性别类型维护
/// </summary>
public
partial
class
SexFrm : Form
{
IList<SystemCode> list;
private
void
BindData()
{
list =
new
SystemCodeManager().GetModelByType(type);
dgvSexType.DataSource = list;
}
private
void
btnAdd_Click(
object sender, EventArgs e)
{
SystemCode sys =
new
SystemCode();
sys.CodeNo =
""
;
sys.EnglishName =
""
;
sys.ID = 12312;
sys.Name =
""
;
sys.QuickSign =
""
;
sys.Remark =
""
;
sys.Type = type;
list.Add(sys);
}
}
|
In this case, the data source changes and the actual display data does not change
Immediately you re-bind the data source in the Add method
dgvSexType.DataSource = list; |
在此,使用<span class = "Apple-style-span" style= "font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; white-space: normal;" >BindingList就可以很好的解决这个问题了.</span> |
/// <summary>
/// 性别类型维护
/// </summary>
public
partial
class
SexFrm : Form
{
int
type = 2;
SystemCodeManager sysManager =
new
SystemCodeManager();
BindingList<SystemCode> list;
private
void
SexFrm_Load(
object sender, EventArgs e)
{
new
BaseCode.LanguageSeting().LoadLanuageSettings(
this
);
BindData();
}
private
void
BindData()
{
list =
new
BindingList<SystemCode>(
new
SystemCodeManager().GetModelByType(type));
dgvSexType.DataSource = list;
}
private
void
btnAdd_Click(
object
sender, EventArgs e)
{
SystemCode sys =
new
SystemCode();
sys.CodeNo =
""
;
sys.EnglishName =
""
;
sys.ID = 12312;
sys.Name =
""
;
sys.QuickSign =
""
;
sys.Remark =
""
;
sys.Type = type;
|
<span class = "Apple-style-span" ><br></span><span class = "Apple-style-span" > } </span> |
<span style= "font-family: verdana, Arial, Helvetica, sans-serif;" >这样子,就可以轻松的实现和DataGridview互动了</span> |
<span style= "font-family: verdana, Arial, Helvetica, sans-serif;" >效果</span> |
<span style= "font-family: verdana, Arial, Helvetica, sans-serif;" >点击新增后可以直接新增,如果使用list,就没有任何反应</span> |
<span style= "font-family: verdana, Arial, Helvetica, sans-serif;" ><br></span> |
<span style= "font-family: verdana, Arial, Helvetica, sans-serif;" >注意:BindList需要引用System.ComponentModel命名空间</span> |
The object-oriented collection class generally implements the interface IBindingList, because when the data source is bound, the interface can interact with it if it implements IBindingList. Inadvertently found that Microsoft added a new class in 2.0, Bindinglist<t>, this class from the collection<t>, and realized the IBindingList.
The charm of IbindingList is that he has addnew,applysort,listchangedeventhandler and other methods. While BindingSource is a bridge between a control's data source and a true data source, it can call methods such as the number of IBindingList AddNew. At the same time IBindingList has the data changes when the time will notify BindingSource to update the interface.
MS Bindinglist<t> also does not support sort, search, this is because I do not know why the east has a relationship, in order to achieve some of the functions only self-expansion. Bindinglist<t> a little regret that the deleted data is not noted, which is not comparable to a powerful table. From object-oriented to aspect-oriented, how to support object-oriented in the basic class is still not perfect. Now in the study, object entities, collection, welcome to the master to come to advise.
The following is a description of Bindinglistr on MSDN, and the code example shows how to bind to a BindingList component that contains a business object.
The Http://msdn2.microsoft.com/zh-cn/library/ms132679.aspx#Mtps_DropDownFilterTextBindingList class can be used as a base class for creating bidirectional data binding mechanisms. The BindingList provides a specific generic implementation of the IBindingList interface. This eliminates the need to implement a full IBindingList interface, and implementing a full interface can become difficult due to subtle interactions between IBindingList, IEditableObject, and associated CurrencyManager. However, a typical solution programmer would use a class that provides data binding capabilities (such as BindingSource) instead of using BindingListdirectly.
BindingList supports factory-created instances with the extensible AddNew method. (This type of extensibility is also present in other classes such as BindingSource ) In addition, because this class implements the Icanceladdnew interface, it implements transactional commit or rollback of new items through the EndNew and Cancelnew methods.
Use BindingList to implement dynamic additions and deletions when DataGridView data source is List