Problem using DataGridView from C++ - no data shown

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I am trying to use the .Net DataGridView from C++ using BindingSource and stuff like that - pretty standard - to display some records from a database. But no data are displayed
1) Ive written some examples in C# and C++ which do the same. In one version I dont bind data but just displayes an empty DataGrid with some columns. This works fine in both C# and C++. But when using binding to a database it doesnt work in C++ - works
fine in C# of course.
2) I am using Odbc - OdbcAdapter, OdbcCommandBuilder etc
3) My managed control is placed on an unmanaged form in an unmanaged class. And this works fine without databinding! The Grid is displayed, Columns are shown correctly, data can be entered and read - only, no data is shown when bound to the database.
And just to be precise: What happens is that no data from the database is displayed in the grid. When the same code written in C# is run the data is displayed correctly!
Below is my reading & binding C++ code.
Regards, Hans Olav Nymand
<div style="color:Black;background-color:White; <pre>
System::Windows::Forms::DataGridViewTextBoxColumn^ DataGridView1_IdColumn = <span style="color:Blue; gcnew System::Windows::Forms::DataGridViewTextBoxColumn();
System::Windows::Forms::DataGridViewTextBoxColumn^ DataGridView1_NameColumn = <span style="color:Blue; gcnew System::Windows::Forms::DataGridViewTextBoxColumn();

<span style="color:Green; // DataGridView1_IdColumn
DataGridView1_IdColumn->DataPropertyName = <span style="color:#A31515; "Id";
DataGridView1_IdColumn->HeaderText = <span style="color:#A31515; "Id";
DataGridView1_IdColumn->Name = <span style="color:#A31515; "DataGridView1_IdColumn";
DataGridView1_IdColumn->ReadOnly = <span style="color:Blue; true;

<span style="color:Green; // DataGridView1_NameColumn
DataGridView1_NameColumn->DataPropertyName = <span style="color:#A31515; "name";
DataGridView1_NameColumn->HeaderText = <span style="color:#A31515; "name";
DataGridView1_NameColumn->Name = <span style="color:#A31515; "DataGridView1_NameColumn";

m_dataGridView1->AllowUserToOrderColumns = <span style="color:Blue; true;
m_dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
m_dataGridView1->Name = <span style="color:#A31515; "dataGridView1";
m_dataGridView1->TabIndex = 0;

m_dataGridView1->Columns->AddRange(<span style="color:Blue; gcnew <span style="color:Blue; array<System::Windows::Forms::DataGridViewColumn^>{ DataGridView1_IdColumn, DataGridView1_NameColumn });

<span style="color:Green; ///////////////////////////////////////////////////////////////////////////////
<span style="color:Green; // Bind grid to datatable

System::String^ ConnectString = <span style="color:#A31515; "DSN=ARGOS_NT;Trusted_Connection=Yes";

<span style="color:Blue; try
{
System::ComponentModel::Container^ components = <span style="color:Blue; gcnew System::ComponentModel::Container();

System::Windows::Forms::BindingSource^ DataGridView1_BindingSource = <span style="color:Blue; gcnew System::Windows::Forms::BindingSource(components);

m_dataGridView1->DataSource = DataGridView1_BindingSource;

<span style="color:Green; // Create a new data adapter based on the specified query.
System::Data::Odbc::OdbcDataAdapter^ odbcAdapter = <span style="color:Blue; gcnew System::Data::Odbc::OdbcDataAdapter(<span style="color:#A31515; "select Id, Name from GridTest", ConnectString);

<span style="color:Green; // Create a command builder to generate SQL update, insert, and
<span style="color:Green; // delete commands based on selectCommand. These are used to
<span style="color:Green; // update the database.
System::Data::Odbc::OdbcCommandBuilder^ commandBuilder = <span style="color:Blue; gcnew System::Data::Odbc::OdbcCommandBuilder(odbcAdapter);

<span style="color:Green; // Populate a new data table and bind it to the BindingSource.
System::Data::DataTable^ table = <span style="color:Blue; gcnew System::Data::DataTable();
table->Locale = System::Globalization::CultureInfo::InvariantCulture;
odbcAdapter->Fill(table);

<span style="color:Green; // Now we actually load the data to the grid via the binding-source
DataGridView1_BindingSource->DataSource = table;

<span style="color:Green; // Resize the DataGridView columns to fit the newly loaded content.
m_dataGridView1->AutoResizeColumns(
System::Windows::Forms::DataGridViewAutoSizeColumnsMode::AllCellsExceptHeader);
}
<span style="color:Blue; catch (System::Exception^ e)
{
AfxMessageBox(CString(e->Message));
}

[/code]


View the full article
 
Back
Top