0

I have a DatagridviewCombobox Column and am creating DatagridviewCombobox Cells on each row and adding items to it. When I change the value of any (combobox) cell, it throws an exception saying that Datagridviewcombobox cell value is not valid. and the cell value becomes '1'.

I am working on the datagridview_currentcelldirtystatechange event, but haven't been able to make it work.

The code below is creating rows and filling combobox cells with a sequence numbers.

    int _rowLimit =1;

    for (int i = _rowLimit - 1; i < _rowLimit; i++)
    {          
         datagridview.Rows.Add();
         item = i + 1;
         datagridview[myColumn, i].Value = _rowLimit;                   

         DataGridViewComboBoxCell oCell = datagridview.CurrentRow.Cells[myColumn] as DataGridViewComboBoxCell;
         oCell.Items.Add(item);

         ((DataGridViewComboBoxColumn)datagridview.Columns[myColumn]).Items.IndexOf(_rowLimit);
         ((DataGridViewComboBoxColumn)datagridview.Columns[myColumn]).Items.Insert(index, item);
    }

And below is what i am doing in datagridview_currentcelldirtystatechange event:

for (int innerIndex = 0; innerIndex < datagridview.Rows.Count; innerIndex++)
    {
      long sequence = 3;
      long oldSequence = 2;
      long tempValue= Convert.ToInt64(datagridview.Rows[innerIndex].Cells[myColumn].Value);
      if (tempValue <= sequence && tempValue> oldSequence)
      {                    
           datagridview.Rows[innerIndex].Cells[myColumn].Value = tempValue+ 1; // increment   the sequence 
// value here i am getting is correct , but it doesn't show in the DatagridviewCombobox cell where it gets changed of gridview and the mentioned exception is thrown.

      }

Any help would be appreciated. Thanks.

varocarbas
  • 12,354
  • 4
  • 26
  • 37
HKhan
  • 53
  • 1
  • 8
  • It seems like you need to put a bit or order in your code to know exactly what you are doing, because the current version is pretty confusing (and this is, almost for sure, what is provoking the errors you refer). In the first code, you are using various indices which are used in a more or less anarchic way; the simpler the better: use one index for coutings rows (row), a variable for total rows (maxRow), the item you would input (item, a list of values a combobox or anything you need) and a last part where the items are added to the datagridview: clear, simple and not prone-to-errors. – varocarbas Aug 16 '13 at 12:53
  • Once the populating part has been done properly, you have to access this information (for reading/editing) also orderly. Before adding a value you have to make sure that its type matches what the given cell expects/is supported. You have to make sure that each step works and intend to solve/come here to ask each new problem. The current version of your code is pretty "unfriendly", problems might be everywhere and a whole code rewriting sounds as the best thing to do. – varocarbas Aug 16 '13 at 13:00
  • Thanks for the reply. okay i will try to make it more friendly. but if you got my problem statement, can you please tell me how to get it work? actually its a simple combobox and there are sequence numbers in combobox dropdown... i want to change its value (selected index change) , on which event should i work and how this can be achieved? Thanks – HKhan Aug 16 '13 at 13:13
  • I told you: do things step by step. Let's start with your first code. You are using various variables which are not even declared. Which values is taking index? – varocarbas Aug 20 '13 at 20:27
  • index and items variables are initially zero and has global scope. (i.e int index = 0; int items = 0) datagridview[myColumn, i].Value = _rowLimit; //this is used to show the value to the combobox cell at each i'th row oCell.Items.Add(item); //Adding items in the combobox cell (i.e i+1 = 1 starting from 1) ((DataGridViewComboBoxColumn)datagridview.Columns[myColumn]).Items.IndexOf(_rowLimit); //This is just to know index of the items – HKhan Aug 21 '13 at 05:15
  • See... this is the problem: you are using variables in a different as they are supposed to be used. Index has to grow with each new item (= not being declared globally). The .IndexOf part does not make any sense on this context because you are not storing its value anywhere. Above you are doing datagridview[myColumn, i].Value = _rowLimit; which again I am not sure about its point. Let's speed up the "helping process", please, tell me a sample of the values you want to include in the combobox-type column and I will write a simple code doing that. – varocarbas Aug 21 '13 at 07:21

1 Answers1

1

the error on selectedindexChange value of the combobox cells and the exception of DataGridviewComboboxcell value is not valid .. that automatically did change the selected vlue to '1'.. i fixed this issue by adding the DatagridviewComboBoxColumn property in the designer file.

this.columnName.ValueType = typeof(long); 

typeof(long) // this is what what i wanted to show the value in the datagridviewcombobox column.

Issue has now resolved. Thanks.

HKhan
  • 53
  • 1
  • 8
  • Thanks varocarbas for your suggestions and quick replies..thanks :) – HKhan Aug 21 '13 at 07:58
  • as comboboxcell was not getting its actual matched type that is why throwing exception of "DataGridviewComboboxcell value is not valid" – HKhan Aug 21 '13 at 08:00