按下添加按钮后 datagridView 不刷新

renstars 2009-12-31 03:59:47
代码 添加后 数据库中存在 窗体里不显示

if (bh1txt.Text == "")
{
MessageBox.Show("请输入教师编号!");
bh1txt.Focus();
return;
}
if (dd1txt.Text == "")
{
MessageBox.Show("请输入教师姓名!");
dd1txt.Focus();
return;
}
if (lx1txt.Text == "")
{
MessageBox.Show("请输入教师性别!");
lx1txt.Focus();
return;
}
if (lx1txt.Text == "")
{
MessageBox.Show("请输入授课专业!");
lx1txt.Focus();
return;
}
SqlConnection conn = new SqlConnection(ConSql1);
conn.Open();
string ConSql = "select * from 教师信息";
SqlCommand cmd = new SqlCommand(ConSql, conn);
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read() == true)
{
if (bh1txt.Text.Trim().CompareTo(dr[0].ToString().Trim()) == 0)
{
MessageBox.Show("教师编号已存在,请重新输入");
return;
}
}
conn.Close();
string bianh = bh1txt.Text;
string mingc = dd1txt.Text;
string rens = lx1txt.Text;
string rens1= comSk.Text;
string ConSql2 = "insert into 教师信息 values('" + bianh + "','" + mingc + "','" + rens + "','" + rens1 + "')";
SqlConnection conn2 = new SqlConnection(ConSql1);
conn2.Open();
SqlCommand cmd2 = new SqlCommand(ConSql2, conn2);
cmd2.ExecuteReader();
TeacherInfo_Load(sender, e);
...全文
273 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
智鹿软件 2010-01-01
  • 打赏
  • 举报
回复
在窗体加载的时候通常要写一个showDim方法,这个方法会把数据库的数据读到datagridView中!
在实现插入数据和再调用一下这个方法就可以了!就是说当你从前台把数据改了之后再从新把数据库的数据读到datagridView中!
jason110163 2010-01-01
  • 打赏
  • 举报
回复
想即时显示数据,估计要用时间控件来设置个时间 刷了
iamwxj 2010-01-01
  • 打赏
  • 举报
回复
最好把数据查询条件拼凑在sql语句里,这样效率会高些
czc123456789 2010-01-01
  • 打赏
  • 举报
回复
要用时间器来刷的 不然你的操作只是操作而已
JenkGan 2010-01-01
  • 打赏
  • 举报
回复
我是这样想的,你可以加以循环哦,吧循环放在要刷新的地方哦,
qinzugan 2010-01-01
  • 打赏
  • 举报
回复
你要做到事件去触发才能够实现刷新,显示现在的数据,因为你开始把数据放在DataSet里,就像你把水放在水池里了,随时用,你要让更新后的DataSet跟dataGridView1绑定,就要重新通过事件触发,比如KeyDown事件,button事件,或 Form Activte事件就可以了。
tianliang1 2010-01-01
  • 打赏
  • 举报
回复
你是给dataGridView帮定的数据源吧,那样的话,你更新了数据表,dataGridView是不能一起更新的。。。
你必须重新帮顶一次。。。
如:
用一个更新按钮来实现:
this.dataGridView1.DataSource=null;
this.dataGridView1.DataSource=newTable;
zhanghang19890621 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用楼主 renstars 的回复:]
代码 添加后 数据库中存在 窗体里不显示

if (bh1txt.Text == "")
            {
                MessageBox.Show("请输入教师编号!");
                bh1txt.Focus();
                return;
            }
            if (dd1txt.Text == "")
            {
                MessageBox.Show("请输入教师姓名!");
                dd1txt.Focus();
                return;
            }
            if (lx1txt.Text == "")
            {
                MessageBox.Show("请输入教师性别!");
                lx1txt.Focus();
                return;
            }
            if (lx1txt.Text == "")
            {
                MessageBox.Show("请输入授课专业!");
                lx1txt.Focus();
                return;
            }
            SqlConnection conn = new SqlConnection(ConSql1);
            conn.Open();
            string ConSql = "select * from 教师信息";
            SqlCommand cmd = new SqlCommand(ConSql, conn);
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read() == true)
            {
                if (bh1txt.Text.Trim().CompareTo(dr[0].ToString().Trim()) == 0)
                {
                    MessageBox.Show("教师编号已存在,请重新输入");
                    return;
                }
            }
            conn.Close();
            string bianh = bh1txt.Text;
            string mingc = dd1txt.Text;
            string rens = lx1txt.Text;
            string rens1= comSk.Text;
            string ConSql2 = "insert into 教师信息 values('" + bianh + "','" + mingc + "','" + rens + "','" + rens1 + "')";
            SqlConnection conn2 = new SqlConnection(ConSql1);
            conn2.Open();
            SqlCommand cmd2 = new SqlCommand(ConSql2, conn2);
            cmd2.ExecuteReader();
            TeacherInfo_Load(sender, e);
[/Quote]
cmd2.ExecuteNonQuery();
TeacherInfo_Load(sender, e);
uuuuuu11 2009-12-31
  • 打赏
  • 举报
回复
对啊没绑定呀
让爱延续 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用楼主 renstars 的回复:]
代码 添加后 数据库中存在 窗体里不显示

if (bh1txt.Text == "")
            {
                MessageBox.Show("请输入教师编号!");
                bh1txt.Focus();
                return;
            }
            if (dd1txt.Text == "")
            {
                MessageBox.Show("请输入教师姓名!");
                dd1txt.Focus();
                return;
            }
            if (lx1txt.Text == "")
            {
                MessageBox.Show("请输入教师性别!");
                lx1txt.Focus();
                return;
            }
            if (lx1txt.Text == "")
            {
                MessageBox.Show("请输入授课专业!");
                lx1txt.Focus();
                return;
            }
            SqlConnection conn = new SqlConnection(ConSql1);
            conn.Open();
            string ConSql = "select * from 教师信息";
            SqlCommand cmd = new SqlCommand(ConSql, conn);
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read() == true)
            {
                if (bh1txt.Text.Trim().CompareTo(dr[0].ToString().Trim()) == 0)
                {
                    MessageBox.Show("教师编号已存在,请重新输入");
                    return;
                }
            }
            conn.Close();
            string bianh = bh1txt.Text;
            string mingc = dd1txt.Text;
            string rens = lx1txt.Text;
            string rens1= comSk.Text;
            string ConSql2 = "insert into 教师信息 values('" + bianh + "','" + mingc + "','" + rens + "','" + rens1 + "')";
            SqlConnection conn2 = new SqlConnection(ConSql1);
            conn2.Open();
            SqlCommand cmd2 = new SqlCommand(ConSql2, conn2);
            cmd2.ExecuteReader();
            TeacherInfo_Load(sender, e);
[/Quote]

Lz 能不能把 你的 TeacherInfo_Load(sender, e);方法的代码贴一下啊!?里面有没有绑定数据的代码啊!?
liangzhl 2009-12-31
  • 打赏
  • 举报
回复
是啊,重新绑定一次就刷新了

lg_tz 2009-12-31
  • 打赏
  • 举报
回复
试一下:this.DataView.DataBind();
xzjxylophone 2009-12-31
  • 打赏
  • 举报
回复
重新绑定一下
sharp_future 2009-12-31
  • 打赏
  • 举报
回复
wf程序的话 点击按钮之后 对dgv执行以下timer
web的话 response.跳转("");
michaelnami 2009-12-31
  • 打赏
  • 举报
回复
er 本来啊就没有刷新的代码 这个要手动刷新的
cicigl 2009-12-31
  • 打赏
  • 举报
回复
得刷新下页面,页面存的是上次数据集里面的数据

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧