0

i've got a problem while trying to get some values through the DataGridViewRow. the problem is my cell index is 7 and when i try to get the data from the cell using this code:

 foreach (DataGridViewRow r in this.mydatagrid.Rows)
        {
            if (Convert.ToBoolean(r.Cells[0].Value) == true)
            {
                DataGridViewComboBoxCell cc = (DataGridViewComboBoxCell)r.Cells[7];
                cc.Value = toolStripComboBox1.SelectedItem.ToString();
                edit_subject(Convert.ToInt32(r.Cells[1].Value), r.Cells[7].Value.ToString());
            }

an Exception Error Comes Saying That Cells[7] is OutOfIndex . so i've tried to change the cell index from 7 to 5 and it's worked sometimes , and sometimes no .

please i need explain because it's really weird.

DLeh
  • 23,806
  • 16
  • 84
  • 128
redcode
  • 1
  • 1
  • It starts counting at 0. So index 7 is the 8th cell. There aren't 8 cells in your data grid apparently. – phpmeh Apr 14 '15 at 17:54
  • It will help if you set a breakpoint on your `edit_subject` line, and see what `r.Cells[1].Value` is equal to. This will help you understand the ordering of your cells. As @phpmeh said, you probably don't have a cell at index 7 if there are only 7 values in your row then the highest cell index will be 6. – dub stylee Apr 14 '15 at 17:57
  • 1
    Show us the markup of your control, then we would see if actually there are 7 cells – Enrique Zavaleta Apr 14 '15 at 18:11
  • @phpmeh i've tried to debug in so many ways , it's 7 cells starting by the 0 of course . my datagrid contains 8 cells , 7 are textboxcell and the last is comboboxcell. when i said it's worked by changing the index to 5 that's mean there's something wrong with the cells indexing because the correct index for the comboboxcell is 7 . but in somehow it became 5. note: i've used same cells with correct index in same program but different code and it's working fine i've checked the value , whenever it's worked i got the right value when ever it didn't i got the wrong value from the wrong cell. – redcode Apr 14 '15 at 18:21

1 Answers1

0

ok i found a solution, the problem caused because my SQL Syntax (The Datagrid Datasource) has different order than the datagridview columns, i didn't get it because the datagrid autofill has been disabled . and i've set the DataPropertyName for each column manually.

anyway , thanks for your response.

redcode
  • 1
  • 1