Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DatagridviewComboBoxCell

Posted on 2011-02-14
2
Medium Priority
?
851 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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