Click here to Skip to main content
15,909,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 25-Apr-18 1:31am
v3
Comments
Richard MacCutchan 25-Apr-18 7:01am    
Just check the row number when the user tries to select one. If it is anything other than the first row then show an error message.
Member 13765884 25-Apr-18 7:07am    
I thought that is what I have done already
Richard MacCutchan 25-Apr-18 7:12am    
I don't quite understand your code. Why are you checking the Count instead of the row number?
F-ES Sitecore 25-Apr-18 7:18am    
if (productionDataGridView.SelectedRows.Count <= 1)
{
// this
}
else if (productionDataGridView.SelectedRows.Count == 0)
{
// that
}

"that" will never be called because if productionDataGridView.SelectedRows.Count is 0 then it will go into "this". Basic debugging will show you these things.

Um.
if (productionDataGridView.SelectedRows.Count <= 1)
{
    MessageBox.Show("Please select the first item in list");
}
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.
 
Share this answer
 
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)[^]

C#
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
}


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)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900