以下代码用于填充 DGV:
private void frmSwitch_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'newCityCollectionDataSet.PropertyInformation' table. You can move, or remove it, as needed.
this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);
// TODO: This line of code loads data into the 'newCityCollectionDataSet.ClientTable' table. You can move, or remove it, as needed.
this.clientTableTableAdapter.Fill(this.newCityCollectionDataSet.ClientTable);
}
此代码允许我将必要的信息传递给“摘要表单”:
private void propertyInformationDataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
System.Data.DataRowView SelectedRowView;
newCityCollectionDataSet.PropertyInformationRow SelectedRow;
SelectedRowView = (System.Data.DataRowView)propertyInformationBindingSource.Current;
SelectedRow = (newCityCollectionDataSet.PropertyInformationRow)SelectedRowView.Row;
frmSummary SummaryForm = new frmSummary(this);
SummaryForm.LoadCaseNumberKey(SelectedRow.CaseNumberKey, true, null);
SummaryForm.LoadBRTNumberKey(SelectedRow.BRTNumber, null);
SummaryForm.Show();
}
我要做的是传递 SelectedRow 并在当前 SelectedRow 不再有效时加 1 以转到下一行(例如,在“摘要表单”上选中 FileFinishedCheckBox 时)。我还希望在选中 DataGridview 上的复选框时发生同样的事情,这样人们就不必滚动回他们正在处理的文件。
在需要时执行刷新的代码如下:
public void PerformRefresh()
{
this.propertyInformationBindingSource.EndEdit();
this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);
this.propertyInformationDataGridView.Refresh();
}
任何帮助都会很棒。
最佳答案
这个问题好像分为两部分:
- 如何在两个窗体之间进行通信
- 如何更改数据 GridView 中的选定行
完成这两项任务有许多不同的方法,所以我只给你两个可行的方法。第一个(对于 Windows 窗体)是最简单的,而第二个(用于更改所选行)在我看来是正确的方法。
窗口窗体之间的通信
在两个窗口窗体之间进行通信的最直接的方法是将对一个窗体的引用传递给另一个窗体。
假设您有 Form1 打开 Form2,您可以这样做:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 f = new Form2(this);
f.Show();
}
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
}
public partial class Form2 : Form
{
private Form1 _parentForm;
public Form2(Form1 parentForm)
{
InitializeComponent();
_parentForm = parentForm;
_parentForm.ShowMessage("I am a message from form1);
}
}
因此,在您的示例中,您将向父表单添加一个方法,该方法将在 dgv3 中选择的要在 gdv1 中显示的行的唯一值作为其参数。在方法中(它是 parentForm 的一个成员,你放置了我将在下面显示的居中代码)。
这样做的其他方法包括将委托(delegate)传递给子窗体,这是使数据 GridView 居中的方法。这样做的好处是您不再受限于总是传递 Form1,甚至可以提供不同的操作来响应复选框,但实现起来稍微复杂一些。
以 DataGridView 中选定的记录为中心
我的首选方法是使用绑定(bind)源为网格提供数据源。您也可以使用 CurrentCell 属性直接访问网格位置,但使用绑定(bind)源可以让您物有所值。
在下面的代码中,我们有一个创建 BindingSource 的表单,将其数据源设置为 MyBindingList 类型的 BindingList,然后将绑定(bind)源设置为 datagridview 的数据源。
BindingList 中的对象有一个独特的属性“PrimaryKey”,允许我们找到它们。
然后我展示居中代码,其实很简单。
首先我们通过调用绑定(bind)源的Find()
方法获取所需绑定(bind)源中的索引。
其次我们更改绑定(bind)源位置(这也会更新 datagridview 显示)。
最后,我们更改 datagridview 的 FirstDisplayedScrollingRowIndex,以便所选行不在网格的最顶部或底部(如果您使用此行,您将需要添加检查以确保这是一个有效索引)。
public partial class Form1 : Form
{
BindingSource bs;
public Form1()
{
InitializeComponent();
bs = new BindingSource();
MyBindingList<BackingObject> backing_objects = new MyBindingList<BackingObject>();
backing_objects.Add(new BackingObject{ PrimaryKey = 1, Name = "Fred", Hidden = "Fred 1"});
bs.DataSource = backing_objects;
dataGridView1.DataSource = bs;
}
private void button1_Click(object sender, EventArgs e)
{
int index = bs.Find("PrimaryKey", 5);
bs.Position = index;
dataGridView1.FirstDisplayedScrollingRowIndex = index - 1;
}
}
现在最后要注意的是开箱即用的绑定(bind)列表不支持绑定(bind)源的 Find()
方法。这就是我使用自定义 MyBindingList 的原因。可以找到实现它的代码 here .
基本上你需要一个像下面这样的类:
public class MyBindingList<T> : BindingList<T>
{
protected override bool SupportsSearchingCore
{
get
{
return true;
}
}
protected override int FindCore(PropertyDescriptor prop, object key)
{
// Get the property info for the specified property.
PropertyInfo propInfo = typeof(T).GetProperty(prop.Name);
T item;
if (key != null)
{
// Loop through the items to see if the key
// value matches the property value.
for (int i = 0; i < Count; ++i)
{
item = (T)Items[i];
if (propInfo.GetValue(item, null).Equals(key))
return i;
}
}
return -1;
}
}
关于c# - 如何转到最后单击的数据 GridView 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812551/