Solved

DatagridviewComboBoxCell

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 37
Excel file not created as expected 7 48
Replace &lt; with < 14 54
Help with C#, MVC, razor. 6 23
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 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

17 Experts available now in Live!

Get 1:1 Help Now