Solved

DatagridviewComboBoxCell

Posted on 2011-02-14
2
833 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:Jam9974
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
Jam9974 earned 0 total points
ID: 34888144
ok I got it to work.

Here is what I did

I created an array of Datasets and an array of data tables.
then I assigned the array demention equal to that of the row in the datagridview. This way each and ever combobox in the datagridview can have its own dataset and table.

I am guessing the reason I was getting the errors was the fact that the actual datasource linked to the first combobox was being cleared or changed and therefore became a null dataset for that row.
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[100];
            DSGetTests[row] = new DataSet();
            DataTable[] MyTable = new DataTable[100];
            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[row]);
            MyTable[row] = new DataTable();
            MyTable[row] = DSGetTests[row].Tables[0];
            if (MyTable[row].Rows.Count > 0)
            {
                ItemTest.DataSource = bindingSource1;
                bindingSource1.DataSource = MyTable[row];
                ItemTest.ValueMember = "Test_Name";
                ItemTest.DisplayMember = ItemTest.ValueMember;
            }
            GetTests.Close();

Open in new window

0
 

Author Closing Comment

by:Jam9974
ID: 34888150
Solved the problem.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question