C# 如何转到datagridview中上次单击的行
以下代码用于填充DGV:C# 如何转到datagridview中上次单击的行,c#,winforms,datagridview,C#,Winforms,Datagridview,以下代码用于填充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.propertyInf
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);
}
此代码允许我将必要的信息传递到“摘要表单”:
如果当前SelectedRow不再有效(例如,在“摘要表单”上选中FileFinishedCheckBox),我要做的是传递SelectedRow并添加1以转到下一行。我还希望在DataGridview上选中复选框时发生同样的事情,这样人们就不必滚动回他们正在处理的文件
需要时执行刷新的代码如下所示:
public void PerformRefresh()
{
this.propertyInformationBindingSource.EndEdit();
this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);
this.propertyInformationDataGridView.Refresh();
}
任何帮助都会很好。这个问题似乎分为两部分:
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的一个成员,您将中心代码放入其中,我将在下面显示)
其他方法包括向子窗体传递委托,子窗体是datagridview的中心方法。这样做的好处是,您不再拘泥于总是传递Form1,甚至可以根据复选框提供不同的操作,但实现起来稍微复杂一些
以DataGridView中的选定记录为中心
我的首选方法是使用bindingsource为网格提供数据源。您也可以使用CurrentCell属性直接访问网格位置,但是使用bindingsource,您可以获得更大的收益
在下面的代码中,我们有一个表单,它创建一个BindingSource,将其数据源设置为MyBindingList类型的BindingList,然后将绑定源设置为datagridview的数据源
BindingList中的对象具有唯一的属性“PrimaryKey”,允许我们查找它们
然后我展示了定心代码,它实际上非常简单
首先,通过调用绑定源的Find()
方法,我们在绑定源中获得所需的索引
其次,我们更改绑定源的位置(这也会更新datagridview显示)
最后,我们更改datagridview的FirstDisplayedScrollingRowIndex,使所选行不在网格的顶部或底部(如果使用此行,您将需要添加一个检查以确保这是一个有效的索引)
对不起,我不明白重点。我甚至没有一个聪明的问题可以问,因为我完全不明白你的目标是什么。你能澄清一下吗?当我双击DGV中的一条记录时,它会弹出一个包含该记录的窗口。在该屏幕上,它有一个复选框,单击该复选框将关闭记录。DGV需要反映窗口关闭时的情况,然后滚动回同一区域,以便无需向下滚动到该区域即可选择下一条记录。例如,选择1000条记录中的456条。用户关闭该记录并关闭窗口。我希望DGV刷新,这将从当前查询中删除该记录,并转到包含记录455457458459等的视图。另一种解决方法是在DGV上而不是表单上放置复选框。我的问题是,我不知道如何使用DGV上的复选框将值传递到单独的表(即如何使用DGV上的事件?)。我需要将值传递给服务器上的windows服务用来向客户端发送报告的表。
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);
}
}
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;
}
}
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;
}
}