Question

I managed to get it to work (I believe), however I am noticing an error when switching between the drop downs between rows.

See below for example:

Row 1 drop down contains {A, E, F}
Row 2 drop down contains {B, C, D, A}

The displayed value in both rows is A. I use the dropdown in row 1 fine and correctly, then use the dropdown in row 2 fine and correctly. When i go back to the dropdown in row 1 i get an error stating that the index[3] (which is the index of A in row 2) is out of range.

Apparently i cannot post images sub 10 rep, so ill try to recreate the error message exactly.

' The following exception occurred in DataGridView:

System.ArguementOutOfRangeException: InvalidArguement = Value of '8' is not a valid for 'SelectedIndex'. Parameter name: SelectedIndex at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value) at System.Windows.Forms.ListControl.DataManager_ItemChanged(Object sender, ItemChangedEventArgs e) at System.Windows.Forms.ListControl.SetDataConnection(Object newDataSource, BindingMemberInfo newDisplayMember, Booleanforce) ... .. . '

I have looked all over and cannot figure this problem out, please see code posted below.

    private void assignResourceGrid_CellBeginEdit_1(object sender, DataGridViewCellCancelEventArgs e)
    {
        if(assignResourceGrid.Columns["rscNameComboBox"].Index == e.ColumnIndex && e.RowIndex != -1)
        {
            resourceList.Clear();

            string currentRowTeam = assignResourceGrid.Rows[e.RowIndex].Cells[assignResourceGrid.Columns["PersistentTeam"].Index].Value.ToString();
            string currentRowDept = assignResourceGrid.Rows[e.RowIndex].Cells[assignResourceGrid.Columns["NAME"].Index].Value.ToString();

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            sql = "select distinct rsc.ID, \"ResourceName\" from rsc "
            + "inner join  persistentteamassign on rsc.id = persistentteamassign.rsc_id "
            + "where persistentteamassign.persistentteam_id = (select id from persistentteam where teamname = :currentTeam "
            + "AND department_id = (select id from departmentteam where name = :currentDept)) "
            + "AND rsc.\"ResourceName\" is not null";
            cmd = new OracleCommand(sql, conn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("currentTeam", currentRowTeam);
            cmd.Parameters.Add("currentDept", currentRowDept);


            dreader = cmd.ExecuteReader();
            if (dreader.HasRows == true)
            {
                while (dreader.Read())
                resourceList.Add(new Resource
                {
                    id = Convert.ToInt64((dreader["ID"])),
                    name = (dreader["ResourceName"].ToString())
                });
            }
            DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(assignResourceGrid.Rows[e.RowIndex].Cells[assignResourceGrid.Columns["rscNameComboBox"].Index]);
            cell.DataSource = null;
            cell.Items.Clear();
            cell.DataSource = resourceList;
            cell.ValueMember = "name";
            cell.DisplayMember = "name";
        }

Note:
1. The DataGridView is being databound by an sql query.
2. These 3 columns mentioned in the code are each bound to a column in the database.
3. I am then applying the custom drop downs on a per row basis once a user begins typing the resource name.

The values in rscNameComboBox should be dependant on the combination of items in PersistentTeam & Name columns. All rows will have a common displayed value. This is because a person can be on multiple teams, but the team has different members.

Était-ce utile?

La solution

For anyone having a similar problem I found a solution to dynamically binding each row. Whatever the reason I do not receive an error by pulling 'all' of the data into a table then just using rowfilter and binding each cell dynamically based on that filtered table. This is achieved either through type ahead or using the dropdown.

    private void assignResourceGrid_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        DataGridViewComboBoxCell cellRscName = (DataGridViewComboBoxCell)(assignResourceGrid.Rows[e.RowIndex].Cells["RscName"]);

        if (assignResourceGrid.Columns["RscName"].Index == e.ColumnIndex && e.RowIndex != -1)
        {
                deptName = assignResourceGrid.Rows[e.RowIndex].Cells["DeptTeamName"].Value.ToString()
                teamName = assignResourceGrid.Rows[e.RowIndex].Cells["PersistentTeamName"].Value.ToString()

                //show all resources on each team
                resourceData.Tables[0].DefaultView.RowFilter = "DepartmentName= '" + deptName+ "' "
                + "and TeamName= '" + teamName + "' ";

                cellRscName.DataSource = resourceData.Tables[0];
                cellRscName.DisplayMember = "ResourceName";
                cellRscName.ValueMember = "ResourceName";
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top