求高手关注,,如何在win窗体程序中修改dataGridView中的数据?

wyb2009333478 2012-05-09 12:50:18
我用VS2010建立了两个窗体程序Form1,和Form2;然后用Access2007建立了数据库jiedai.accdb和一个表lend。
lend表中有5列。ID(主键,自动编号);借书人(文本);借书时间(日期);还书时间(日期);归还与否(是否);
Form2中对应控件:textBox1,textBox1,dateTimePicker1,dateTimePicker2,comboBox1
然后,我在Form1中放了一个dataGridView控件,和一个button控件(命名修改,实现修改功能);
然后,在Form2中也放了个dataGridView控件,和两个个button控件(确定,取消);
问题是,我想在Form1中,选中dataGridView显示的一行后,点击修改。弹出Form2,而Form2中的dataGridView只显示Form1中选中的那一行,并在Form2中的相应空间显示出来,修改空间中的值后,点击确定,完成修改。。。
因为是新手,,所以不会插图,,谅解。。
谁能给我编写下呀,,求代码。
...全文
293 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyb2009333478 2012-05-10
  • 打赏
  • 举报
回复
是时间哪儿有问题。。除了时间有问题,其他的都差不多可以了,
我在Form1中的修改加了
int id = int.Parse(dgv1.SelectedRows[0].Cells[0].Value.ToString());
f2.textBox1.Text = this.dgv1.SelectedRows[0].Cells[1].Value.ToString();
f2.textBox2.Text = this.dgv1.SelectedRows[0].Cells[2].Value.ToString();
// f2.dTimer1.Value = this.dgv1.SelectedRows[0].Cells[3].Value.ToString();
// f2.dTimer2.Value = this.dgv1.SelectedRows[0].Cells[4].Value.ToString();
// f2.dgv2.DataSource = this.dgv1.SelectedRows[0].Cells.ToString();
string strSql = "SELECT * FROM lend where ID=" + id;
f2.dgv2.DataSource = db.dataTable(strSql).DefaultView;
f2.Show();
点击修改按钮后,除了时间,其他都显示了,数据表中的时间值(如2010/10/01)无法返回控件dTimer1?
另外,,在Form2中的dataGridView控件中显示了选中行,但是Form1中的dataGridView控件也只显示了选中行;
Form1的显示是使用的
string strSql = "SELECT * FROM lend";
dgv1.DataSource = db.dataTable(strSql).DefaultView;
(strSql是定义的显示方法, datebase db =new datebase)
然后我该怎么办?
另外在Form2的确定按钮添加什么代码?可以实现修改并保存功能?
w767687781 2012-05-10
  • 打赏
  • 举报
回复
怎么有错误…………
wyb2009333478 2012-05-10
  • 打赏
  • 举报
回复
嗯嗯,,谢谢各位大神。。。我先去做做看,,有问题在问你们。。。谢谢哈。。。
wyb2009333478 2012-05-10
  • 打赏
  • 举报
回复
是时间哪儿有问题。。除了时间有问题,其他的都差不多可以了,
我在Form1中的修改加了
int id = int.Parse(dgv1.SelectedRows[0].Cells[0].Value.ToString());
f2.textBox1.Text = this.dgv1.SelectedRows[0].Cells[1].Value.ToString();
f2.textBox2.Text = this.dgv1.SelectedRows[0].Cells[2].Value.ToString();
// f2.dTimer1.Value = this.dgv1.SelectedRows[0].Cells[3].Value.ToString();
// f2.dTimer2.Value = this.dgv1.SelectedRows[0].Cells[4].Value.ToString();
// f2.dgv2.DataSource = this.dgv1.SelectedRows[0].Cells.ToString();
string strSql = "SELECT * FROM lend where ID=" + id;
f2.dgv2.DataSource = db.dataTable(strSql).DefaultView;
f2.Show();
点击修改按钮后,除了时间,其他都显示了,数据表中的时间值(如2010/10/01)无法返回控件dTimer1?
另外,,在Form2中的dataGridView控件中显示了选中行,但是Form1中的dataGridView控件也只显示了选中行;
Form1的显示是使用的
string strSql = "SELECT * FROM lend";
dgv1.DataSource = db.dataTable(strSql).DefaultView;
(strSql是定义的显示方法, datebase db =new datebase)
然后我该怎么办?
另外在Form2的确定按钮添加什么代码?可以实现修改并保存功能?[Quote=引用 9 楼 的回复:]
怎么有错误…………
[/Quote]
taylor-yang 2012-05-09
  • 打赏
  • 举报
回复
方法一:在Form1中,选中dataGridView显示的一行后,获取这一行的数据,放在DataTable中,show Form2的时候传参过去,传DataTable,Form2接收DataTable,绑定到Form2的dataGridView中。

方法二:在Form1中,选中dataGridView显示的一行后,获取主键列,show Form2的时候传参过去,Form2接收参数,然后在Form2中根据传来的主键,去数据库里查询对应的信息,查出来之后放在DataTable中,然后绑定到Form2的dataGridView中。
wangsong145 2012-05-09
  • 打赏
  • 举报
回复
你的Form2是为了实现编辑的功能,所以你在Form1中选择一行以后点击修改按钮,只需要将Form1里面的选中行传给Form2,然后在Form2里面就不要使用DataGridView了,直接通过TextBox等这些控件显示选中行的信息,然后保存Form2中的TextBox控件的内容就可以了
zhujiawei7 2012-05-09
  • 打赏
  • 举报
回复
dataGridView.SelectedRows 获取选中行,然后作为参数传给Form2
Form2从DataGridViewRow中获取数据显示到各个控件上
zhujiawei7 2012-05-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

作为参数传递,,,怎么传递?能给我代码吗?帮忙写下可以吗?谢谢。。。引用 1 楼 的回复:
dataGridView.SelectedRows 获取选中行,然后作为参数传给Form2
Form2从DataGridViewRow中获取数据显示到各个控件上
[/Quote]

private void btn修改_Click(object sender, EventArgs e)
{
new Form2(dataGridView1.SelectedRows).Show();
}

在Form2定义个有参构造函数
public Form2(DataGridViewSelectedRowCollection rows)
{
InitializeComponent();
s_rows = rows;
}
private DataGridViewSelectedRowCollection s_rows;


然后获取数据
foreach (DataGridViewRow row in s_rows)
{
MessageBox.Show("借书人:" + row.Cells[0].Value + "结束时间:" + row.Cells[1].Value);
}
xky96 2012-05-09
  • 打赏
  • 举报
回复
----------------------Form1

BindingSource bindingSource;

Form1_Load:
bindingSource=new BindingSource();
dataGridView.DataSource=数据集.数据表
bindingSource.DataSource=数据集.数据表

修改按钮_Click:

bindingSource.Filter = "id=" + dataGridView1.CurrentRow.Cells["id"];
Form2 form=new Form2(bindingSource);
form.ShowDialog();

----------------------Form2
BindingSource bindingSource;
Form(BindingSource bindingSource)
{
this.bindingSource=bindingSource;
}

Form2_Load:
dataGridView.DataSource=bindingSource;
textBox1.DataBindings.Add("Text",bindingSource,"id");
textBox2.DataBindings.Add("Text",bindingSource,"借书人");
dataTimePicker1.DataBindings.Add("Value",bindingSource,"借书日期");
dataTimePicker2.DataBindings.Add("Value",bindingSource,"还书日期");
comboBox.DataBindings.Add("Text",bindingSource,"归还与否");


确定按钮_Click:
bindingSource.EndEdit();
this.Close();

取消按钮_Click:
bindingSource.CancelEdit();
this.Close();
-------------------------------
上述代码没有考虑保存数据库
wyb2009333478 2012-05-09
  • 打赏
  • 举报
回复
作为参数传递,,,怎么传递?能给我代码吗?帮忙写下可以吗?谢谢。。。[Quote=引用 1 楼 的回复:]
dataGridView.SelectedRows 获取选中行,然后作为参数传给Form2
Form2从DataGridViewRow中获取数据显示到各个控件上
[/Quote]
bdmh 2012-05-09
  • 打赏
  • 举报
回复
又是这类问题,最直接的方法

把form2中你要操作的控件的modifers属性设置为public,然后form1中双击时

Form2 frm = new Form2();
frm.TextBox1.Text = this.DataGridView1.CurrentRow.xxxxxx;
frm.TextBox2.Text = xxxxx;

或者Form2中声明一个变量
public DataGridView Grid;双击时
Form2 frm = new Form2();
frm.Grid = this.DataGridView1;
这样在Form2中就访问Grid即可

111,126

社区成员

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

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

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