高手指点,两个窗体数据如何联系

bianfy0956 2012-04-06 06:05:49
form1的CellDoubleClick事件中,双击datagridview1中显示数据库的索引值的单元格,弹出form2,设置若干个textbox,用来显示数据库中索引对应的数据值。
...全文
106 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bianfy0956 2012-04-07
  • 打赏
  • 举报
回复
谢谢楼上大虾帮助,回去好好学习!感谢~
orochiheart 2012-04-06
  • 打赏
  • 举报
回复
这是我初学时写的代码。。。要是现在 肯定不会这样写了(特别是那个sql语句居然写了三句去取 太神奇了现在看来)。。。我也懒得修改了 你要能用就自己改改吧呵呵。。。
banchedlq 2012-04-06
  • 打赏
  • 举报
回复
可以添加一个类,并添加类属性
class Chuanzhi
{
public static string text1;
public static string text2;
………
}

然后在form1的CellDoubleClick事件中
Chuanzhi.text1="要会的值1";
Chuanzhi.text2="要会的值2";
………
form2 frm=new form2();
frm.show();


再在form2的Load事件中
textbox1.text=Chuanzhi.text1;
textbox1.text=Chuanzhi.text1;
orochiheart 2012-04-06
  • 打赏
  • 举报
回复
string ConStr = @"server=.\SQLEXPRESS;uid=sa;pwd=sa;database=Northwind";
string i = "";
public UpdateForm(string i)
{
InitializeComponent();
this.i = i;

SqlConnection con = new SqlConnection(ConStr);
con.Open();
SqlCommand cmdProductName = new SqlCommand("select ProductName from Products where ProductID=" + i, con);
SqlCommand cmdQuantityPerUnit = new SqlCommand("select QuantityPerUnit from Products where ProductID=" + i, con);
SqlCommand cmdUnitPrice = new SqlCommand("select UnitPrice from Products where ProductID=" + i, con);

object objProductName = cmdProductName.ExecuteScalar();
object objcmdQuantityPerUnit = cmdQuantityPerUnit.ExecuteScalar();
object objcmdUnitPrice = cmdUnitPrice.ExecuteScalar();

txt_ProductName.Text = objProductName.ToString();
txt_QuantityPerUnit.Text = objcmdQuantityPerUnit.ToString();
txt_UnitPrice.Text = objcmdUnitPrice.ToString();

con.Close();
con.Dispose();
}
orochiheart 2012-04-06
  • 打赏
  • 举报
回复
string ConStr = @"server=.\SQLEXPRESS;uid=sa;pwd=sa;database=Northwind";
string i = "";
public UpdateForm(string i)
{
InitializeComponent();
this.i = i;

SqlConnection con = new SqlConnection(ConStr);
con.Open();
SqlCommand cmdProductName = new SqlCommand("select ProductName from Products where ProductID=" + i, con);
SqlCommand cmdQuantityPerUnit = new SqlCommand("select QuantityPerUnit from Products where ProductID=" + i, con);
SqlCommand cmdUnitPrice = new SqlCommand("select UnitPrice from Products where ProductID=" + i, con);

object objProductName = cmdProductName.ExecuteScalar();
object objcmdQuantityPerUnit = cmdQuantityPerUnit.ExecuteScalar();
object objcmdUnitPrice = cmdUnitPrice.ExecuteScalar();

txt_ProductName.Text = objProductName.ToString();
txt_QuantityPerUnit.Text = objcmdQuantityPerUnit.ToString();
txt_UnitPrice.Text = objcmdUnitPrice.ToString();

con.Close();
con.Dispose();
}
orochiheart 2012-04-06
  • 打赏
  • 举报
回复
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string strId = dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString();
UpdateForm upf = new UpdateForm(strId);
upf.ShowDialog();
dgvSet();
}
threenewbee 2012-04-06
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20110407/19/c1068d69-7331-4d02-bc0b-f5ba7a5f8dd8.html
threenewbee 2012-04-06
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20110407/19/c1068d69-7331-4d02-bc0b-f5ba7a5f8dd8.html
觉悟之时 2012-04-06
  • 打赏
  • 举报
回复
用属性。

111,126

社区成员

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

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

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