how to save the particular cell value in database - infragistics

I have ultrawinGrid related to infragistics.
In my Grid I have the column name with "Area" when user enters the value in a cell row after click on save button the value should save in database please provide the code whether I can implement in this event
ex:private void syncGrid_CellChange(object sender, CellEventArgs e)
{
}

The UltraGrid is bound to a list that is in memory and that list will automatically have the updates so there is no need to handle the CellChange event of the UltraGrid. In the event handler for the button you will need to add logic that will persist the list the grid is bound to to the database.
If you are using a DataTable/DataSet, TableAdapters provide this functionality. If you are using EntityFramework, call SaveChanges on your DBContext.

Related

Finding a wxMenu's Selected Radio Item

Let's say that I have a group of radio items in a wxMenu. I know that exactly one of these will be checked at any given time.
Does the wxMenu or some other construct hold onto the index of the checked item, or do I need to call the isChecked on each radio item till I find the checked element to find it's index?
I've asked this question about how to do that, but I'd much prefer wxWidgets saved me from doing that everywhere.
No, saving the index of the last selected item (as shown in ravenspoint's answer) or using wxMenuBarBase::IsChecked() until you find the selected radio button is the only way to do it.
For wxWidgets to provide access to the currently selected button it would need not only to store it (which means not forgetting to update not only when the selected changes, but also when items are inserted into/deleted from the menu, so it's already not completely trivial), but to somehow provide access to the radio items group you're interested in, which would require being able to identify it and currently there is no way to do it and adding it isn't going to be particularly simple.
What could be done easily, however, is writing a reusable function int GetIndexOfSelectedRadioItem(int firstItem) that would start at the given item and call IsChecked() on subsequent items until it returns true and return the offset of the item. You should be able to do it in your own code, but if you'd like to include such function in wxWidgets itself (as a static wxMenuBar method, probably), please don't hesitate to send patches/pull requests doing it!
It is easy enough to roll your own.
Bind an event handler to wxEVT_COMMAND_RADIOBUTTON_SELECTED for every button. In the handler, extract the ID of the selected radio button and store it somewhere.
Like this:
ResolMenu = new wxMenu();
ResolMenu->AppendRadioItem(idRcvLoRez,"Low Resolution");
ResolMenu->AppendRadioItem(idRcvMeRez,"Medium Resolution");
ResolMenu->AppendRadioItem(idRcvHiRez,"High Resolution");
ResolMenu->Check( idRcvLoRez, true );
Bind(wxEVT_MENU,&cFrame::onRcvRez,this,idRcvLoRez);
Bind(wxEVT_MENU,&cFrame::onRcvRez,this,idRcvMeRez);
Bind(wxEVT_MENU,&cFrame::onRcvRez,this,idRcvHiRez);
void onRcvRez( wxCommandEvent& event )
{
myRezID = event.GetId();

Save the state of QCheckBox in file, and load the state when program restarts

In my GUI application, I have some labels in my mainwindow the visibility of the labels are controlled from checkboxes in a dialog which opens when a button (setting) is pressed. Now, it all works fine, i.e. if I open the settings dialog I can check or uncheck the checkboxes; consequently the labels are also set visible or invisible.
mysettingsdialog.cpp
void mysettingsdialog::onclick(bool checked) //by AJ kpi conf
{
if(myCheckBox->isChecked()==true)
{
emit setlabelvisible();
}
else
{
emit setlabelinvisible();
}
}
mainwindow.cpp
MySettingsDialog* myset=new MySettingsDialog(this);
connect(myset,SIGNAL(setlabelvisible()),this,SLOT(enable1()));
connect(myset,SIGNAL(setlabelinvisible()),this,SLOT(disable1()));
void MainWindow::enable1()
{
ui->label->setVisible(true);
qDebug()<<"VISIBLE label";
}
void MainWindow::disable1()
{
ui->label->setVisible(false);
qDebug()<<"INVISIBLE label";
}
Now the problem is, every time my application restarts it does not retain the previous state of the checkboxes. So I was thinking to save the state of the checkbox in a variable and writing it to a file, so whenever my application starts it will read the file and set the status of check box accordingly.
My question is, how can I store the "state" of checkbox in a variable and write it to file. And again use the same to set the state of checkbox ???
I mean reading / writing values from file for QLabels and QLineEdits is easy enough but I am baffled about how to do it with checkbox.
Create a container to store the pointer of each checkbox.
Create another container to store the "state" of each checkbox. For a binary check box, you can use isChecked() to query whether or not a checkbox is checked. Otherwise you can call checkState() to return the state as enum if you use a tri-state check box (see the edit).
When loading settings, assign the state to each check box accordingly.
You may use QSettings to manage the settings and save them as an ini file.
Edit
Just mention there is an option for a tri-state check box. From the document:
QCheckBox optionally provides a third state to indicate "no change".
This is useful whenever you need to give the user the option of
neither checking nor unchecking a checkbox. If you need this third
state, enable it with setTristate(), and use checkState() to query the
current toggle state.

How to create a Checkbox Change event for a wxGrid cell

I've created a wxGrid, populated it with data, and have created a column that contains checkboxes, and made them editable. All good so far.
co_Grid->SetReadOnly(at_RowCount, 24, false);
co_Grid->SetCellRenderer(at_RowCount, 24, new wxGridCellBoolRenderer);
co_Grid->SetCellEditor(at_RowCount, 24, new wxGridCellBoolEditor);
What I want to be able to do now is to add an event handler for the checkbox toggle event.
I've tried using the OnCellValueChanged event for the grid, but that only fires after the user leaves the cell, because before then the editor is still open (and the cell hasn't actually changed yet)
I'm pretty sure that I need to create an event handler for the wxGridCellBoolEditor but that's where I'm struggling.
I tried connecting an event in the OnEditorShown event, but that didn't go well (unhandled exception when I click on the cell to open the editor):
void cTeamGrid::OnEditorShown( wxGridEvent& ev )
{
int row = ev.GetRow(),
col = ev.GetCol();
co_Grid->GetCellEditor(row, col)->GetControl()->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED,
wxCommandEventHandler(cTeamGrid::OnGridCheckChange), NULL, this);
}
What am I doing wrong?
I had a similar problem myself. I bypassed it by setting the checkbox column to read-only and having the wxGrid control manually handle the click event to toggle the checkbox state (you also have to manage the double-click). This method is not the most orthodox, also because now each click on the cell, and not on the checkbox, will change the state. In my opinion, however, this can also be a desirable behaviour. In addition, this enables you to let the user change the checkbox with the keyboard (by capturing the KeyPress events).

wxGrid shows the old value instead of new value after user edits a cell

I created the wxGrid in editablemode. I registered the following event handler.
class ReadWriteGrid : public wxGrid
{
public:
ReadWriteGrid(wxWindow *parent, wxWindowID ID,
const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize,
long style=262144, const wxString &name=wxGridNameStr)
: wxGrid(parent, ID, pos, size, style, name)
{
SetEditable();
}
};
EVT_GRID_CELL_CHANGE(IndexTableFrame::OnEditField)
Now when user changes the cell value my function gets called but after it completes. The cell value changes back to its old value.
void IndexTableFrame::OnEditField(wxGridEvent& event)
{
int RowNumber;
int ColNumber;
wxString type;
wxGridCellEditor *m_Editor;
wxString NewValue;
RowNumber = event.GetRow();
ColNumber = event.GetCol();
m_Editor = m_grid->GetDefaultEditorForCell(RowNumber,ColNumber);
NewValue = m_Editor->GetValue();
m_Editor->Destroy();
m_grid->SetCellValue(NewValue,RowNumber,ColNumber);
event.skip();
}
This is the first time I am using WxWidget. I am not sure how to avoid the cell from changing back to its old value.
You are working too hard! Relax and let wxGrid take the strain.
The simplest thing to do is to do nothing. wxGrid will accept user edits in cells and display the changes without you having to do anything. When the user is finished, he can click a button SAVE, where you can implement code to read the values from the cells and use them for whatever you need.
In many applications, one has to do processing on the cell value on the fly as the user changes it, (for example to update other on-screen controls on the fly based on the new cell values; or to send data via a serial port to another device immediately as the cells are changed).
Requiring the user to click a separate "save button" as suggested above is not a solution that will be acceptable to users in many use cases.
I can confirm the observations by the original poster: If you create a wxGrid and attach either wxEVT_GRID_CELL_CHANGED, wxEVT_GRID_CELL_CHANGING, or wxEVT_GRID_EDITOR_HIDDEN events to it, a call to grid->GetCellValue(row,col); returns the old value. The event parameter to the handler function also does not contain the new text. It looks like a "feature" of wxGrid.
You should create your own class inherited from wxGridTableBase.
Then you should attach your grid table object to the wxGrid, using it's SetTable method.
wxGrid object will use your table's methods (GetValue, SetValue) to retrieve and store data.
There's a grid sample shipped with wxWidgets, that will help you understand how does wxGrid work.

When dragging a row in a QTableWidget, how can I find out what row index it was dragged FROM and TO?

I'm trying to keep some array data synchronized with the contents of a QTableWidget. I'd like to enable drag and drop reordering (moving items within the table, as opposed to copying), but it's not clear to me how, when the drop event is fired, I can find out what index the item was dragged FROM. Hence, I have no way of knowing what object to move within the list I'm synchronizing with. How can I get the original row index of the item being dragged?
Encode the from index in QMimeData and store it in the QDrag object with setMimeData(). When the drop event occurs, extract the data from the QDropEvent with mimeData().
Step 1. Override the QTableWidget::mimeData function. Call the base class implementation, then stuff your own custom MIME type into the QMimeData, and return it.
Step 2. Override the QTableWidget::dropEvent function. If your MIME data is in the QMimeData, accept the drop and extract your data. Use the QTableWidget::indexAt to find what row/column the drop went into.
QDropEvent has a source() function that will give you the widget that started the drag drop event. Then do a qobject_cast<QTableWidget> on the source. Once you verify the pointer, call QTableWidget::findItems to get the row of the item.
So something like this:
void dropEvent ( QDropEvent * event ) {
if (event) {
QTableWidget* table = qobject_cast<QTableWidget*>(event->source());
if (table) {
QString item = ""// Decode MIME data here.
Qt::MatchFlag someFlag = Qt::MatchExactly; // Check documentation for match type.
QList<QTableWidgetItem *> items = table->findItems(item, someFlag)
// If you don't have repeats, items[0] is what you want.
int initialRow = table->row(items[0]);
}
}
}
I tend to use model/view classes so this might be a little off, but it should work.