Solved

DatagridviewComboBoxCell

Posted on 2011-02-14
2
815 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now