下面的这段代码,如果是更新过程,也就是update,datagridview可以自动刷新。但如果是insert或delete就不行。跟踪调试,数据库的内容是确实插入或删除(可以控制台输出),但datagridview就是不能自动更新,试着update(),refresh()等都不行。请高手指点。
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
DBTestDataContext dbtest = new DBTestDataContext();
//BindingSource binddb = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedItem.ToString())
{
case "select":
Databind();
break;
case "insert":
LinqInsert();
break;
case "update":
LinqUpdate();
break;
case "delete":
LinqDelete();
break;
default:
break;
}
}
private void LinqDelete( )
{
Faculty fi = dbtest.Faculty.Where(f => f.faculty_id == "M56789").Single();
dbtest.Faculty.DeleteOnSubmit(fi);
dbtest.SubmitChanges();
Databind();
}
private void LinqUpdate()
{
Faculty fi = dbtest.Faculty.Where(f => f.faculty_id == "M56789").First();
fi.college = "qhdx";
dbtest.SubmitChanges();
Databind( );
}
private void LinqInsert()
{
Faculty newft = new Faculty();
newft.faculty_id = "D19886";
newft.faculty_name = "hcg";
newft.title = "gly";
newft.office = "33253";
newft.phone = "13344553344";
newft.college = "bjdx";
newft.email = "hcg@163.com";
dbtest.Faculty.InsertOnSubmit(newft);
dbtest.SubmitChanges();
Databind( );
}
private void Databind()
{
this.dataGridView1.DataSource =dbtest.Faculty;
}
}
}
|