adding columns to datagrid control at design time
I know this must be simple, but I can't figure out how to add columns to my datagrid control. I provided it with a data source and I am able to set the two default columns that it gives me to two columns from the data source, but I can't see how to add the rest of the columns. I started out using DBGrid and was able to add columns by right clicking in the control and inserting columns. When I tried this in the datagrid control I only get the properties sheet.
thanks in advance,
Tracy
Re: adding columns to datagrid control at design time
There is a bug with the datagrid control using it with Visual C++ although microsoft maitains this is by design.
You need to add the columns programmatically which is tedious but doable. PRB: Cannot Manipulate Columns of MS DataGrid in Visual C++ at Design Time
Check out http://support.microsoft.com/support/kb/articles/ for
Q225065 on Microsoft's support site. I have include the code for the teminally lazy.
Next...
--------------------------------------------------------------------------------
The information in this article applies to:
Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
--------------------------------------------------------------------------------
SYMPTOMS
Microsoft DataGrid version 6.0 does not support modifications to the columns of Grid at design time when using it in Visual C++.
RESOLUTION
You can manipulate the columns of DataGrid programmatically at run time. The sample code below shows how to add/remove a column, and modify the column's caption when using ADO data control and DataGrid with an MFC CVCDataGridDlg class.
Insert the following variables and member function prototypes in the corresponding dialog class declaration:
CDataGrid m_Grid; // DataGrid variable.
CAdodc m_DataControl; // ADO Data Control variable.
void OnAdd( ) // Function for adding a column.
void OnRemove( ) // Function for removing a column.
void OnModify( ) // Function for modifying a column.
Sample Code
//implementation
#include "columns.h" // CColumns collection object header file.
#include "column.h" // CColumn object header file.
#include "_recordset.h" // C_Recordset object header file.
#include "fields.h" // CFields collection object header file.
#include "field.h" // CFields object header file.
#include <comdef.h>
// Adding a column.
void CVCDataGridDlg::OnAdd()
{
//Get the total number of columns.
short i = (short)(m_DataGrid.GetColumns().GetCount());<BR/>
//Insert the new column.
m_DataGrid.GetColumns().Add(i);
CString rCaption;
GetDlgItemText(IDC_EDIT2,rCaption);
//Set the caption of the column.
m_DataGrid.GetColumns().GetItem(_variant_t(i)).SetCaption((LPCTSTR)rCaption);
m_DataGrid.Refresh();
}
// Removing a column.
void CVCDataGridDlg::OnRemove()
{
m_DataGrid.GetColumns().Remove( _variant_t((long) atoi(m_Edit.GetBuffer(m_Caption.GetLength()) )));
m_DataGrid.Refresh();
}
// Modifying a column caption.
void CVCDataGridDlg::OnModify()
{
CString rCaption, rColNum;
GetDlgItemText(IDC_EDIT1,rColNum);
GetDlgItemText(IDC_EDIT2,rCaption);
m_DataGrid.GetColumns().GetItem(_variant_t((long) atoi(rColNum))).SetCaption((LPCTSTR) rCaption);<BR/>
m_DataGrid.Refresh();
}