我的一个父窗体中有一个DataGridView,里面是绑定的一个学生表Student的数据,现在的程序是,双击DataGridView中的一行,
便弹出一个可以修改该行学生详细信息的模式窗体,修改完成后,点保存,模式窗体关闭,此时要DataGridView更新,我的处理方法是重新绑定数据,但有一个很严重的问题,DataGridView中的数据很多,我修改的那行在DataGridView中间,垂直滚动条也在中间,我修改完成窗体关闭后,DataGridView的滚动条又回到了最顶上,也就是第一行,这样肯定是不方便的,我怎么处理,才能使得我修改的是哪条记录,关闭窗体后,滚动条不动,并且DataGridView中的这行记录也是修改后的新数据呢?
这是修改,还有删除记录,也有同样的问题,删除后了重新给DataGridView绑定数据,滚动条又到了最顶上。
请大家帮帮我!
便弹出一个可以修改该行学生详细信息的模式窗体,修改完成后,点保存,模式窗体关闭,此时要DataGridView更新,我的处理方法是重新绑定数据,但有一个很严重的问题,DataGridView中的数据很多,我修改的那行在DataGridView中间,垂直滚动条也在中间,我修改完成窗体关闭后,DataGridView的滚动条又回到了最顶上,也就是第一行,这样肯定是不方便的,我怎么处理,才能使得我修改的是哪条记录,关闭窗体后,滚动条不动,并且DataGridView中的这行记录也是修改后的新数据呢?
这是修改,还有删除记录,也有同样的问题,删除后了重新给DataGridView绑定数据,滚动条又到了最顶上。
请大家帮帮我!
<%@ Page MaintainScrollPositionOnPostBack="true" %>
1.记录修改数据行KEY
2.关闭子窗口后重新绑定数据
3.查找数据行并显示此行,包括位置二.基于JS脚本
1.记录修改数据行KEY
2.关闭子窗口后定位此数据行
3.采用JS+DHTML动态更新每一列的数据:TableID.rows[x].cells[y].innerText=value;第一种方法适合数据量较小的环境,第二种方法适合表格显示,编辑弹出的方式
to:rangeon,你说的重定位是怎么搞的,有效果吗?
大家遇到过这个问题吗?
我给grid控件数据的方法就是:
先从数据库中取得表Student的记录给datatable1,
再datagrid1.DataSource = datatable1;
我修改完一条记录后,关闭窗口,这时我要grid控件中的数据是修改后的数据,
就再做了一次:
从数据库中取得表Student的记录给datatable1,
再datagrid1.DataSource = datatable1;
这时,grid控件中的数据是更新了,但是滚动条到了最上面,就是这个意思。
public static event SendMessage SendEvent;
public static void DoSendMessage(Cls_BookDeatil c)
{
SendEvent(c);
}
form1: private void Form1_Load(object sender, EventArgs e)
{
Class1.SendEvent+=new Class1.SendMessage(this.GetItem);
}
private void GetItem(Cls_BookDeatil c)
{
this.dataGridView1.Rows.Add(c.BookId, c.BookName, c.Count);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.ShowDialog();
}
form2: private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Cls_BookDeatil c = new Cls_BookDeatil();
c.BookId = textBox1.Text;
c.BookName = textBox2.Text;
c.Count = textBox3.Text; Class1.DoSendMessage(c);
}
this.BindingContext[datatable1].EndCurrentEdit();
//在子窗体中修改DataGrid的数据//主窗体代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace ZZ
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet ds;
private System.Windows.Forms.Button button1;
//标识当前行
private int dataGridCurrentIndex;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
ds = new DataSet("MyDataSet");
InitData(ds);
this.dataGrid1.DataSource = this.ds;
this.dataGrid1.DataMember = this.ds.Tables[0].TableName;
this.dataGridCurrentIndex = this.dataGrid1.CurrentRowIndex;
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 20);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(340, 160);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(264, 196);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "修改";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(372, 229);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion
//模拟数据输入
private void InitData(DataSet ds)
{
DataTable dt = new DataTable("TabeHost");
DataColumn dc = new DataColumn("IpAddress",typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("UserName",typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Password",typeof(string));
dt.Columns.Add(dc);
ds.Tables.Add(dt);
DataRow dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.229";
dr["UserName"] = "zhzuo";
dr["Password"] = "zhengzuo";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.230";
dr["UserName"] = "11";
dr["Password"] = "12";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.231";
dr["UserName"] = "123";
dr["Password"] = "12";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.232";
dr["UserName"] = "22";
dr["Password"] = "788";
dt.Rows.Add(dr);
} private void button1_Click(object sender, System.EventArgs e)
{
DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
Form2 form2 = new Form2(drv);
form2.ShowDialog(); } private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
if(this.dataGridCurrentIndex != this.dataGrid1.CurrentRowIndex)
{
this.dataGridCurrentIndex = this.dataGrid1.CurrentRowIndex;
DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
Form2 form2 = new Form2(drv);
form2.ShowDialog();
}
}
}
}//子窗体代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace ZZ
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private DataRowView drv;
private System.ComponentModel.Container components = null; public Form2(DataRowView dr)
{
InitializeComponent();
this.drv = dr;
this.textBox1.Text = (string)drv["IpAddress"];
this.textBox2.Text = (string)drv["UserName"];
this.textBox3.Text = (string)drv["Password"];
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(28, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(28, 56);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 4;
this.textBox2.Text = "textBox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(28, 92);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 5;
this.textBox3.Text = "textBox3";
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 6;
this.button1.Text = "保存";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(236, 152);
this.button2.Name = "button2";
this.button2.TabIndex = 7;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(324, 201);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
drv["IpAddress"] = this.textBox1.Text;
drv["UserName"] = this.textBox2.Text;
drv["Password"] = this.textBox3.Text;
} private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}