用c#窗体程序实现带值的跳转
FORM1:[img=http://my.csdn.net/my/album/detail/1306586#1306584][/img]
当选中某行之后点击查看详情会跳转到FORM2:
[img=http://my.csdn.net/my/album/detail/1306586#1306586][/img]
数据库:
[img=http://my.csdn.net/my/album/detail/1306588#1306588][/img]
、显示和删除查询的代码如下!求各位前辈叫我实现上面的功能!
from1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection("server=.;database=studentInfoDB;integrated security = SSPI");
SqlDataAdapter da;
DataSet ds = new DataSet();
string tablename = "T_studentInfo";
private void 查询(object sender, EventArgs e)
{
string tj = textBox1.Text;
if (textBox1.Text != "")
{
ds.Clear();
string sql = "SELECT student_id,student_name,student_sex FROM " + tablename + " where student_name='" + tj + "'";
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, tablename);
dataGridView1.DataSource = ds.Tables[tablename];
}
else
{
ds.Clear();
string sql = "SELECT student_id,student_name,student_sex FROM " + tablename;
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, tablename);
dataGridView1.DataSource = ds.Tables[tablename];
}
}
private void Form1_Load(object sender, EventArgs e)
{
ds.Clear();
string sql = "SELECT student_id,student_name,student_sex FROM "+tablename;
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, tablename);
dataGridView1.DataSource = ds.Tables[tablename];
}
private void 查看详情_Click(object sender, EventArgs e)
{
}
private void 全部显示_Click(object sender, EventArgs e)
{
ds.Clear();
string sql = "SELECT student_id,student_name,student_sex FROM " + tablename;
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, tablename);
dataGridView1.DataSource = ds.Tables[tablename];
}
private void 删除_Click(object sender, EventArgs e)
{
Int32 n = dataGridView1.CurrentCell.RowIndex;
DataRowCollection drc = ds.Tables["T_studentInfo"].Rows;
drc[n].Delete();
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, "T_studentInfo");
}
}
}
--------------------编程问答--------------------
--------------------编程问答-------------------- FORM1:
private void 查看详情_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
当选中某行之后点击查看详情会跳转到FORM2:
数据库:
--------------------编程问答-------------------- 在From2的构造函数上加:
public Form2(string id)
{
student_id = id;
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
然后Form1的按钮事件:
Form2 form2=new Form2(dgv.SelectedRows[0].Cells[0].Value.ToString());
这样不就取到学号了,最后通过学号从数据库中取出相应数据即可 --------------------编程问答-------------------- - -忘了 删掉 Control.CheckForIllegalCrossThreadCalls = false; 这个你删掉吧 .. 我忘记没删 --------------------编程问答-------------------- form2 里:
string key;
public form2(string _key)
{
this.key=_key;
}
form1里:
form2 f=new form2("value");
f.show(); --------------------编程问答-------------------- 楼上正解 --------------------编程问答--------------------
5楼的前辈,FORM生成的时候说:找不到类型或命名空间名称form2(是否缺少引用集....)
我FORM的代码,是这样查询的不,有什么办法能将FORM2查询到的数据绑定到label1.text啊!只要一个后面的就可以一样了!
--------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
string key;
public Form2(string _key)
{
this.key = _key;
InitializeComponent();
}
SqlConnection conn = new SqlConnection("server=.\\SQLEXPRESS;database=studentInfoDB;integrated security = SSPI");
SqlDataAdapter da;
DataSet ds = new DataSet();
string tablename = "T_studentInfo";
private void Form2_Load(object sender, EventArgs e)
{
ds.Clear();
string sql = "select student_id,student_name,student_sex,student_class,student_birth,student_native from dbo.T_studentInfo where student_id='" + key + "'";
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, tablename);
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
把form2换成大写了就可以了,但是f.show();
这题提示并不包含show的定义! --------------------编程问答--------------------
Form2 form2=new Form2(dgv.SelectedRows[0].Cells[0].Value.ToString());
索引超出范围。必须为非负值并小于集合大小。
参数名: index
求解
补充:.NET技术 , C#