Search code examples
c#winformsdatatabledatagridviewpagination

How to disable mutiselect option in datagrid in c#


How to disable mutiselect option in datagrid with datatables in c# ? Actually I have two tables A and B now I'm gonna select a value from table A and click on down arrow button to move the selected row from table A to table B

Now for first time, when i select a row in table A and click on down arrow button selected row fails to add but when i try to the same operation its working fine ..

Here is my code snippet -- Here very first time when i select a row and click on down arrow button it pop up me the :: No rows selected! and after that it works perfectly ..

private void btnSelect_Click(object sender, EventArgs e)
        {          
            dgvFormFieldsView.MultiSelect = false;        
            DataGridViewSelectedRowCollection selectedRows = dgvFormFieldsView.SelectedRows;
            dgvFormFieldsView.ClearSelection();

            if (selectedRows.Count == 0)
            {
            MessageBox.Show("No rows selected!", "Warning");
            return;
            }

            for (int i = selectedRows.Count -1 ; i >= 0; i--)
            {
                    string fieldLabel = null;
                    string fieldType = null;
                    string tabOrder = null;

                    tabOrder = (string)selectedRows[i].Cells[0].Value;
                    fieldLabel = (string)selectedRows[i].Cells[1].Value;
                    fieldType = (string)selectedRows[i].Cells[2].Value;

                    DataRow newRow = selectedFieldsTable.NewRow();
                    newRow["Field Name"] = fieldLabel;
                    newRow["Field Type"] = fieldType;

                    /*Temp Table*/
                    DataRow newRows = TempTable.NewRow();
                    newRows["Field Name"] = fieldLabel;
                    newRows["Field Type"] = fieldType;

                    if (!selectedFieldsTable.Rows.Contains(new System.Object[] { fieldType, fieldLabel }))
                    {
                       selectedFieldsTable.Rows.Add(newRow);
                       acc = selectedFieldsTable.Rows.Count;
                       temprow = TempTable.Rows.Count;
                       if (temprow < 5)
                       {
                           TempTable.Rows.Add(newRows);
                           //Console.WriteLine(counter);
                           currenttemptablecounter = currenttemptablecounter + 1;
                           Console.WriteLine(currenttemptablecounter);
                       }
                       if (temprow >= 5)
                       {
                           NextSelect.Enabled = true;
                       }
                    }
                    else
                    {
                          MessageBox.Show("Form Field :" + fieldLabel + " already selected", "PDF Perform Info");
                    }
            }
            dgvSelectedFieldsView.DataSource = TempTable;
            dgvSelectedFieldsView.ClearSelection();
            applyFormattingSelectedFieldsTable();
         }

What am I doing wrong?


Solution

  • you must use the MultiSelect Property if the DataGridView, set to false (like you did), but in the initalization of the window (or in the designer), not in the select event.