有2个winform,form1里有个datagridview(这个里面没有记录)有个button1,点击button1弹出form2,form2里
有2个winform,form1里有个datagridview(这个里面没有记录)有个button1,点击button1弹出form2,form2里也有个datagrid...
有2个winform,form1里有个datagridview(这个里面没有记录)有个button1,点击button1弹出form2,form2里也有个datagridview(这个里面有记录),现在想完成:双击form2里datagridview的某行,在form1里datagridview就显示这行,同时form2关闭。 本人菜鸟、求完整版程序可以直接借用的~
怎么将数据显示在最后一行呢? 展开
怎么将数据显示在最后一行呢? 展开
展开全部
form1代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string GetEmpCode = "";
private static string ConnectionString = "Data source=.;Initial Catalog=mrmfsoft;uid=sa;pwd=";
public Form1(string empCode)
{
GetEmpCode = empCode;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
if (GetEmpCode.Trim() != "")
{
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("select * from EmpInfo where EmpCode='" + GetEmpCode.Trim() + "'", conn);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
DataSet ds = new DataSet();
dap.Fill(ds, "tb_emp");
dgvEmpInfo.AutoGenerateColumns = false;
dgvEmpInfo.DataSource = ds.Tables[0];
dgvEmpInfo.ClearSelection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
form2代码:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string ConnectionString = "Data source=.;Initial Catalog=mrmfsoft;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("select * from EmpInfo", conn);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
DataSet ds = new DataSet();
dap.Fill(ds, "tb_emp");
dgvEmpInfo.AutoGenerateColumns = false;
dgvEmpInfo.DataSource = ds.Tables[0];
dgvEmpInfo.ClearSelection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void dgvEmpInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string GetEmpCode = this.dgvEmpInfo.CurrentRow.Cells["EmpCode"].Value.ToString();
Form1 f1 = new Form1(GetEmpCode);
f1.Show();
this.Close();
}
}
可供参考
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string GetEmpCode = "";
private static string ConnectionString = "Data source=.;Initial Catalog=mrmfsoft;uid=sa;pwd=";
public Form1(string empCode)
{
GetEmpCode = empCode;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
if (GetEmpCode.Trim() != "")
{
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("select * from EmpInfo where EmpCode='" + GetEmpCode.Trim() + "'", conn);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
DataSet ds = new DataSet();
dap.Fill(ds, "tb_emp");
dgvEmpInfo.AutoGenerateColumns = false;
dgvEmpInfo.DataSource = ds.Tables[0];
dgvEmpInfo.ClearSelection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
form2代码:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string ConnectionString = "Data source=.;Initial Catalog=mrmfsoft;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("select * from EmpInfo", conn);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
DataSet ds = new DataSet();
dap.Fill(ds, "tb_emp");
dgvEmpInfo.AutoGenerateColumns = false;
dgvEmpInfo.DataSource = ds.Tables[0];
dgvEmpInfo.ClearSelection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void dgvEmpInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string GetEmpCode = this.dgvEmpInfo.CurrentRow.Cells["EmpCode"].Value.ToString();
Form1 f1 = new Form1(GetEmpCode);
f1.Show();
this.Close();
}
}
可供参考
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询