GTK CellRendererCombo set value into appropriate cell - combobox

What I'm trying to do it a TreeView that have several columns. The first column is a ComboBox, which means that I use Gtk.CellRendererCombo. The thing is, when I selected a value from the ComboBox, I'd like the Text from the cell to change from "" to the value I just selected. It feasible if at the Gtk.CellRendererCombo.Edited event I set the columns Text field to the EditedArgs.NewText.
The problem is, each time I set a value, I create a new row, and I'd like the Text Field to act like it does in the Gtk.CellRendererText, but it doesn't. It's not a different value for each row at that column, but the same value as setted in the Gtk.CellRendererCombo.Text
The Gtk.CellRenderer should not contain any state, so OK, using the Text field is a really bad idea from what I'm trying to do.
But if I set some value from the Gtk.ListStore that is the Model of my TreeView (Which is different from the Model for the Gtk.CellRendererCombo). The values setted will never show at the Column of the ComboBox.
class Program
{
private static Gtk.TreeView treeview = null;
static void OnEdited(object sender, Gtk.EditedArgs args)
{
Gtk.TreeSelection selection = treeview.Selection;
Gtk.TreeIter iter;
selection.GetSelected(out iter);
treeview.Model.SetValue(iter, 0, args.NewText); // the CellRendererCombo
treeview.Model.SetValue(iter, 1, args.NewText); // the CellRendererText
//(sender as Gtk.CellRendererCombo).Text = args.NewText; // Will set all the Cells of this Column to the Selection's Text
}
static void Main(string[] args)
{
Gtk.Application.Init();
Gtk.Window window = new Window("TreeView ComboTest");
window.WidthRequest = 200;
window.HeightRequest = 150;
Gtk.ListStore treeModel = new ListStore(typeof(string), typeof(string));
treeview = new TreeView(treeModel);
// Values to be chosen in the ComboBox
Gtk.ListStore comboModel = new ListStore(typeof(string));
Gtk.ComboBox comboBox = new ComboBox(comboModel);
comboBox.AppendText("<Please select>");
comboBox.AppendText("A");
comboBox.AppendText("B");
comboBox.AppendText("C");
comboBox.Active = 0;
Gtk.TreeViewColumn comboCol = new TreeViewColumn();
Gtk.CellRendererCombo comboCell = new CellRendererCombo();
comboCol.Title = "Combo Column";
comboCol.PackStart(comboCell, true);
comboCell.Editable = true;
comboCell.Edited += OnEdited;
comboCell.TextColumn = 0;
comboCell.Text = comboBox.ActiveText;
comboCell.Model = comboModel;
comboCell.WidthChars = 20;
Gtk.TreeViewColumn valueCol = new TreeViewColumn();
Gtk.CellRendererText valueCell = new CellRendererText();
valueCol.Title = "Value";
valueCol.PackStart(valueCell, true);
valueCol.AddAttribute(valueCell, "text", 1);
treeview.AppendColumn(comboCol);
treeview.AppendColumn(valueCol);
// Append the values used for the tests
treeModel.AppendValues("comboBox1", string.Empty); // the string value setted for the first column does not appear.
treeModel.AppendValues("comboBox2", string.Empty);
treeModel.AppendValues("comboBox3", string.Empty);
window.Add(treeview);
window.ShowAll();
Gtk.Application.Run();
}
}
I'd like the Cell of the ComboBox in which the selection has been made to show the value, but continue to be editable for later changes.
If someone has a way of doing this, I would be very grateful for you input. thanks.
Update:
What I think, because Gtk.CellRendererCombo inherits from Gtk.CellRendererText it just ignores the value setted in the cell. Now, I guess I could create a custom MyCellRendererCombo that inherits from Gtk.CellRendererCombo and use the value setted in the cell when supplied for the rendering, but the documentation on the difference between Gtk.CellRendererText and Gtk.CellRendererCombo is quite slim... I guess I should visit the implementation in C to know the details.

Ok, I've found a solution to my problem, I use a "hidden" column in which I read the "text" value. The hidden column contains the Gtk.CellRendererText, the Gtk.CellRendererCombo must have an Attribute Mapping with the new Column.
The resulting code is below:
class Program
{
private static Gtk.TreeView treeview = null;
static void OnEdited(object sender, Gtk.EditedArgs args)
{
Gtk.TreeSelection selection = treeview.Selection;
Gtk.TreeIter iter;
selection.GetSelected(out iter);
treeview.Model.SetValue(iter, 1, args.NewText); // the CellRendererText
}
static void Main(string[] args)
{
Gtk.Application.Init();
Gtk.Window window = new Window("TreeView ComboTest");
window.WidthRequest = 200;
window.HeightRequest = 150;
Gtk.ListStore treeModel = new ListStore(typeof(string), typeof(string));
treeview = new TreeView(treeModel);
// Values to be chosen in the ComboBox
Gtk.ListStore comboModel = new ListStore(typeof(string));
Gtk.ComboBox comboBox = new ComboBox(comboModel);
comboBox.AppendText("<Please select>");
comboBox.AppendText("A");
comboBox.AppendText("B");
comboBox.AppendText("C");
comboBox.Active = 0;
Gtk.TreeViewColumn comboCol = new TreeViewColumn();
Gtk.CellRendererCombo comboCell = new CellRendererCombo();
comboCol.Title = "Combo Column";
comboCol.PackStart(comboCell, true);
comboCol.AddAttribute(comboCell, "text", 1);
comboCell.Editable = true;
comboCell.Edited += OnEdited;
comboCell.TextColumn = 0;
comboCell.Text = comboBox.ActiveText;
comboCell.Model = comboModel;
comboCell.WidthChars = 20;
Gtk.TreeViewColumn valueCol = new TreeViewColumn();
Gtk.CellRendererText valueCell = new CellRendererText();
valueCol.Title = "Value";
valueCol.PackStart(valueCell, true);
valueCol.AddAttribute(valueCell, "text", 1);
valueCol.Visible = false;
treeview.AppendColumn(comboCol);
treeview.AppendColumn(valueCol);
// Append the values used for the tests
treeModel.AppendValues("comboBox1", "<Please select>"); // the string value setted for the first column does not appear.
treeModel.AppendValues("comboBox2", "<Please select>");
treeModel.AppendValues("comboBox3", "<Please select>");
window.Add(treeview);
window.ShowAll();
Gtk.Application.Run();
}
}

Related

How to display Object data in a TableLayoutPanel in a performant way

I have written the folowing code to populate the data from a object in a tablelayoutpanel control. It works Smile | :) , but when its loading the data onto the table, it flickers/jumps for few seconds and then after 2-3 seconds when its done processing the data it populates the data Frown | :( . I believe this behaviour is because of the code written for dynamically processing & drawing of the various controls in the table based on the object data.
I need your help in optimising the code/improving the performance of this code so that the table can load smoothly and fast. Please help. Thanks.
PS: This code is written for a table containing small amount of data. But going forward the same is planned for populating table with 4X more data. If this is the case, then performance will be very poor, which worries me. Please suggest some ideas.
private void button1_Click(object sender, EventArgs e)
{
Common obj = new Common();
obj.CreateDeserializedXmlObject(#"E:\TestReport.xml");
var v = obj.GetAdminData();
tableLayoutPanel1.ColumnCount = 4;
tableLayoutPanel1.RowCount = ((v.DOCREVISIONS.Length * 4) + 1 + (v.USEDLANGUAGES.L10.Length));
Label labelLanguage = new Label();
Label labelUsedLanguage = new Label();
Label labelDocRevisions = new Label();
labelLanguage.Text = "Language:";
labelUsedLanguage.Text = "Used Language:";
labelDocRevisions.Text = "Doc-Revisions:";
ComboBox comboBoxLanguage = new ComboBox();
comboBoxLanguage.Items.Add(v.LANGUAGE.Value.ToString());
comboBoxLanguage.SelectedIndex = 0;
ComboBox comboBoxUsedLanguage = new ComboBox();
foreach (LPLAINTEXT Lang in v.USEDLANGUAGES.L10)
{
comboBoxUsedLanguage.Items.Add(Lang.L.ToString());
}
comboBoxUsedLanguage.SelectedIndex = 0;
int index = 0;
Label[] labelDocRevision = new Label[v.DOCREVISIONS.Length];
Label[] labelRevision = new Label[v.DOCREVISIONS.Length];
Label[] labelState = new Label[v.DOCREVISIONS.Length];
Label[] labelTeamMember = new Label[v.DOCREVISIONS.Length];
Label[] labelDate = new Label[v.DOCREVISIONS.Length];
TextBox[] textBoxRevision = new TextBox[v.DOCREVISIONS.Length];
TextBox[] textBoxState = new TextBox[v.DOCREVISIONS.Length];
TextBox[] textBoxTeamMember = new TextBox[v.DOCREVISIONS.Length];
TextBox[] textBoxDate = new TextBox[v.DOCREVISIONS.Length];
foreach (DOCREVISION dcr in v.DOCREVISIONS)
{
labelDocRevision[index] = new Label();
labelRevision[index] = new Label();
labelState[index] = new Label();
labelTeamMember[index] = new Label();
labelDate[index] = new Label();
textBoxRevision[index] = new TextBox();
textBoxState[index] = new TextBox();
textBoxTeamMember[index] = new TextBox();
textBoxDate[index] = new TextBox();
labelDocRevision[index].Text = "DOCREVISION["+index.ToString()+"]:";
labelRevision[index].Text = "Revision:";
labelState[index].Text = "State:";
labelTeamMember[index].Text = "TeamMemberRef:";
labelDate[index].Text = "Date:";
textBoxRevision[index].Text = dcr.REVISIONLABEL.Value.ToString();
textBoxState[index].Text = dcr.STATE.Value.ToString();
textBoxTeamMember[index].Text = dcr.TEAMMEMBERREF.Value.ToString();
textBoxDate[index].Text = dcr.DATE.Value.ToString();
index++;
}
// Add child controls to TableLayoutPanel and specify rows and column
tableLayoutPanel1.Controls.Add(labelLanguage, 0, 0);
tableLayoutPanel1.Controls.Add(labelUsedLanguage, 0, 1);
tableLayoutPanel1.Controls.Add(labelDocRevisions, 0, 2);
tableLayoutPanel1.Controls.Add(comboBoxLanguage, 1, 0);
tableLayoutPanel1.Controls.Add(comboBoxUsedLanguage, 1, 1);
int docRevRowSpacing = 2;
for (int loop = 0; loop < index; loop++)
{
tableLayoutPanel1.Controls.Add(labelDocRevision[loop], 1, docRevRowSpacing);
tableLayoutPanel1.Controls.Add(labelRevision[loop], 2, docRevRowSpacing);
tableLayoutPanel1.Controls.Add(labelState[loop], 2, docRevRowSpacing+1);
tableLayoutPanel1.Controls.Add(labelTeamMember[loop], 2, docRevRowSpacing+2);
tableLayoutPanel1.Controls.Add(labelDate[loop], 2, docRevRowSpacing+3);
tableLayoutPanel1.Controls.Add(textBoxRevision[loop], 3, docRevRowSpacing);
tableLayoutPanel1.Controls.Add(textBoxState[loop], 3, docRevRowSpacing+1);
tableLayoutPanel1.Controls.Add(textBoxTeamMember[loop],3 , docRevRowSpacing+2);
tableLayoutPanel1.Controls.Add(textBoxDate[loop], 3, docRevRowSpacing+3);
docRevRowSpacing += 4;
}
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
Controls.Add(this.tableLayoutPanel1);
}
There are two minor changes that helps a little bit.
At the start of your code you can call SuspendLayout. This prevents the TableLayoutPanel to redraw itself every time you add a control to it. When you're done adding all controls at the end you call ResumeLayout. At that moment the TableLayoutPanel will redraw only once. It still takes time but at least most the flickering is gone. At the end of your example code you add the tableLayoutPanel1 again to the forms control collection. If the TableLayoutPanel is on your form designer you don't need that and by doing it you make your performance worse because now you have two tableLayoutPanels that need to be painted.
private void button1_Click(object sender, EventArgs e)
{
tableLayoutPanel1.SuspendLayout();
// all your other code goes here
// not sure why you add the tableLayouyPanel AGAIN to the
// form control collection.
// Controls.Add(this.tableLayoutPanel1);
tableLayoutPanel1.ResumeLayout();
}
I noticed in my testing that resizing the form gives the same flickering effect. I used the ResizeBegin and ResizeEnd events to do the same Suspend and Resume layout trick:
private void Form1_ResizeBegin(object sender, EventArgs e)
{
tableLayoutPanel1.SuspendLayout();
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
tableLayoutPanel1.ResumeLayout();
}
This as much as you can do with your current code (except maybe the use of all those arrays with controls but their overhead is not the major issue here).
The TableLayoutPanel is maybe not the best control for what you want to achieve. It lacks for example VirtualMode support, something the DataGridView does. That would enable you to only load and show data that is visible on the form (and therefor create controls for it). Adapting your code to use that control is left as an exercise for the reader and if new issues pop-up feel free to start a new question.

Add Dynamic Check Box in Grid Takes Time in Silverlight

I Have a Grid (BrkGrid) in ViewMode... I Add dynamic Check Box (brkChkBox) depending on some logic. A foreach is responsible for adding check boxes to a specific row and column of this grid.. But it takes time while adding check boxes to the BrkGrid.. When I Comment the Add Statement (BrkGrid.Children.Add(brkChkBox)) then the code executes faster.. Any help will be highly appreciated...
CheckBox brkChkBox = null;
foreach (var s in this.ViewData.PlnDtShiftBrksDateList)
{
brkChkBox = new CheckBox
{
DataContext = s,
Tag = s.BreakID,
Width = 20,
VerticalAlignment = VerticalAlignment.Top,
};
Binding chkBoxBinding = new Binding("IsSelected");
chkBoxBinding.Source = s;
chkBoxBinding.Mode = BindingMode.TwoWay;
brkChkBox.SetBinding(CheckBox.IsCheckedProperty, chkBoxBinding);
brkChkBox.Click += brkChkBox_Click;
Grid.SetColumn(brkChkBox, gridColDic.FirstOrDefault(x => x.Key == s.BreakID).Value);
Grid.SetRow(brkChkBox, gridRowDic.FirstOrDefault(x => x.Key.Date == s.Date.Date).Value);
BrkGrid.Children.Add(brkChkBox);
}

Binding to dynamically created controls

I'm having trouble binding values to dynamically created controls. When a user loads a custom file and that file will have a unknown number of arguments. Arguments have a Group property that when grouped will dynamically add tabItems to a tabControl. I then loop through the arguments and add a label and, for now, a textbox to a grid inside the tabs. though i intend to use different controls depending on the arument type. I want to bind the argument Value property to the textbox. The tabs, labels and textboxes are added fine but, no value binding
He if my not yet re-factored solution so far;
myTab.Items.Clear();
var args = viewModel.Arguments;
var groups = args.GroupBy(arg => arg.Groups);
foreach (var group in groups)
{
TabItemExt tab = new TabItemExt();
tab.Header = group.Key;
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
int count = 0;
foreach (var argument in group)
{
RowDefinition newRow = new RowDefinition();
grid.RowDefinitions.Insert(count, newRow);
LabelTextBlock label = new LabelTextBlock();
label.Text = argument.DisplayName;
Grid.SetRow(label, count);
Grid.SetColumn(label, 0);
TextBox textBox = new TextBox();
var binding = new Binding();
binding.Source = viewModel.Arguments[argument.Name];
//binding.Source = argument
binding.Path = new PropertyPath("Value");
textBox.SetBinding(TextBlock.TextProperty, binding);
Grid.SetRow(textBox, count);
Grid.SetColumn(textBox, 1);
grid.Children.Add(label);
grid.Children.Add(textBox);
count += 1;
}
tab.Content = grid;
myTab.Items.Add(tab);
}
textBox.SetBinding(TextBlock.TextProperty, binding);
should have been
textBox.SetBinding(TextBox.TextProperty, binding);
just a little over dependent on intellisense.

DataSet created in code not Binding to ListView

I have a WPF User Control with a ListView in it that is created based on the DataSet that is passed to it:
public void PopulateList(DataSet ds) {
listView.View = CreateGridViewColumns(ds.Tables[0]);
listData.DataContext = ds.Tables[0];
}
private GridView CreateGridViewColumns(DataTable dt) {
// Create the GridView
var gv = new GridView {AllowsColumnReorder = true};
// Create the GridView Columns
foreach (DataColumn item in dt.Columns) {
var gvc = new GridViewColumn
{
DisplayMemberBinding = new Binding(item.ColumnName),
Header = item.ColumnName,
Width = Double.NaN
};
gv.Columns.Add(gvc);
}
return gv;
}
Now I create the user control in code and call it's PopulateList with the appropriate dataset and this is where the problems are starting:
If I pass in a dataset that was created from a call to the database the list view shows all the data but if i pass in a DataSet that i created in code the ListView shows the Columns but will not show the data
//This is a function that hides the DB call return type is DataSet
var dsPatientSmokingStatusHistory = DataRepository.PatientSmokingStatusProvider.GetHistory(PatientId);
//radGridViewPatientSmokingStatus.DataSource = dsPatientSmokingStatusHistory.Tables[0];
var dt = new DataTable();
string c1 = "Date".PadLeft(23).PadRight(23);
string c2 = "Status".PadLeft(20).PadRight(50);
dt.Columns.Add(c1);
dt.Columns.Add(c2);
int i = 0;
foreach (DataRow row in dsPatientSmokingStatusHistory.Tables[0].Rows) {
var dataRow = dt.NewRow();
dataRow[c1] = ((DateTime)row["Date"]).ToString("MM/dd/yyyy");
dataRow[c2] = row["Status"].ToString();
dt.Rows.Add(dataRow);
dt.Rows[i].AcceptChanges();
i++;
}
DataSet ds = new DataSet();
dt.TableName = "Table";
ds.Tables.Add(dt);
ds.AcceptChanges();
smokingStatusGrid.GridWidth = 455;
smokingStatusGrid.GridHight = 97;
//This line does not show data
smokingStatusGrid.PopulateGrid(ds);
//This line will show data
smokingStatusGrid.PopulateGrid(dsPatientSmokingStatusHistory);
Is there a difference between these two datasets that i don't know about that is preventing me from databinding to it?
Also the user control is being used as an ElementHost in a WinForms application (not sure if this makes a difference)
Your code says:
DisplayMemberBinding = new Binding(item.ColumnName)
This binding constructor takes a string paramter which as per MSDN is "The initial Path for the binding" and is of datatype System.Windows.PropertyPath. I guess, since system tries to find a property with the same name in your class, and your string (item.ColumnName) has spaces at start, it runs into a problem (properties can't start with a space).
Would recommend you to take off the padding that you are doing in column name of your table. Apply any padding/margins in the Header of your GridView.

Databinding of DataGridViewComboBoxColumn

In the code below, the combo box named "ConnectionType" shows the selected item, but one cannot change the selected item (it seems like there is only one item in the combo box). If I comment out the line
typeCol.DataPropertyName = "ConnectionTypeName";
then the combo box is selectable, but the correct item is not selected, of course. What am I doing wrong??
Thanks.
private void LoadConnectionsGrid()
{
_dc = new EnterpriseEntities();
dataGridViewConnections.AutoGenerateColumns = false;
dataGridViewConnections.DataSource = _dc.Connection.Include("ConnectionType");
DataGridViewComboBoxColumn typeCol =
(DataGridViewComboBoxColumn)dataGridViewConnections.Columns["ConnectionType"];
typeCol.DataPropertyName = "ConnectionTypeName";
var qry = from c in _dc.ConnectionType
select c.Type;
typeCol.DataSource = qry;
DataGridViewTextBoxColumn nameCol =
(DataGridViewTextBoxColumn)dataGridViewConnections.Columns["ConnectionName"];
nameCol.DataPropertyName = "Name";
DataGridViewTextBoxColumn connStrCol =
(DataGridViewTextBoxColumn)dataGridViewConnections.Columns["ConnectionString"];
connStrCol.DataPropertyName = "ConnectionString";
}
Ultimately, I could not get data binding, the entity framework, and comboboxes to play nice and I just created the data grid brute force (code below). This means that I handle inserts, updates and deletes by hand.
private void LoadConnectionsGrid()
{
DataGridViewComboBoxColumn typeCol =
(DataGridViewComboBoxColumn)dataGridViewConnections.Columns["ConnectionType"];
var qry = from c in _dc.ConnectionType
select c.Type;
typeCol.DataSource = qry;
dataGridViewConnections.Rows.Clear();
foreach (Connection conn in _dc.Connection.Include("ConnectionType"))
{
dataGridViewConnections.Rows.Add(conn.Name,
conn.ConnectionType.Type, conn.ConnectionString);
}
}

Resources