| |

VerySource

 Forgot password?
 Register
Search
Author: yefeng619

Column width setting of DataGrid control in VC.NET !!

[Copy link]

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-6 22:15:01
| Show all posts
I have encountered this problem before. First of all, you should use the following methods to add CColumn and Ccolumns class methods in VS: (This method of adding caused your problem)
Solution, program name -> right-click -> add class -> MFC class in ActiveX control -> add pop-up "Add class from ActiveX control wizard, then select "file" -> fill in the location of the control you added .Ocx file (C:\WINDOWS\system32\MSDATGRD.OCX), here I just added two classes CColumn and CColumns.
Note: The Chocolate and Chocolate classes added in this way will not only change the function name, but also have various problems when using it, even the most basic Chocolate cols=m_grid.GetItem(ColeVariant(long(0))); The compilation fails, and it is more difficult due to the changes of many function names.
After researching, I can successfully use all the functions in the CColumn and Ccolumns classes by using the following method without errors

We can first add the Microsoft DataGrid Control Version (OLEDB) control in VC++, the IDE will automatically add the required functions to us, copy the header files and source files of the added Chocolate and Chocolates to our VS project directory, and They can be used normally after being added to the project, and there will be no problems in compiling!

I believe the above answer can solve your problem.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-6 22:30:01
| Show all posts
Haha, your second problem happened to be solved by me, here is the solution:

The database I use is ACCESS. After checking a lot of information, I realized that ACCESS uses JET SQL, while SQL SERVER uses T-SQL. There is a big difference. JET SQL is quite bad, and many do not support it. I originally wanted to use declare Come to declare the cursor to solve, who knows that ACCESS does not recognize

In the end, I had no choice but to choose the method of generating a temporary table. JET SQL does not support generating a temporary table, so I can only generate a table, and deleting it later is the same.

I use VS + MFC

The main methods are:

1. First determine whether the table tempTable to be generated already exists, if it exists, delete it first (this step is mainly to prevent abnormalities)

2. Then generate a temporary table tempTable, and store the query results in it

3. Add an ID column to this temporary table and set its attribute to "automatic numbering"

4. Finally, this temporary table is passed to the DATAGRID control to display

The code and description are as follows:

m_ptrRecordset-> CursorLocation = adUseClient; //Very important, if not, the data cannot be displayed

m_ptrConnection-> CursorLocation = adUseClient;

CString temp_s=L"";



long l=0;

_variant_t NO;

temp_s.Format(L"SELECT Count(*) AS RTab FROM MSysObjects WHERE (((MSysObjects.Name) Like\"%s\"));",L"tempTable");

//This SQL statement is used to determine whether the tempTable table already exists, and the following is to delete it if it exists

m_ptrRecordset=m_ptrConnection->Execute(_bstr_t(temp_s),COleVariant(l),adCmdText);

NO=m_ptrRecordset->GetCollect(COleVariant(long(0)));

if(NO.vt==VT_I4) //First judge whether there is a tempTable table, if it exists, delete it first

{

     if(NO.lVal>0)

     {

         m_ptrConnection->Execute(_bstr_t(L"drop table tempTable"),COleVariant(l),adCmdText);

     }

}





temp_Str.Format(L"select Main.FileName ,Main.InningsNum ,Main.Date ,Main.ItemAttribute INTO tempTable from Main where Main.Date between #%s# and #%s# ORDER BY Main.Id",startTime,endTime );

m_ptrConnection->Execute(_bstr_t(temp_Str),COleVariant(l),adCmdText);

//This SQL statement is to generate a new table tempTable from the query results and save it in the newly generated table



m_ptrConnection->Execute(_bstr_t(L"alter table tempTable add id Counter "),COleVariant(l),adCmdText);

//This statement modifies the newly generated tempTable table and adds a new column named ID to it. The attribute is automatic numbering, namely Counter

//The following article contains the attribute values ​​of all columns in ACCESS

//http://hi.baidu.com/djiznew/item/0faef5eb8ddbede3fa42ba8c



if(m_ptrRecordset->GetState()==adStateOpen) //Determine whether the data set is open, if open, close

{

     m_ptrRecordset->Close();

}



HRESULT hr = m_ptrRecordset->Open(_bstr_t(L"select id as [serial number],FileName as [file name],InningsNum as [round time],Date as [match time],ItemAttribute as [item attribute] from tempTable order by id"), _variant_t((IDispatch*)m_ptrConnection, TRUE),adOpenStatic, adLockBatchOptimistic, adCmdUnknown);

if(hr != S_OK)

{

     AfxMessageBox(L"create recordset error!");

     return;

}

            

m_dataGrid.putref_DataSource((LPUNKNOWN)m_ptrRecordset); //Realize the connection of CRecordset with our DATAGRID control

m_dataGrid.Refresh(); // The m_dataGrid variable is the variable associated with the DATAGRID control



this->m_ptrConnection->Execute(_bstr_t(L"drop table tempTable"),COleVariant(l),adCmdText);

//After connecting to DATAGRID, delete the tempTable table





There are problems:

Since the msysobjects object is not open to the outside world by default, that is, it cannot be modified, so we must modify some options before we can achieve its operation, otherwise it will be executed

temp_s.Format(L"SELECT Count(*) AS RTab FROM MSysObjects WHERE (((MSysObjects.Name) Like\"%s\"));",L"tempTable");

This statement gives an error

Solution:

Just add the update operation to msysobjects in the admin's authority.
The default situation is that admin has no authority to msysobjects! Include query
under access 2000

Tools -> Options -> View -> Hidden Objects, System Objects
Check the previous one.

Tools -> Security -> User and group permissions, select "Table" for object type
Select MSysObjects in the object name, and set its permissions in Permissions.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list