只允许用户点击第一个datagridview行C# [英] Only allow user to click first datagridview row C#
问题描述
是否可以仅允许用户选择datagridview框中的第一项。到目前为止这是我的代码,阻止所有使用 - 包括第一行...
Is it possible to only allow the user to select the first item in the datagridview box. So far this is the code I have, blocks all use - including the first row...
private void productionDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (productionDataGridView.SelectedRows.Count <= 1)
{
MessageBox.Show("Please select the first item in list");
}
else if (productionDataGridView.SelectedRows.Count == 0)
{
productionDataGridView.CurrentRow.Cells[4].Value.ToString().Split(',').ToList().ForEach(r => listBox2.Items.Add(r.Trim()));
OrderIDTxt.Text = this.productionDataGridView.CurrentRow.Cells[0].Value.ToString();
}
}
我的尝试:
重新安排代码和谷歌 - 但是没有太多关于它的信息
What I have tried:
re arranging code and google - but theres not much information about it
推荐答案
嗯。
Um.
if (productionDataGridView.SelectedRows.Count <= 1)
{
MessageBox.Show("Please select the first item in list");
}
因此,如果他选择任何东西,无论他选择哪一行,计数都将是一个或多个...
Plus,双击并不是选择行的唯一方法:单击即可完成,单击一下,然后单击CTRL,第二行单击,或者......
相反,处理SelectionChanged事件:
So if he selects anything, the count will be one or more regardless of the row he selects...
Plus, Double click isn't the only way to select a row: a single click will do it, as will a single click followed by CTRL and a second row single click, or ...
Instead, handle the SelectionChanged event:
private void myDataGridView_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null)
{
DataGridViewSelectedRowCollection selected = dgv.SelectedRows;
if (selected.Count != 1 || selected[0].Index != 0)
{
dgv.ClearSelection();
}
}
}
并且用户无法选择除第一行以外的任何内容。
And the user can't select anything except the first row.
请阅读文档! DataGridView.SelectedRows Property(System.Windows。表格) [ ^ ]获取用户选择的行集合,但您需要找出 CurrentRow [ ^ ]是第一行(行[0 ]
)!
我建议使用此事件: DataGridView.SelectionChanged事件(System.Windows.Forms) [ ^ ]
Please, read the documentation! DataGridView.SelectedRows Property (System.Windows.Forms)[^] gets the collection of rows selected by the user, but you need to find out that CurrentRow[^] is first row (Rows[0]
)!
I'd suggest to use this event: DataGridView.SelectionChanged Event (System.Windows.Forms)[^]
if(productionDataGridView.CurrentRow != productionDataGridView.Rows[0])
{
//MessageBox.Show("Please select the first item in list");
productionDataGridView.CurrentCell = productionDataGridView.Rows[0].Cells[e.ColumnIndex]; //change selection to first row
}
详情请见:
如何:获取Windows窗体DataGridView控件中的选定单元格,行和列Microsoft Docs [ ^ ]
DataGridView.CurrentCell Property(System.Windows.Forms) [ ^ ]
DataGridViewCell类(System.Windows.Forms) [ ^ ]
For further details, please see:
How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control | Microsoft Docs[^]
DataGridView.CurrentCell Property (System.Windows.Forms)[^]
DataGridViewCell Class (System.Windows.Forms)[^]
这篇关于只允许用户点击第一个datagridview行C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!