刚由Java转学C# 还请指教 一个简单的windows窗体程序
做了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());
}
}
}