Link to home
Start Free TrialLog in
Avatar of Jam9974
Jam9974

asked on

DatagridviewComboBoxCell

I have a issues dealing with a comboBox cell in a datagrid view.   It works fine on the first drop drop down selection but errors out on all other rows drop down selections.

I need the drop down box which is the second column to referance the first column in each row in order to retrieve the proper combobox item list which is generated from an SQL query.

After once the second combobox calls the drop down event I get the following error

"DataGridViewComboBoxCell value is not valid"

any help pointing me in the right direction would help.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LabData
{
    public partial class DataEntryMicro : Form
    {
        DatabaseConn GetItem = new DatabaseConn();
        MillConn Formula = new MillConn();
        DataGridViewTextBoxColumn Item = new DataGridViewTextBoxColumn();
        DataGridViewComboBoxColumn ItemTest = new DataGridViewComboBoxColumn();
        DataGridViewTextBoxColumn LotCodeDate = new DataGridViewTextBoxColumn();
        DataGridViewComboBoxColumn MyLocation = new DataGridViewComboBoxColumn();
        DataGridViewTextBoxColumn TestValue = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn Estimation = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn BatchTime = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn BatchID = new DataGridViewTextBoxColumn();
        int row = 0;
        string myItem = "";
        public DataEntryMicro()
        {
            InitializeComponent();
            this.KeyPreview = true;
        }
        private void DataEntryMicro_Load(object sender, EventArgs e)
        {
           
            Item.Name = "Item Number";
            Item.Width = 77;
            Item.HeaderText = "Item Number";
            Item.DataPropertyName = "Item Number";
            ItemTest.Name = "Test";
            ItemTest.Width = 130;
            ItemTest.HeaderText = "Test";
            ItemTest.DataPropertyName = "Test_Name";

            LotCodeDate.Name = "LotCode";
            LotCodeDate.Width = 70;
            LotCodeDate.HeaderText = "Lot Code Date";
            LotCodeDate.DataPropertyName = "Lot Code";
            MyLocation.Name = "Location";
            MyLocation.Width = 60;
            MyLocation.HeaderText = "Location";
            MyLocation.DataPropertyName = "Location";
            MyLocation.Items.AddRange("01","02","03","06","07");
            TestValue.Name = "Test Value";
            TestValue.Width = 100;
            TestValue.HeaderText = "Test Value";
            TestValue.DataPropertyName = "Test Value";
            Estimation.Name = "Micro Est.";
            Estimation.Width = 80;
            Estimation.HeaderText = "Micro Est.";
            Estimation.DataPropertyName = "Micro Est.";
            BatchTime.Name = "Batch Time";
            BatchTime.Width = 50;
            BatchTime.HeaderText = "Batch Time";
            BatchTime.DataPropertyName = "Batch Time";
            BatchID.Name = "Batch ID";
            BatchID.Width = 50;
            BatchID.HeaderText = "Batch ID";
            BatchID.DataPropertyName = "Batch ID";
            dataGridView1.Columns.Add(Item);
            dataGridView1.Columns.Add(ItemTest);
            dataGridView1.Columns.Add(LotCodeDate);
            dataGridView1.Columns.Add(MyLocation);
            dataGridView1.Columns.Add(TestValue);
            dataGridView1.Columns.Add(Estimation);
            dataGridView1.Columns.Add(BatchTime);
            dataGridView1.Columns.Add(BatchID);
            dataGridView1.Columns[2].DefaultCellStyle.Format = "M/d/yyyy HH:mm:ss.fff";
            dataGridView1.Columns[2].ValueType = typeof(System.DateTime);
            dataGridView1.Columns[6].DefaultCellStyle.Format = "t";
            dataGridView1.Columns[6].ValueType = typeof(System.DateTime);
            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
             string SQL;
            SQL = "select Distinct Users.UserID, Objects.ObjectName, RoleAccess.Access, Screen.ScreenName from SecurityRole ";
            SQL += " inner join Users on Users.UserID = SecurityRole.PKUserID ";
            SQL += " inner join Role on Role.roleID = SecurityRole.PKRoleID ";
            SQL += " inner join RoleAccess on RoleAccess.RoleID = SecurityRole.PKRoleID ";
            SQL += " inner join Objects on Objects.PKID = RoleAccess.ObjectID ";
            SQL += " inner join Screen on Screen.ScreenID=Objects.ScreenID ";
            SQL += " where users.LoginName = '" + GetItem.UserID + "' and Objects.ScreenID = '3'";
            SqlConnection MyAccess = new SqlConnection(GetItem.MyConn);
            MyAccess.Open();
            DataSet DSMyAccess = new DataSet();
            SqlCommand commMyAccess = new SqlCommand(SQL, MyAccess);
            commMyAccess.CommandType = CommandType.Text;
            SqlDataAdapter adapterMyAccess = new SqlDataAdapter(commMyAccess);
            adapterMyAccess.Fill(DSMyAccess);
            DataTable DTMyAccess = DSMyAccess.Tables[0];
            for (int i = 0; i < (int)DTMyAccess.Rows.Count; i++)
            {
                switch (DTMyAccess.Rows[i][1].ToString())
                {
                    case "dataGridView1":
                        if (Convert.ToInt32(DTMyAccess.Rows[i][2]) == 0)
                            dataGridView1.ReadOnly = true;
                        else
                            dataGridView1.ReadOnly = false;
                        break;
                    default:
                        break;
                }
            }
        }
        private void dataGridView1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 1)
            {
                
                
                // Check box column
                ComboBox mycomboBox = e.Control as ComboBox;
               if(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value != null)
                 mycomboBox.DropDown += new EventHandler(comboBox_DropDown);               
            }
        }
        private void ComboBoxCell_Enter(object sender, EventArgs e)
        {
          row = dataGridView1.CurrentCell.RowIndex;
          if (dataGridView1.Rows[row].Cells[0].Value != null)
          {
              myItem = dataGridView1.Rows[row].Cells[0].Value.ToString();
              myItem = "Select Test_Name from Specification_Tests where Parent_Item = '" + myItem + "' and Test_Type <> 'STANDARD'";
          }
        }
        private void comboBox_DropDown(object sender, EventArgs e)
        {
            
            row = dataGridView1.CurrentCell.RowIndex;
            if (dataGridView1.Rows[row].Cells[0].Value != null)
            {
                myItem = dataGridView1.Rows[row].Cells[0].Value.ToString();
                myItem = "Select Test_Name from Specification_Tests where Parent_Item = '" + myItem + "' and Test_Type <> 'STANDARD'";
                            }
            SqlConnection GetTests = new SqlConnection(GetItem.MyConn);
            GetTests.Open();
            DataSet DSGetTests = new DataSet();
            SqlCommand SqlGetTests = new SqlCommand(myItem, GetTests);
            SqlGetTests.CommandType = CommandType.Text;
            SqlDataAdapter AdapterGetTests = new SqlDataAdapter(SqlGetTests);            
            DataTable TableGetTests = new DataTable();
            TableGetTests.Locale = System.Globalization.CultureInfo.InvariantCulture;
            AdapterGetTests.Fill(DSGetTests);
            if (DSGetTests.Tables[0].Rows.Count > 0)
            {
                ItemTest.DataSource = bindingSource1;
                bindingSource1.DataSource = DSGetTests;
                bindingSource1.DataMember = DSGetTests.Tables[0].TableName;
                ItemTest.ValueMember = "Test_Name";
                ItemTest.DisplayMember = ItemTest.ValueMember;
            }
            GetTests.Close();
           }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jam9974
Jam9974

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jam9974
Jam9974

ASKER

Solved the problem.