Example of CLI/C++ dataGridView - setting index of comobox cell list

535 Views Asked by At

I am trying to set up a dataGridView in MS CLI/C++.

Simply if I have a list of letters (Say A - D) I want to populate the dataGridView type. what I want to do is to create a for loop that will populate each cell with a Letter

like Row(0) Cell(0) with A, Row(0) Cell(1) with B, etc

I can't find a simple direct way to do it. can Someone help please

A sample of my code is below

int columns = getColumnCount();
int rows = getRowCount();

// Clear existing items
dataGridView1->Columns->Clear();
dataGridView1->Rows->Clear();

DataGridViewComboBoxColumn ^ dataGridComboColumn = gcnew DataGridViewComboBoxColumn;
DataGridViewCell ^ dataGridComboCell = gcnew DataGridViewComboBoxCell;
DataGridViewComboBoxColumn ^ dataGridComboColumn = gcnew DataGridViewComboBoxColumn;

for (int col = 0; col < columns; col++)
{
    //addItemToComboBox(dataGridComboColumn);
    dataGridComboColumn->Items->Add("A");
    dataGridComboColumn->Items->Add("B");
    dataGridComboColumn->Items->Add("C");
    dataGridComboColumn->Items->Add("D");

    dataGridView1->Columns->Add(dataGridComboColumn);
}

for (int row = 0; row < rows; row++)
{
    dataGridView1->Rows->Add(row);
}

dataGridView1->Rows[1]->Cells[1]->Value = dataGridComboColumn->Items[1];


int selectcnt = 0;
for (int row = 0; row < rows; row++)
{
     for (int col = 0; col < columns; col++)
    {
        selectcnt++;
    }
}

}

1

There are 1 best solutions below

0
lalnashi On

I found a solution to my implementation.

I have to define a temporary cell

DataGridViewComboBoxCell^ tempCell = (DataGridViewComboBoxCell^)dataGridView1->Rows[row]->Cells[col];
if (tempCell != nullptr)
{
    dataGridView1->Rows[row]->Cells[col]->Value = tempCell->Items[selectCnt];
}

and just cycle through the list This did the trick