假定负责修改数据的窗口名叫Dialog,如下定义
public class Dialog : Form
{
private DataRow m_Row;
public Dialog(DataRow row)
{
m_Row = row;
//其他初始化代码
} public void BtnOK_Click(object sender, EventArgs e)
{
//此处修改m_Row中各字段的值
//保存到数据库
this.DialogResult = DialogResult.OK;
}
}在父窗口中采用如下方式弹出修改窗口:
DataRowView rv = (DataRowView)this.dataGridView1.BindingContext[this.dataGridView1.DataSource, this.dataGridView1.DataMember].Current;
DataRow row = rv.Row;
Dialog dlg = new Dialog(row);
if (dlg.ShowDialog() != DialogResult.OK)
{
row.CancelEdit();
}不整个过程不需要对DataGridView做任何操作
public class Dialog : Form
{
private DataRow m_Row;
public Dialog(DataRow row)
{
m_Row = row;
//其他初始化代码
} public void BtnOK_Click(object sender, EventArgs e)
{
//此处修改m_Row中各字段的值
//保存到数据库
this.DialogResult = DialogResult.OK;
}
}在父窗口中采用如下方式弹出修改窗口:
DataRowView rv = (DataRowView)this.dataGridView1.BindingContext[this.dataGridView1.DataSource, this.dataGridView1.DataMember].Current;
DataRow row = rv.Row;
Dialog dlg = new Dialog(row);
if (dlg.ShowDialog() != DialogResult.OK)
{
row.CancelEdit();
}不整个过程不需要对DataGridView做任何操作
{
Form f=new Form();
f.ShowDialog(this);
}
在窗口的文本框显示选择行的各列数据。
textbox1.Text = Convert.ToString(dataGridView [1,dataGridView.CurrentCell.RowIndex].Value).Trim();然后修改文本内容,按保存按钮,保存后窗口关闭
SqlCommandBuilder builder = new SqlCommandBuilder(已创建的DataAdapter对象);
DataAdapter对象. Update(数据集对象, "数据表名称字符串");
可以自动修改。