Datagridview更新帮助 [英] Datagridview update help
问题描述
大家好,我正在做我的第一个winforms EDM项目,并且试图用一个可由用户更新的Presenters列表填充Datagridview.我网格中的列是名称和电子邮件地址(DataGridViewLinkColumn).到目前为止,我已经能够正确显示网格,但是当我尝试添加新行时,即使我收到一个msgbox来表示所做的更改,我的更改也不会保存回数据库.这是我填充DGV的方式:
Hi everyone, I''m working on my first winforms EDM project and I''m trying to populate a Datagridview with a list of Presenters that can be updated by a user. The cols in my grid are Name and Email address(DataGridViewLinkColumn). So far I have been able to display the grid correctly but when I try to add a new row my changes are not being saved back to the DB even though I get a msgbox to say they have. Here''s how I''m populating my DGV:
private void frmPresenters_Load(object sender, EventArgs e)
{
dal = new DataAccessLayer();
try
{
//Get the List of Contacts from the DAL and bind to the DataGrid
source = new BindingSource();
source.DataSource = dal.GetPresentersList();
this.dgvPresenters.DataSource = source;
dgvPresenters.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dgvPresenters.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Exception occurred: {0}", ex.Message));
}
}
我也无法编辑电子邮件地址"字段(我认为这可能是一个单独的线程)
所有更新都是通过单击按钮进行的:
I''m unable to edit the Email address field as well (i think this may be a seperate thread)
all updates are made by clicking a button:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
// Save object changes to the database, display a message, and refresh the form.
this.source.EndEdit();
//dal.entities.SaveChanges();
MessageBox.Show("Changes saved to the database.");
this.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
谁能帮我吗?
更正了第一个代码块中未正确设置的C#的[edit] pre标签-PES [/edit]
Can anyone help me please?
[edit]pre tag for C# not set properly in the first code block is corrected - PES [/edit]
推荐答案
如Wes Aday ,调用 EndEdit 从 DataGridView 实例不会将更改保存到数据库.为此,您可以使用 TableAdapter .调用其 Update 方法会将更改保存到数据库.
如何使用DataGrid插入,更新和删除 [ ^ ]
使用VS C#Windows Form中的datagridview更新SQL中的数据 [带有明细表的DataGridView-VS 2005 [ ^ ]
第4步:插入,更新和删除数据 [
As mentioned by Wes Aday, calling EndEdit from the DataGridView instance does not save changes to the database. To do that you can use a TableAdapter. Calling its Update method will save changes to the database.
How to insert, update, delete using DataGrid[^]
Updating data in SQL using datagridview in VS C# Windows Form[^]
DataGridView with Detail Edit Form - VS 2005[^]
Step 4: Inserting, Updating, and Deleting Data[^]
The above links are informative and provide examples.
不是使用List对象填充Datagridview,而是将以下内容用作Datagridview.datasource
instead of populating the Datagridview with a List object I am now using the following as my Datagridview.datasource
public ObjectResult<presenters> GetPresentersList()
{
// Check we have an ObjectContext
if (entities == null) entities = new CompanySecretaryEntities();
// Create a query from the entityset
var query = from p in entities.Presenters
select p;
ObjectQuery<presenters> presenters = (ObjectQuery<presenters>)query;
return presenters.Execute(MergeOption.AppendOnly);
}</presenters></presenters></presenters>
这篇关于Datagridview更新帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!