Hi, I'm using VS2008 C++. I've used the form template provided. I've tried before this simplified example to add a data source but am not sure exactly how to get it to work. I have two columns in a DataGrid, one TextBoxColumn and the second a ComboBoxColumn. Basically, I would like to understand how to get data to appear in the ComboBoxColumn in a few different ways. I would like to add permanent values to the DataGridViewComboBoxColumn such as: option01 option02 option03 as fixed so they will automatically be in each drop down box. Then, each item entered in the program column which is a DataGridViewTextBoxColumn will also be added to the DataGridViewComboBoxColumn so that they appear in every other drop down box. So, if in the first row, myProgram01 is entered, the next row's second column will have option01, option02,... and myProgram01 as a choice in it. I will attach the code below for just form1.h, the rest is basically just set by the template. Thank you.
////////// form1.h /////////
#pragma once
namespace Cerulean_IO_Sheet {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::DataGridView^ dataGridView1;
protected:
private: System::Windows::Forms::DataGridViewTextBoxColumn^ cerProgram;
private: System::Windows::Forms::DataGridViewComboBoxColumn^ relation1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
this->cerProgram = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
this->relation1 = (gcnew System::Windows::Forms::DataGridViewComboBoxColumn());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->BeginInit();
this->SuspendLayout();
//
// dataGridView1
//
this->dataGridView1->AllowUserToOrderColumns = true;
this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^ >(2) {this->cerProgram,
this->relation1});
this->dataGridView1->Location = System::Drawing::Point(12, 12);
this->dataGridView1->Name = L"dataGridView1";
this->dataGridView1->Size = System::Drawing::Size(268, 150);
this->dataGridView1->TabIndex = 0;
//
// cerProgram
//
this->cerProgram->HeaderText = L"Program";
this->cerProgram->Name = L"cerProgram";
//
// relation1
//
this->relation1->HeaderText = L"Relationship 1";
this->relation1->Name = L"relation1";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(31)),
static_cast<System::Int32>(static_cast<System::Byte>(145)));
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->dataGridView1);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->Name = L"Form1";
this->Text = L"Cerulean";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
| | Elizabeth Elizabeth Tuesday, September 29, 2009 10:09 AM | I am not a VC++ programmer, but I think you should use CellValueChanged event. Following code is in VC# but I think you will be able to understand the logic through which you can get the required result
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
string textValue = (string)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
DataGridViewComboBoxCell combo =(DataGridViewComboBoxCell) dataGridView1.Rows[e.RowIndex].Cells[1];
if (combo.Items.Contains(textValue) == false)
{
combo.Items.Add(textValue);
}
}
}
Gaurav Khanna - Marked As Answer byLing WangMSFT, ModeratorTuesday, October 06, 2009 2:19 AM
- Unmarked As Answer byElizabeth Elizabeth Tuesday, October 06, 2009 8:34 AM
- Marked As Answer byElizabeth Elizabeth Tuesday, October 06, 2009 8:45 AM
-
| | Khanna Gaurav Tuesday, September 29, 2009 11:13 AM | The following is VC version.
private: System::Void dataGridView1_CellValueChanged(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e)
{
if(e->ColumnIndex == 0 && e->RowIndex >=0)
{
String^ textValue = dataGridView1->Rows[e->RowIndex]->Cells[0]->Value->ToString();
DataGridViewComboBoxCell^ combo = (DataGridViewComboBoxCell^)(dataGridView1->Rows[e->RowIndex]->Cells[1]);
if(combo->Items->Contains(textValue) == false)
{
combo->Items->Add(textValue);
}
}
Best regards, Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byElizabeth Elizabeth Tuesday, October 06, 2009 8:46 AM
-
| | Ling Wang Monday, October 05, 2009 10:08 AM | I am not a VC++ programmer, but I think you should use CellValueChanged event. Following code is in VC# but I think you will be able to understand the logic through which you can get the required result
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
string textValue = (string)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
DataGridViewComboBoxCell combo =(DataGridViewComboBoxCell) dataGridView1.Rows[e.RowIndex].Cells[1];
if (combo.Items.Contains(textValue) == false)
{
combo.Items.Add(textValue);
}
}
}
Gaurav Khanna - Marked As Answer byLing WangMSFT, ModeratorTuesday, October 06, 2009 2:19 AM
- Unmarked As Answer byElizabeth Elizabeth Tuesday, October 06, 2009 8:34 AM
- Marked As Answer byElizabeth Elizabeth Tuesday, October 06, 2009 8:45 AM
-
| | Khanna Gaurav Tuesday, September 29, 2009 11:13 AM | Hi Gaurav, I have inserted the following code but have some errors still, which I think have to do with C++ syntax in the translation. If you or anyone else has any advice, I think I'm declaring .cells and .value as a member function instead of a value.
private: System::Void dataGridView1_CellContentClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
if (e->ColumnIndex == 0 && e->RowIndex >= 0)
{
std::string textValue = (std::string)dataGridView1->Rows[e->RowIndex].Cells[0].Value;
DataGridViewComboBoxCell combo =(DataGridViewComboBoxCell) dataGridView1->Rows[e.RowIndex].Cells[1];
if (combo.Items.Contains(textValue) == false)
{
combo.Items.Add(textValue);
}
}
}
My errors are as follows:
1>c:\...\Form1.h(306) : error C2228: left of '.Cells' must have class/struct/union
1> type is 'System::Windows::Forms::DataGridViewRow ^'
1> did you intend to use '->' instead?
1>c:\...\Form1.h(306) : error C2228: left of '.Value' must have class/struct/union
1>c:\...\Form1.h(308) : error C2228: left of '.RowIndex' must have class/struct/union
1> type is 'System::Windows::Forms::DataGridViewCellEventArgs ^'
1> did you intend to use '->' instead?
1>c:\...\Form1.h(308) : fatal error C1903: unable to recover from previous error(s); stopping compilation
| | Elizabeth Elizabeth Tuesday, September 29, 2009 11:59 AM | The following is VC version.
private: System::Void dataGridView1_CellValueChanged(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e)
{
if(e->ColumnIndex == 0 && e->RowIndex >=0)
{
String^ textValue = dataGridView1->Rows[e->RowIndex]->Cells[0]->Value->ToString();
DataGridViewComboBoxCell^ combo = (DataGridViewComboBoxCell^)(dataGridView1->Rows[e->RowIndex]->Cells[1]);
if(combo->Items->Contains(textValue) == false)
{
combo->Items->Add(textValue);
}
}
Best regards, Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byElizabeth Elizabeth Tuesday, October 06, 2009 8:46 AM
-
| | Ling Wang Monday, October 05, 2009 10:08 AM |
|