Filling a DataGridView ComboBox with values in C/C++ - c++

I'm creating a windows forms application for university. My data is stored in multible c arrays of abstract data types. Each array is displayed in it's own datagridview. This all works no problem.
Since the data is connected like in a database I added a new column to my parent table and set the ColumnType to DataGridViewComboBoxColumn. Now I need to fill this combobox with the data of a different array / datagridview.
I've already tried to add the options manually from my array as discribed in this question: Filling DataGridView ComboBox programatically in unbound mode?. Since I need to use C/C++ and this example uses vb the used functions are not availible. I can select my column dataGridView1->Columns[0] but the ->Items does not exist.
Is it possible to create a datasource from my arrays to link them directly? Or have I overlooked a way to add them manually from my array?

You don't provide any code, neither you describe well where did you ran into a problem. Assuming you're after unbound list to be filled into the DataGridViewComboBoxColumn, here is a simple example.
C#:
DataTable dt;
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Category", typeof(string));
dt.Rows.Add({1, "Fruits"});
dt.Rows.Add({2, "Vegetables"});
dt.Rows.Add({3, "Trees"});
DataGridViewComboBoxColumn dgCol = this.DataGridView1.columns("Category");
dgCol.ValueMember = "ID";
dgCol.DisplayMember = "Category";
dgCol.DataSource = dt;
In this example, we create a DataTable, fill it with desired values and use it as a DataSource of the DataGridViewComboBoxColumn. Obviously, your values provided to this column in the DataGridView DataSource must match IDs in the list in new DataTable "dt".
You can convert an array or other data into DataTable or even use them directly as DataSource. It depends on what data you have. The principle will be very similar, though.
VB.NET:
Dim dt As DataTable
dt.Columns.Add("ID", GetType(Int32))
dt.Columns.Add("Category", GetType(String))
dt.Rows.Add({1, "Fruits"})
dt.Rows.Add({2, "Vegetables"})
dt.Rows.Add({3, "Trees"})
Dim dgCol As DataGridViewComboBoxColumn = Me.DataGridView1.columns("Category")
dgCol.ValueMember = "ID"
dgCol.DisplayMember = "Category"
dgCol.DataSource = dt

Related

Converting JSON into Table (PowerQuery)

What would be a correct PowerQuery syntax to extract the information from this Web JSON into a table:
I'm not very familiar with PowerQuery, and this is probably the only time I'll need this, so I'd be grateful if someone would help me out without refering me to documentation. Thanks
[{"time_entry_group": {"minutes": 301,"time_entries_params": {"locked": "0","from": "2021-02-01","to": "2021-02-28","customer_id": "11223344","project_id": "223388","service_id": "435248"},"revenue": 57691.6666666667,"project_id": 223388,"project_name": "Scrb","service_id": 435248,"service_name": "Meetings","month": "202102"}}
, {"time_entry_group": {"minutes": 1175,"time_entries_params": {"locked": "1","from": "2021-01-01","to": "2021-01-31","customer_id": "11223344","project_id": "223388","service_id": "421393"},"revenue": 225208.333333333,"project_id": 223388,"project_name": "Scrb","service_id": 421393,"service_name": "Design","month": "202101"}}
, {"time_entry_group": {"minutes": 24,"time_entries_params": {"locked": "1","from": "2021-01-01","to": "2021-01-31","customer_id": "11223344","project_id": "3168911","service_id": "95033"},"revenue": 4600.0,"project_id": 3168911,"project_name": "youkn Dev","service_id": 95033,"service_name": "Reviews","month": "202101"}}]
For future reference, if you have a column that you need to expand, you can instead click this arrow icon to the right of the column name. Clicking it should display a menu that should then allow you to specify which nested columns you want to get expand or get at. To be clear, it will expand that column for all rows in that table, not just one.
The JSON you've included is basically an array of objects, so maybe use:
Json.Document to parse the JSON, which should give you a list of records
Table.FromRecords to turn the list of records into a table.
Table.ExpandRecordColumn to expand a nested record columns.
Example implementation:
let
json = "[{""time_entry_group"":{""minutes"":301,""time_entries_params"":{""locked"":""0"",""from"":""2021-02-01"",""to"":""2021-02-28"",""customer_id"":""11223344"",""project_id"":""223388"",""service_id"":""435248""},""revenue"":57691.6666666667,""project_id"":223388,""project_name"":""Scrb"",""service_id"":435248,""service_name"":""Meetings"",""month"":""202102""}},{""time_entry_group"":{""minutes"":1175,""time_entries_params"":{""locked"":""1"",""from"":""2021-01-01"",""to"":""2021-01-31"",""customer_id"":""11223344"",""project_id"":""223388"",""service_id"":""421393""},""revenue"":225208.333333333,""project_id"":223388,""project_name"":""Scrb"",""service_id"":421393,""service_name"":""Design"",""month"":""202101""}},{""time_entry_group"":{""minutes"":24,""time_entries_params"":{""locked"":""1"",""from"":""2021-01-01"",""to"":""2021-01-31"",""customer_id"":""11223344"",""project_id"":""3168911"",""service_id"":""95033""},""revenue"":4600,""project_id"":3168911,""project_name"":""youkn Dev"",""service_id"":95033,""service_name"":""Reviews"",""month"":""202101""}}]",
parsed = Json.Document(json),
initialTable = Table.FromRecords(List.Transform(parsed, each [time_entry_group])),
expanded = Table.ExpandRecordColumn(initialTable, "time_entries_params", {"locked", "from", "to", "customer_id"})
in
expanded
One thing about the code above is that it doesn't expand nested fields project_id and service_id (present within time_entries_params). This is because these columns already exist in the table (and having duplicate column names would cause an error). I've assumed this isn't a problem, as the nested values aren't different.

C++ Builder Convert TIBQuery to TIBTable

How to convert TIBQuery to TIBTable, additionally to display it in DBGrib?
I want to sort data in DBGrid and I used TIBQuery to take sort data from database, and I have problem to convert data from TIBQuery to TIBTable.
Create a VCL C++Builder app - Drop in TIBDatabase, TIBTable, TDataSource and TDBGrid on your form. Make active the TIBDatabase and TIBTable connections. Right mouse click on the TIBTable to add the columns you want to have appear in your TDBGrid. Leave the Column Header text to be the same as the Column Names.
Add the following line of code to the onTitleClick for the DBGrid:
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
// set TIBTable's IndexFieldNames property to the column title field name
// this will sort ascending all of the data in the DBGrid
IBTable1->IndexFieldNames = Column->FieldName;
}
If you want to do even more, I suggest that you add a TDataSetProvider and TClientDataSet to your form and then you can do more with your app.

How to insert programmatically a value in a new row of a QtableView

I'm using a QtableView to display and edit data from a QsqlTableModel.
Everything's fine : data from a postgreSQL table is displayed, and user can edit it and save modifications.
I want to add a row when uses click on a button. I use the insertRecord method from my QslTableModel.
The row is correctly added in the QTableView.
My problem is :
I want to insert a value from a query in the first cell of this new row. (to automatically populate an unique identifier).
This is my code :
def ajoutlgn(self):
query_idNewLine = QtSql.QSqlQuery(self.db)
if query_idNewLine.exec_('SELECT sp_idsuivi+1 FROM bdsuivis.t_suivprev_tablo ORDER BY sp_idsuivi DESC LIMIT 1'):
while query_idNewLine.next():
identifiant = query_idNewLine.value(0)
#print identifiant
record = QtSql.QSqlRecord()
record.setValue(1,identifiant)
self.model.insertRecord(self.model.rowCount(), record)
The value is not inserted in the new row (but if I enter a value by hand, it works perfectly).
Nevertheless, the query is OK (as I can see with the « print identifiant » line, which returns the expected integer).
Do you see what I'm doing wrong ?
Are there other methods to insert programmatically a value in a QTableView cell?
Or do I have to use a QitemDelegate ?
Thanks for advance.
Finally I found the solution :
This line creates a record, but not a record for my QsqlTableModel
record = QtSql.QSqlRecord()
I replaced it with this one, and it works perfectly :
record = self.model.record()

Use DataColumn values as source for ValueList on UltraGridColumn

Is there a way I can use a DataColumn from a DataTable as the ValueList for an UltraGrid column?
I have the column style set to DropDownList. The question is, what do I use for ValueList?
ultraGrid1.DisplayLayout.Bands[0].Columns["col"].Style = ColumnStyle.DropDownList;
ultraGrid1.DisplayLayout.Bands[0].Columns["col"].ValueList = ???
Create a BindableValueList. One of the Constructors takes the object for the datasource, the dataMember, the displayMember, valueMember, and bindingContextControl.
The line of code to set the ValueList:
this.ultraGrid1.DisplayLayout.Bands[0].Columns["col"].ValueList = new BindableValueList(dt, "", "displayColumn","valueColumn", this.ultraGrid1);
In the above example, dt is the DataTable you are binding to, displayColumn and valueColumn would be the keys for the columns that you want to use as the display and value portions of the drop down. If you are binding to a DataSet and want to bind to a table other than the first table you would use the second parameter to pass in the name of the table to bind to.
This could be of help. It a simple procedure that receives a DataTable, the value for the Key property of the ValueList and the name of the field to use as list for the the ValueList and returns a Value list to set in one of your UltraGridColums
public ValueList DataTableToValueList(DataTable dt, string vlKey, string fieldName)
{
ValueList vl = new ValueList();
if (!string.IsNullOrEmpty(vlKey)) vl.Key = vlKey;
foreach (DataRowView r in dt.DefaultView)
vl.ValueListItems.Add(r[fieldName]);
return vl;
}
use in this way (probably in the InitializeLayout event)
grd.DisplayLayout.Bands[0].Columns["col"].ValueList =
DataTableToValueList(dtCustomers,"vlCustomer", "CustomerName");

Is creating a custom model the way to access data in a table by name?

Say I have a QTableWidget with 10 columns. I set data in the table with
QTableWidgetItem* textItem1 = new QTableWidgetItem;
textItem1->setData(Qt::DisplayRole, 20);
this->tableWidget->setItem(row, col, textItem1);
The problem is that if I change the order or titles of the columns in the table using QtDesigner, I have to go through my code and change all of the column numbers. I would rather do something like this:
QTableWidgetItem* textItem1 = new QTableWidgetItem;
textItem1->setData(Qt::DisplayRole, "Smith");
this->tableWidget->setLastName(row, textItem1);
to add "Smith" to the "LastName" column, without needing to know which column index LastName currently is. Is the way to do this with a custom Model? I looked into QAbstractTableModel, but I don't understand how to provide named access to data? Can anyone briefly explain how one would do this? And if this is a reasonable/common thing to want to do?
Thanks.
I posted an example of using QAbstractTableModel:
http://programmingexamples.net/index.php?title=Qt/ModelView/QAbstractTableModel
As you can see, the data is simply stored in a member variable, and can therefore be accessed however you would like.