At the moment, I've created a loop which runs through every cells in my datagridview to find if any cells are selected (highlighted), and if so that cell's row/ column header backcolor changes. The problem I have with this is that once the row and column count start to become a large amount, I start experiencing lag. So I wondering does anybody know a way of instantly being able to find whether a row or column contains a selected or highlighted cell without using a loop.
目前,我创建了一个循环,它遍历我的datagridview中的每个单元格,以查找是否选择了任何单元格(突出显示),如果是,则单元格的行/列标题backcolor会发生变化。我遇到的问题是,一旦行数和列数开始变大,我就开始遇到滞后。所以我想知道有没有人知道如何在不使用循环的情况下立即查找行或列是否包含选定或突出显示的单元格。
I was hoping that something like me.datagridview1.rows(1).selectedcells.count would exist, but haven't been able to find something like that, that works. The other option (which I'd prefer not to do), is color the headercells as the selection is made.
我希望像我这样的东西.datagridview1.rows(1).selectedcells.count会存在,但是找不到那样的东西,那就行了。另一个选项(我不想这样做)是在进行选择时为headercells着色。
Here is what I am using to find the selections. I have it looping through only the displayed cells to reduce the lag (which works), but I need the headercells colored, even when the selected cells are out of view (which then doesn't work).
这是我用来查找选择的内容。我只通过显示的单元格来循环以减少延迟(这是有效的),但我需要将标题单元着色,即使选定的单元格不在视图中(然后不起作用)。
a = .FirstDisplayedCell.RowIndex
Do While a < .FirstDisplayedCell.RowIndex + .DisplayedRowCount(True)
b = .FirstDisplayedCell.ColumnIndex
Do While b < .FirstDisplayedCell.ColumnIndex + .DisplayedColumnCount(True)
If .Rows(a).Cells(b).Selected = False Then
.Rows(a).HeaderCell.Style.BackColor = colorfieldheader
.Columns(b).HeaderCell.Style.BackColor = colorfieldheader
End If
b += 1
Loop
a += 1
Loop
a = .FirstDisplayedCell.RowIndex
Do While a < .FirstDisplayedCell.RowIndex + .DisplayedRowCount(True)
b = .FirstDisplayedCell.ColumnIndex
Do While b < .FirstDisplayedCell.ColumnIndex + .DisplayedColumnCount(True)
If .Rows(a).Cells(b).Selected = True Then
.Rows(a).HeaderCell.Style.BackColor = colorfieldheaderhighlight
.Columns(b).HeaderCell.Style.BackColor = colorfieldheaderhighlight
End If
b += 1
Loop
a += 1
Loop
2 个解决方案
#1
You can do something like this C# code:
你可以做这样的C#代码:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
dataGridView1.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
Ensure that the event handler is attached after data binding.
确保在数据绑定后附加事件处理程序。
If you are highlighting the row same a selected cell (blue), you might want to set SelectionMode
as FullRowSelect
for the DataGridView
. Like this:
如果要突出显示与所选单元格相同的行(蓝色),则可能需要将SelectionMode设置为DataGridView的FullRowSelect。像这样:
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
#2
place this code in your data grid view MouseDown event ...
将此代码放在数据网格视图MouseDown事件中...
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left )
{
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
and for more info use how to highlight specific row in datagridview in c#
有关更多信息,请使用如何在c#中的数据网格视图中突出显示特定行
#1
You can do something like this C# code:
你可以做这样的C#代码:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
dataGridView1.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
Ensure that the event handler is attached after data binding.
确保在数据绑定后附加事件处理程序。
If you are highlighting the row same a selected cell (blue), you might want to set SelectionMode
as FullRowSelect
for the DataGridView
. Like this:
如果要突出显示与所选单元格相同的行(蓝色),则可能需要将SelectionMode设置为DataGridView的FullRowSelect。像这样:
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
#2
place this code in your data grid view MouseDown event ...
将此代码放在数据网格视图MouseDown事件中...
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left )
{
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
and for more info use how to highlight specific row in datagridview in c#
有关更多信息,请使用如何在c#中的数据网格视图中突出显示特定行