For example, there are two forms, Form SelectForm query information collection, using the information set DataGridview display, add and modify information EditForm form, EditForm edited and closed, positioning the row DataGridview editing SelectForm now to modify information for example, first declare an event in the form EditForm
public event EventHandler AfterEditingBillsNumber;
Then when EditForm off, or edit the information after you complete, the values you want to pass a written
AfterEditingBillsNumber(label.Text, EventArgs.Empty);
Back SelectForm form, you certainly have to write a few lines of code in SelectForm
EditForm form=new EditForm();
form.ShowDialog();
Well, now that a few lines of code into this
EditForm form=new EditForm();
form.AfterEditingBillsNumber += new EventHandler(this.AfterEditingBillsNumber );
form.ShowDialog();
In this form then write event handling method AfterEditingBillsNumber
private void AfterEditingBillsNumber(object sender, EventArgs e)
{
string focusStr = sender as string;
if (!string.IsNullOrEmpty(focusStr))
{
SelectBills();
SetFocusOnEditing(dataGridView1, "ColumnBillsNumber", "OperationsDate", focusStr);
}
}
/// <summary>
/// 设置DataGridView编辑前的焦点,写的不好,还望海涵,望大神改进。
/// <param name="dgv">DataGridView控件</param>
/// <param name="ColumnName">用于定位的列名</param>
/// <param name="VisibleCell">可见单元,大部分用于定位的列都是隐藏的,所以需要一个可见的单元格进行光标定位</param>
/// <param name="SelectedValue">用于定位的值</param>
/// </summary>
public void SetFocusOnEditing(DataGridView dgv, string ColumnName, string VisibleCell, string SelectedValue)
{
try
{
int j = 0;//计数器
foreach (DataGridViewRow dr in dgv.Rows)
{
if (dr.Cells[ColumnName].Value.ToString() == SelectedValue)
{
dgv.FirstDisplayedScrollingRowIndex = j;//将当前找到的行设置到第一行
dgv.CurrentCell = dr.Cells[VisibleCell];//去掉其他行的选择状态并将当前行的第一个单元格置为选择状态
dr.Selected = true;//选择当前整行
return;
}
j++;
}
}
catch (Exception ex)
{
MsgBox.ShowError(ex.Message);
}
}
Reproduced in: https: //my.oschina.net/dongri/blog/610896