how does WxGrid work ?

im new in wxWidgets, its interesting for me, but can anyone tell me how to with wxGrid, this is part of mydialog class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class maydialogDlg : public wxDialog
{
	private:
		DECLARE_EVENT_TABLE();
		
	public:
		maydialogDlg(wxWindow *parent, wxWindowID id = 1, const wxString &title = wxT("maydialog"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = maydialogDlg_STYLE);
		virtual ~maydialogDlg();
		void WxButton1Click(wxCommandEvent& event);
		void maydialogDlgLeftDclick(wxMouseEvent& event);
		void MyTableCellLeftClick(wxGridEvent& event);
		void MyTableSelectCell(wxGridEvent& event);
	private:
		//Do not add custom control declarations between 
		//GUI Control Declaration Start and GUI Control Declaration End.
		//wxDev-C++ will remove them. Add custom code after the block.
		////GUI Control Declaration Start
		wxTextCtrl *WxEdit2;
		wxButton *WxButton1;
		wxGrid *MyTable;
		wxTextCtrl *WxEdit1;
		////GUI Control Declaration End
		
	private:
		//Note: if you receive any error with these enum IDs, then you need to
		//change your old form code that are based on the #define control IDs.
		//#defines may replace a numeric value for the enum names.
		//Try copy and pasting the below block in your old form header files.
		enum
		{
			////GUI Enum Control ID Start
			ID_WXEDIT2 = 1003,
			ID_WXBUTTON1 = 1004,
			ID_MYTABLE = 1002,
			ID_WXEDIT1 = 1001,
			////GUI Enum Control ID End
			ID_DUMMY_VALUE_ //don't remove this value unless you have other enum values
		};
	
	private:
		void OnClose(wxCloseEvent& event);
		void CreateGUIControls();
};


this is implementation of WxButton1click, and its work fine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void maydialogDlg::WxButton1Click(wxCommandEvent& event)
{
    int SellectedRow;
    wxString Message;
    SellectedRow = MyTable->GetGridCursorRow();
    Message = "Selected Row : ";Message<<SellectedRow;
    wxMessageBox(Message);
    MyTable->SetCellValue(SellectedRow,0,WxEdit1->GetLineText(1));
    MyTable->SetCellValue(SellectedRow,1,WxEdit2->GetLineText(1));
    MyTable->SelectRow(SellectedRow++);
    MyTable->SetGridCursor(SellectedRow++,1);
    WxEdit1->Clear(); WxEdit2->Clear();
	WxEdit1->SetFocus();
}

im stuck with this
1
2
3
4
5
6
void maydialogDlg::MyTableSelectCell(wxGridEvent& event)
{
   int selected = MyTable->GetSelectedRows()[0];
   wxString data; data<<selected;

}

it says that GetSelectedRows is wxArrayInt, but int selected = MyTable->GetSelectedRows()[0]; cause an Error;

As you say GetSelectedRows returns a wxArrayInt - so you should not be using a plain int.
Given that we will get an array of integers of which rows are selected then I think you should
loop through the rows and get the cell values for each rows into the data string ( I dont see a wx function to get the whole row data in one go)

So your function might look something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void maydialogDlg::MyTableSelectCell(wxGridEvent& event)
{
   wxString data;
   
   wxArrayInt rowsSelected = MyTable->GetSelectedRows();
   for (size_t rowCount =0; rowCount < rowsSelected.getCount(); rowCount++)
   {
        for (int colCount =0; colCount < myTable->GetNumberCols(); colCount++)
        {
            //get the data for each cell in the row
            data << myTable->GetCellValue(rowCount, colCount) << "  ";
        }
        data << "\n"; //add a new line to end of each row data.
    }
            
    //Do what you want with the data string
}



I'm not an expert on wxwidgets though.
I did the same thing, im still stuck there. i gotten error. aka doenst work.
You took a long time.

And your answer is not very helpful.
What doesn't work??
What's your code now??

Come on - if you want help make yourself useful.
closed account (3pj6b7Xj)
I only use wxWidgets to make dialog boxes but since I don't like the coding style, I just design the dialog box and rob/steal (^_^) the co-ordinates of the controls and implement it in my own code, lol.
Last edited on
this is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "wx/wx.h" 
#include <wx/grid.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/dynarray.h>

WX_DEFINE_ARRAY_INT(int, ArrayInt);


class MyDialogDlgApp : public wxApp
    {
        public:
            bool OnInit();
            int OnExit();
    };
#define MyDialogDlg_STYLE wxCAPTION | wxSYSTEM_MENU | wxDIALOG_NO_PARENT | wxMINIMIZE_BOX | wxCLOSE_BOX

class MyDialogDlg : public wxDialog
    {
        private:
            DECLARE_EVENT_TABLE();
            wxGrid *WxGrid1;
			wxStaticText *T_Alamat;
			wxStaticText *T_Nama;
			wxTextCtrl *WxEdit2;
			wxTextCtrl *WxEdit1;  
            
        public:
        	MyDialogDlg(wxWindow *parent, wxWindowID id = 1, const wxString &title = wxT("mygrid1"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = MyDialogDlg_STYLE);
			virtual ~MyDialogDlg();
			void WxEdit1Enter(wxCommandEvent& event);
			void WxEdit2Enter(wxCommandEvent& event);
			void WxGrid1SelectCell(wxGridEvent& event);
            enum
			{
				////GUI Enum Control ID Start
				ID_WXGRID1 = 1005,
				ID_T_ALAMAT = 1004,
				ID_T_NAMA = 1003,
				ID_WXEDIT2 = 1002,
				ID_WXEDIT1 = 1001,
				////GUI Enum Control ID End
				ID_DUMMY_VALUE_ //don't remove this value unless you have other enum values
			};

        private:
			void OnClose(wxCloseEvent& event);
			void CreateGUIControls();
    }; 
//----------event table
BEGIN_EVENT_TABLE(MyDialogDlg,wxDialog)
	EVT_CLOSE(MyDialogDlg::OnClose)	
	EVT_GRID_SELECT_CELL(MyDialogDlg::WxGrid1SelectCell)
	EVT_TEXT_ENTER(ID_WXEDIT2,MyDialogDlg::WxEdit2Enter)
	EVT_TEXT_ENTER(ID_WXEDIT1,MyDialogDlg::WxEdit1Enter)
END_EVENT_TABLE()

//------------implement
IMPLEMENT_APP(MyDialogDlgApp)
bool MyDialogDlgApp::OnInit()
    {
        MyDialogDlg* dialog = new MyDialogDlg(NULL);
        SetTopWindow(dialog);
        dialog->Show(true);
        return true;
    } 

int MyDialogDlgApp::OnExit()
    {
        return 0;
    }
 

MyDialogDlg::MyDialogDlg(
						wxWindow *parent, 
						wxWindowID id, 
						const wxString &title, 
						const wxPoint &position, 
						const wxSize& size, 
						long style
						)
: wxDialog(parent, id, title, position, size, style)
{
	CreateGUIControls();
}

MyDialogDlg::~MyDialogDlg()
{
} 

 void MyDialogDlg::CreateGUIControls()
{
	//Do not add custom code between
	//GUI Items Creation Start and GUI Items Creation End.
	//wxDev-C++ designer will remove them.
	//Add the custom code before or after the blocks
	////GUI Items Creation Start

	WxGrid1 = new wxGrid(this, ID_WXGRID1, wxPoint(16, 83), wxSize(480, 289), wxRAISED_BORDER);
	WxGrid1->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Segoe UI")));
	WxGrid1->SetDefaultColSize(200);
	WxGrid1->SetDefaultRowSize(25);
	WxGrid1->SetRowLabelSize(20);
	WxGrid1->SetColLabelSize(25);
	WxGrid1->CreateGrid(12,2,wxGrid::wxGridSelectRows);

	T_Alamat = new wxStaticText(this, ID_T_ALAMAT, wxT("Alamat"), wxPoint(18, 49), wxDefaultSize, wxALIGN_LEFT, wxT("T_Alamat"));

	T_Nama = new wxStaticText(this, ID_T_NAMA, wxT("Nama"), wxPoint(17, 21), wxDefaultSize, wxALIGN_LEFT, wxT("T_Nama"));

	WxEdit2 = new wxTextCtrl(this, ID_WXEDIT2, wxT("WxEdit1"), wxPoint(90, 49), wxSize(188, 23), wxTE_PROCESS_ENTER, wxDefaultValidator, wxT("WxEdit2"));

	WxEdit1 = new wxTextCtrl(this, ID_WXEDIT1, wxT("WxEdit1"), wxPoint(91, 16), wxSize(188, 23), wxTE_PROCESS_ENTER, wxDefaultValidator, wxT("WxEdit1"));

	SetTitle(wxT("mygrid1"));
	SetIcon(wxNullIcon);
	SetSize(198,44,538,421);
	Center();
	
	////GUI Items Creation End
	WxEdit1->Clear();
	WxEdit2->Clear();
	WxEdit1->SetFocus();
}


	
void MyDialogDlg::OnClose(wxCloseEvent& /*event*/)
    {
		wxMessageBox("Tutup Aja yach");
        Destroy();
    }
    
void MyDialogDlg::WxEdit1Enter(wxCommandEvent& event)
    {
        WxEdit2->SetFocus();
    }

/*
 * WxEdit2Enter
 */
void MyDialogDlg::WxEdit2Enter(wxCommandEvent& event)
{
    static int row = WxGrid1->GetGridCursorRow();
    WxGrid1->SetCellValue(row,0,WxEdit1->GetLineText(1));
    WxGrid1->SetCellValue(row,1,WxEdit2->GetLineText(1));
    WxGrid1->SetGridCursor(row++,0);
    WxEdit1->Clear();
    WxEdit2->Clear();
    WxEdit1->SetFocus();
}

/*
 * WxGrid1SelectCell
 */
void MyDialogDlg::WxGrid1SelectCell(wxGridEvent& event)
{
    wxArrayInt myint;
    wxString mystring;
    myint = WxGrid1->GetSelectedRows();
    mystring<<myint.GetCount();
    //wxMessageBox(mystring); will show 0
	if(WxGrid1->IsInSelection(2,0))
	   {
            wxMessageBox("2.0 selected");
        }
        else
        {
            wxMessageBox("2.0 not selected");
        }
}
Topic archived. No new replies allowed.