刚由Java转学C# 还请指教 一个简单的windows窗体程序

moonstarlonely 2009-04-01 02:53:03
做了2个窗体
其中Form1中只有一个菜单栏,包括:刷新 添加 编辑 删除 四个菜单项
Form2只是做的添加事件的处理 我想实现添加的功能,可是好像Form2中的button执行event后,应该与Form1关联
请c#高手帮忙看看
谢谢
//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
{
string sql;
sql = "select * from bb";
dataGridView1.DataSource = EMS.SQLHelper.GetDataTable(sql);
}

private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();

}

private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{
int row;
row = dataGridView1.CurrentRow.Index;
string userid;
userid = dataGridView1.Rows[row].Cells["userid"].Value.ToString();
string password;
password=dataGridView1.Rows[row].Cells["password"].Value.ToString();
string sql;
sql = "update bb set userid='"+userid+"' and password='"+password+"'";
dataGridView1.DataSource = EMS.SQLHelper.GetDataTable(sql);
}

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
int row;
row = dataGridView1.CurrentRow.Index;

string userid;
userid = dataGridView1.Rows[row].Cells["userid"].Value.ToString();

string sql;
sql = "delete from bb where userid='"+userid+"'";
dataGridView1.DataSource = EMS.SQLHelper.GetDataTable(sql);
}
}
}
//Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
}

private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
{
MessageBox.Show("请输入用户名和科目名称", "ERROR");
}
else
{
string s1 = textBox1.Text.Trim();
string s2 = textBox2.Text.Trim();
string sql;
sql = "insert into table bb(userid,password) values('"+s1+"','"+s2+"') ";
EMS.SQLHelper.ExecuteNonQuery(sql, null);
}
}
}
}
//Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//Form1 f = new Form1();
//if(f.ShowDialog()==DialogResult.OK)
EMS.SQLHelper.Set_ConnString("WANGG", "sa", "", "test");//设置数据库服务器,用户名,密码,数据库名
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}



...全文
126 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
moonstarlonely 2009-04-01
  • 打赏
  • 举报
回复
哥们
能讲的详细点不
yang_road 2009-04-01
  • 打赏
  • 举报
回复
建议你将刷新的一段重构成一个方法:
private void RefreshData()
{
string sql;
sql = "select * from bb";
dataGridView1.DataSource = EMS.SQLHelper.GetDataTable(sql);
}
每次添加,修改数据后,直接调用这个方法,这样是不是符合你的要求?
moonstarlonely 2009-04-01
  • 打赏
  • 举报
回复
为什么没有把数据写入数据库中的表中呢?
lihong1024 2009-04-01
  • 打赏
  • 举报
回复
D
kkun_3yue3 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 moonstarlonely 的回复:]
好像不正确
谢谢
请在想想
[/Quote]

什么不正确?
moonstarlonely 2009-04-01
  • 打赏
  • 举报
回复
好像不正确
谢谢
请在想想
kkun_3yue3 2009-04-01
  • 打赏
  • 举报
回复
在FORM2里声明个事件
在FORM1里注册这个事件
在FORM2添加完数据后触发一下事件

cja03 2009-04-01
  • 打赏
  • 举报
回复
sql = "insert into table bb(userid,password) values('"+s1+"','"+s2+"') "; EMS.SQLHelper.ExecuteNonQuery(sql, null);

问题是点击FORM2的添加后,数据库中并没有添加数据
-----------------------------------------------------------------

进ExecuteNonQuery方法里面看看执行的过程出现了什么问题。
moonstarlonely 2009-04-01
  • 打赏
  • 举报
回复
谢谢
我试一下
Harvey_He 2009-04-01
  • 打赏
  • 举报
回复
你说的意思是Form2添加后Form1中没有自动刷新吧,把刷新单独的写出来,声明为Static,然后调用一下就可以了!

//form1中
private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.refresh();
}

public Static void refresh()
{
string sql;
sql = "select * from bb";
dataGridView1.DataSource = EMS.SQLHelper.GetDataTable(sql);
}


//form2中

private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
{
MessageBox.Show("请输入用户名和科目名称", "ERROR");
}
else
{
string s1 = textBox1.Text.Trim();
string s2 = textBox2.Text.Trim();
string sql;
sql = "insert into table bb(userid,password) values('"+s1+"','"+s2+"') ";
EMS.SQLHelper.ExecuteNonQuery(sql, null);

Form1.refresh();
}
}

moonstarlonely 2009-04-01
  • 打赏
  • 举报
回复
问题是点击FORM2的添加后,数据库中并没有添加数据
kkun_3yue3 2009-04-01
  • 打赏
  • 举报
回复
能把问题说清楚么

111,126

社区成员

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

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

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