C# DataGridView 捕获用户行选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1027360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView capturing user row selection
提问by SO User
I am having trouble handling the selections in DataGridView
.
My grid view contains an amount column. There is a textbox on the form which should display the total amount of the selected grid view rows. Hence I need to capture events when the user selects/ deselects the gridview rows and calculate (add/ subtract) the amount accordingly. I have found two methods of doing it:
我在处理DataGridView
. 我的网格视图包含一个金额列。表单上有一个文本框,应显示所选网格视图行的总数。因此,当用户选择/取消选择 gridview 行并相应地计算(加/减)数量时,我需要捕获事件。我找到了两种方法:
Using the
RowEnter
andRowLeave
events. These work fine when user selects/ deselects a single row. However, when the user is selecting multiple rows at one go, the event gets fired only for the last row. Hence, from my total amount only the amount in the last row gets added/ subtracted. Thus making my result erroneous.Using the
RowStateChanged
event. This works for multiple rows. However, the event gets fired event if the user scrolls through the datagrid.
使用
RowEnter
和RowLeave
事件。当用户选择/取消选择单行时,这些工作正常。但是,当用户一次选择多行时,事件只会为最后一行触发。因此,从我的总金额中,只有最后一行的金额被添加/减去。从而使我的结果错误。使用
RowStateChanged
事件。这适用于多行。但是,如果用户滚动数据网格,则该事件会被触发。
Has anyone handled such a scenario. I would like to know which datagrid event I should be using, so that my code executes only when user selects/ deselects rows including multiple rows.
有没有人处理过这种情况。我想知道我应该使用哪个 datagrid 事件,以便我的代码仅在用户选择/取消选择包括多行的行时执行。
采纳答案by SO User
Found the solution. I can use RowStateChanged
and run my code only if StateChanged
for the row is Selected
...
找到了解决办法。RowStateChanged
只有当StateChanged
该行是Selected
...时,我才能使用和运行我的代码
private void dgridv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
// For any other operation except, StateChanged, do nothing
if (e.StateChanged != DataGridViewElementStates.Selected) return;
// Calculate amount code goes here
}
回答by BFree
You can use your first method (row enter row leave) along with the SelectedRowsproperty. Meaning, when you detect those events, and you need to calculate, instead of using the row from the event args, loop through the SelectedRows and get your total.
您可以将第一种方法(行输入行离开)与SelectedRows属性一起使用。意思是,当您检测到这些事件时,您需要计算而不是使用事件参数中的行,而是遍历 SelectedRows 并获得总数。
回答by Shriram panchal
You can simply capture this in following way, but it is restricted to single row selection only:
您可以通过以下方式简单地捕获它,但仅限于单行选择:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("Selected row is=" + e.RowIndex);
// you can perform (any operation) delete action on selected row like
dataGridView1.Rows.RemoveAt(e.RowIndex);
dataGridView1.Refresh();
}
回答by franxdj
I think you can consider the SelectionChanged event:
我认为您可以考虑 SelectionChanged 事件:
private void DataGridView1_SelectionChanged(object sender, EventArgs e) {
textbox1.Text = DataGridView1.SelectedRows.Count.ToString();
}
回答by Kiquenet
I use SelectionChangedevent or CellValueChangedevent:
我使用SelectionChanged事件或CellValueChanged事件:
dtGrid.SelectionChanged += DataGridView_SelectionChanged;
this.dtGrid.DataSource = GetListOfEntities;
dtGrid.CellValueChanged += DataGridView_CellValueChanged;
private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = dtGrid.Rows[e.RowIndex];
SetRowProperties(row);
}
private void DataGridView_SelectionChanged(object sender, EventArgs e)
{
var rowsCount = dtGrid.SelectedRows.Count;
if (rowsCount == 0 || rowsCount > 1) return;
var row = dtGrid.SelectedRows[0];
if (row == null) return;
ResolveActionsForRow(row);
}
回答by Alex Pandrea
You can use SelectionChangedevent and retrieve the row index of the selected cell(s):
您可以使用SelectionChanged事件并检索所选单元格的行索引:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0)
Do_Something_With_It(dataGridView1.SelectedCells[0].RowIndex);
}