c#Datagrid在刷新后保留Cell焦点 [英] c# Datagrid retain Cell focus after refresh

查看:102
本文介绍了c#Datagrid在刷新后保留Cell焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我正在寻找一种在刷新数据集后保留DataGridView上的单元格选择的方法。



这是我正在玩的代码是:



Hi,


I am looking for a way to retain the cell selection on the DataGridView after I refresh the data set.

This is the code I am playing with is:

 int cell1 = (this.agentsDataGridView.CurrentCell.RowIndex);
 int cell2 = (this.agentsDataGridView.CurrentCell.ColumnIndex);


this.agentsTableAdapter.Fill(assistReportsDataSet.Agents);
this.agentsDataGridView.CurrentCell = this.agentsDataGridView[cell1, cell2];





我在填充DataGridView之前获取单元格位置,然后使用两个整数设置位置。



问题是当我点击一个超过(8,8)的单元格值时出现以下错误



System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。





我可以看到单元格选择是不断跳跃所以我显然不能用这种方法来保留细胞选择。



I get the cell location before I populate the DataGridView and then I set the location after with the two integers.

The problem is that I get the following error when I click a cell value of more than (8,8)

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.


and I can see the cell selection is constantly jumping so I obviously cannot use this method to retain cell selection.

推荐答案

当我指出问题时你会笑。



这是修改后的代码(我已经更改了几个变量名以使其更清晰):

You'll laugh when I point out the problem.

Here is the modified code (I have changed a couple of your variable names to make it clearer):
int row1 = (this.agentsDataGridView.CurrentCell.RowIndex);
int col1 = (this.agentsDataGridView.CurrentCell.ColumnIndex);

this.agentsTableAdapter.Fill(assistReportsDataSet.Agents);
this.agentsDataGridView.CurrentCell = this.agentsDataGridView[col1, row1];





您是否发现了变化?



CurrentCell 属性首先要求列,然后是行。 :-D



Daft不是吗?





你的评论后:



你可能想看一下 DataGridView.SelectedCells [ ^ ] property。

您可以迭代该集合,随时重新选择它们:





Did you spot the change?

The CurrentCell property expects the column first, then the row. :-D

Daft ain't it?


After your comment:

You might want to look at the DataGridView.SelectedCells[^] property.
You can iterate over that collection, reselecting them as you go:

DataGridViewSelectedCellCollection selCells = myDGV.SelectedCells;
RefreshData();
foreach (DataGridViewCell cell in selCells)
{
  myDGV.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Selected = true;
}





我输入的内容没有Visual Studio的好处,所以请原谅任何语法错误,但它应该给你这个想法。



祝你好运! :)

[/ Edit]



I have typed that in without benefit of Visual Studio, so please forgive any syntax errors, but it should give you the idea.

Good luck! :)
[/Edit]


private void dataGridView1_CellValidating(object sender,DataGridViewCellValidatingEventArgs e)

{

int row = e.RowIndex;

int col = e.ColumnIndex;

if(row< 0 || col!= 3)

返回;

if(e.FormattedValue.ToString()。Equals(String.Empty))

{

}

其他

{

双倍数量= 0;

尝试

{

quantity = Convert.ToDouble(e.FormattedValue.ToString());

if(quantity == 0)

{

MessageBox.Show(数量不能为零,信息,MessageBoxButtons.OK,MessageBoxIcon.Information);

e.Cancel = true;

返回;

}

}

catch

{

MessageBox.Show(数量应为十进制值。,信息,MessageBoxButtons.OK,MessageBoxIcon.Information);

e.Cancel = true;

返回;

}

}

}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int row = e.RowIndex;
int col = e.ColumnIndex;
if (row < 0 || col != 3)
return;
if (e.FormattedValue.ToString().Equals(String.Empty))
{
}
else
{
double quantity = 0;
try
{
quantity = Convert.ToDouble(e.FormattedValue.ToString());
if (quantity == 0)
{
MessageBox.Show("The quantity can not be Zero", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
return;
}
}
catch
{
MessageBox.Show("The quantity should be decimal value.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
return;
}
}
}


这篇关于c#Datagrid在刷新后保留Cell焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆