如何通过鼠标选择一行把datagridview里面的值赋给一个窗体进行修改和删除操作。
需要使用datagridview里的那个事件。
需要使用datagridview里的那个事件。
解决方案 »
- C# datavgridview
- 上传文件后的数据流,可以得到文件的创建时间和修改时间吗?
- Vs 2005 工具栏的查找框和查找按钮不见了,要进菜单栏去点“查找”才能出来。
- 如何能在web页面上显示word文档的内容,请各位高手指教!!(在线跪求答案)
- 如何取得网关,DNS,代理服务器等信息?
- 谁有.net版本的··无限级分类导航的源码(前台展示分类的页面和,增删改查管理分类的后台)
- 求!把asp分页类 翻译成 c#
- c#共享
- --------------100分求教WinForm中控件布局的问题。--------------
- C#WinFrom编程:不同页面之间dataSet问题
- 如何把SQLSERVER中定义的表结构直接导出CS类代码?
- GridView控件内某一个TextBox有他的事件吗?有的话请指出如何弄出来?
x.id=当前id;
...
在Form2中添加你要接收相应的public变量数组: public string[] str; private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow dr = this.dataGridView1.Rows[e.RowIndex]; if (dr != null)
{
Form2 frm =new Form2();
int i=0;
foreach (DataGridViewCell cell in dr.Cells)
{
frm.str[i]=cell.Value.ToString();
i++;
}
frm.ShowDialog();
}
}
当然了,你需要在form2中对str进行处理,显示出来
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
this.textBox1.Text = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
this.textBox2.Text = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
this.textBox3.Text = this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
this.textBox4.Text = this.dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
.
.
.
this.textBox5.Text = this.dataGridView1.Rows[e.RowIndex].Cells[n].Value.ToString();
}//点击单元格或行获得值
private void button1_Click(object sender, EventArgs e)
{
SqlCommand command= new SqlCommand("update set ="+"'"+this.textBox1.Text +"','"+this.textBox2.Text +"','"...
+" where 主键="+所选行主键值,Connection);
command.ExecuteNonQuery();
command.Dispose(); }//更新
在新窗体中根据该id查询数据库,再把数据中的相关字段绑定到相应的TextBox。
{
// MessageBox.Show("" + this.dataGridView1.SelectedRows[0].Cells["name"].Value.ToString()); String name = this.dataGridView1.SelectedRows[0].Cells["name"].Value.ToString(); String password = this.dataGridView1.SelectedRows[0].Cells["password"].Value.ToString(); Form2 f2 = new Form2(name, password); f2.Show();
}
后面调用的窗口 public Form2()
{
InitializeComponent();
} public Form2(String name,String password)
{
InitializeComponent(); textBox1.Text = name;
textBox2.Text = password;
}这样就可以传值了。。
select username as name, password as password from userinfo
private void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//获取界面上当前对象
M_Customer mObject = dtsCustomer.Current as M_Customer; //将该对象传入编辑窗口
frmCustomer mfrm = new frmCustomer(mObject, FormType.Modify); if (mfrm.ShowDialog() == DialogResult.OK)
{
CustomerHandler mHandler = new CustomerHandler();
mHandler.Save(mObject);
}
}//编辑窗口的构造函数
public frmCustomer(M_Customer mDtCustomer, FormType mFormType)
{
InitializeComponent(); //将传入对象与控件绑定
this._Customer = mDtCustomer;
this.dtsCustomer.DataSource = this._Customer;
}用绑定方式和传对象是比较简单的,不需要到编辑界面中再根据ID查找出当前记录。