关于datagirdview的疑惑

润滑剂 2010-01-11 03:36:10
我的代码基本是这样的datagridview的datasource是一个datatable,现在datagridview可以正常显示datatable里面的数据,

现在我要双击一个单元格 弹出一个窗体,窗体显示出该单元格的所在字段和单元格的值,我现在修改这个值,怎么能让

datatable里的数据也随之变化 并且datagirdeivw立即显示出来

...全文
180 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianliang1 2010-01-12
  • 打赏
  • 举报
回复
给你写成那样了,你还不会更新数据库吗?只需要添加几句sql语句更新一下就ok了
HELLOWORDC 2010-01-11
  • 打赏
  • 举报
回复
datagirdeivw很自然就显示出来 ,关键是怎样同时也改了datatable
HELLOWORDC 2010-01-11
  • 打赏
  • 举报
回复
mark
Valefish 2010-01-11
  • 打赏
  • 举报
回复
更新数据再读出来不就可以了?
无刷新?
  • 打赏
  • 举报
回复
你把Scott Mitchell的asp.net2.0编程教程看一遍吧。不看(并且自己也写不出),怎么上岗工作?
tianliang1 2010-01-11
  • 打赏
  • 举报
回复
看了哈你的代码,帮你重新写了一个,其实你的没有必要重新去更新数据表,如果你一定要更新,也可以,自我给你的From2的关闭事件里写。。。
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.IO;
using System.Collections;
using System.Data .SqlClient ;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

private string strCurrentValue;
/// <summary>
/// 用以保存当前单元格的值
/// </summary>
public string StrCurrentValue
{
get { return strCurrentValue; }
set { strCurrentValue = value; }
}

/// <summary>
/// 主窗体构造函数
/// </summary>
public Form1()
{
InitializeComponent();

}

/// <summary>
/// 封装Form1的dataGridView
/// </summary>
public DataGridView Dgv
{
get { return this.dataGridView1; }
}

/// <summary>
/// 为dataGridView添加数据源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("id");
table.Columns.Add("name");

DataRow row = table.NewRow();
row["id"] = "007";
row["name"] = "hello world";

table.Rows.Add(row);
DataRow row1 = table.NewRow();
row1["id"] = "008";
row1["name"] = "nihao";

table.Rows.Add(row1);
this.dataGridView1.DataSource = table;

}

/// <summary>
/// 双击单元格内容时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
strCurrentValue = dataGridView1.CurrentCell.Value.ToString ();
int row = this.dataGridView1.CurrentCell.RowIndex;
int col = this.dataGridView1.CurrentCell.ColumnIndex;
Form2 frm = new Form2(strCurrentValue,row ,col,this );
frm.Show();

}

}
}

from2,修改窗口
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;

namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private string txtValue;
Form1 frm = null;
int row;
int col;
public Form2(string str,int row,int col,Form1 frm)
{
this.row = row;
this.col = col;
txtValue = str;
InitializeComponent();
this.frm=frm;
}

private void Form2_Load(object sender, EventArgs e)
{
this.textBox1.Text = txtValue;
}

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{


}

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
frm.Dgv.Rows[row].Cells[col].Value = this.textBox1.Text;
//若你必须更新数据表,就在这里开始写sql语句。。。
}

}
}
killaxing 2010-01-11
  • 打赏
  • 举报
回复
1楼的就可以了 不必这么麻烦
象8楼那样,麻烦。
tianliang1 2010-01-11
  • 打赏
  • 举报
回复
楼主说了半天还不是就是想修改你点击单元格的的值,一定要用弹出一个窗体来实现吗?又一定要去修改dataTable吗?
notlikeGaoShou 2010-01-11
  • 打赏
  • 举报
回复
你可以将Activated引用load事件来,它会及时显示你的数据的,
美福种田伯 2010-01-11
  • 打赏
  • 举报
回复
很简单的,具体的思路如下:

步骤A:父页面绑定数据源;
步骤B:父页面双击后打开子页面;
步骤C:在子页面做的任何修改都保存到数据库;
步骤D:关闭子页面后创新刷新父页面,回到步骤A。

这里面一个核心技术就是绑定浏览器的窗口关闭事件,然后调用父窗口的window.reload()方法。
tan124 2010-01-11
  • 打赏
  • 举报
回复
return ;
wonderful131 2010-01-11
  • 打赏
  • 举报
回复

test2 t2=new test2();
t2.ShowDialog();
if (t2.DialogResult == DialogResult.Yes)
{
bind();//重新绑定数据
}
else
{
return;
}
深海之蓝 2010-01-11
  • 打赏
  • 举报
回复
参考 form 间 通信
网上查一下,很多的
不懂装懂 2010-01-11
  • 打赏
  • 举报
回复
新手路过,自己写了下很罗嗦,拿出来晒晒!
窗体1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace mytest
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private string a = "";
public void seta(string _a)
{
a = _a;
}
private string b = "";
public void setb(string _b)
{
b = _b;
}
private string c = "";
public void setc(string _c)
{
c = _c;
}
private string d = "";
public void setd(string _d)
{
d = _d;
}
private string id = "";
public void setid(string _id)
{
id = _id;
}
private DataTable dt1 = null;
public void setdt(DataTable _dt1)
{
dt1 = _dt1;
}
DataTable dt = new DataTable();

private void button1_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["a"] = textBox1.Text;
dr["b"] = textBox2.Text;
dr["c"] = textBox3.Text;
dr["d"] = textBox4.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
}



private void Form5_Load(object sender, EventArgs e)
{
if (id == "")
{
dt.TableName = "aaa";
dt.Columns.Add("a");
dt.Columns.Add("b");
dt.Columns.Add("c");
dt.Columns.Add("d");
}
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellCancelEventArgs e)
{

int i = dataGridView1.CurrentRow.Index;
string i1 = i.ToString();
string a1 = dt.Rows[i][0].ToString();
string b1 = dt.Rows[i][1].ToString();
string c1 = dt.Rows[i][2].ToString();
string d1 = dt.Rows[i][3].ToString();
Form6 fr = new Form6();
fr.seta(a1);
fr.setb(b1);
fr.setc(c1);
fr.setd(d1);
fr.setid(i1);
fr.setdt(dt);
this.Visible = false;
fr.ShowDialog();
}

private void Form5_VisibleChanged(object sender, EventArgs e)
{
if (id != "")
{
dt1.Rows[Convert.ToInt32(id)][0] = a;
dt1.Rows[Convert.ToInt32(id)][1] = b;
dt1.Rows[Convert.ToInt32(id)][2] = c;
dt1.Rows[Convert.ToInt32(id)][3] = d;
dataGridView1.DataSource = dt1;
}
//}
}
}
}

窗体2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace mytest
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private string a = "";
public void seta(string _a)
{
a = _a;
}
private string b = "";
public void setb(string _b)
{
b = _b;
}
private string c = "";
public void setc(string _c)
{
c = _c;
}
private string d = "";
public void setd(string _d)
{
d = _d;
}
private string id = "";
public void setid(string _id)
{
id = _id;
}
private DataTable dt = null;
public void setdt(DataTable _dt)
{
dt = _dt;
}
private void Form6_Load(object sender, EventArgs e)
{
textBox1.Text = a;
textBox2.Text = b;
textBox3.Text = c;
textBox4.Text = d;
}

private void button1_Click(object sender, EventArgs e)
{
string q1 = textBox1.Text;
string w1 = textBox2.Text;
string e1 = textBox3.Text;
string r1 = textBox4.Text;
string i = id;


Form5 fr = new Form5();
fr.seta(q1);
fr.setb(w1);
fr.setc(e1);
fr.setd(r1);
fr.setid(i);
fr.setdt(dt);
fr.Visible = true;
this.Close();
}
}
}
润滑剂 2010-01-11
  • 打赏
  • 举报
回复
给点具体的代码啊
打转的风铃 2010-01-11
  • 打赏
  • 举报
回复
重新赋值datatable,
并且重新绑定datagridview!
hanzhaoever 2010-01-11
  • 打赏
  • 举报
回复
在弹出窗体时将datetable传到本窗体的文本框中加入触发事件 然后修改datatable[,]相应的行列将datetable传回到dataGridview页面 绑定
LoveLife_Go 2010-01-11
  • 打赏
  • 举报
回复
int rIndex = dgv.CurrentRow.Index;
int cIndex = dgv.CurrentCell.ColumnIndex;
LoveLife_Go 2010-01-11
  • 打赏
  • 举报
回复
弹出窗体里:public string a=你修改过的值;

dt.Rows[rIndex][cIndex] =弹出窗体.a ;
lsj_zrp 2010-01-11
  • 打赏
  • 举报
回复
datagridview绑定了datatable,只要你修改了datatable里面的值,datagridview也会随之变化
你弹出窗口的时候,获得该信息在datatable里面的行号,关闭窗口的时候修改datatable对应的值就行了
加载更多回复(1)

111,120

社区成员

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

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

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