ListView with only 1 column is not filling the whole width - winforms

I have a list view that gets populated dynamically each time a new message comes in. I did not add a column to the collection in the designer since I only need 1 column. But when the new message comes in, the "listview" gets populated but the single column is not taking the whole width of the listview.
lvRfidMessages.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
lvRfidMessages.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
string[] rowData = { e };
var lvItem = new ListViewItem(rowData);
lvRfidMessages.Items.Add(lvItem);
The design
this.lvRfidMessages.ForeColor = System.Drawing.Color.DimGray;
this.lvRfidMessages.FullRowSelect = true;
this.lvRfidMessages.GridLines = true;
this.lvRfidMessages.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.lvRfidMessages.HideSelection = false;
this.lvRfidMessages.Location = new System.Drawing.Point(0, 64);
this.lvRfidMessages.Name = "lvRfidMessages";
this.lvRfidMessages.Size = new System.Drawing.Size(182, 185);
this.lvRfidMessages.TabIndex = 3;
this.lvRfidMessages.UseCompatibleStateImageBehavior = false;

Related

get cell content of WPF DataGrid after CellEditEnding and format numbers in DataGrid

Now I've searched for hours but didn't find an answer.
I have a WPF app with a DataGrid and want to edit data directly within a DataGrid cell. I can read the Data before change but not after change and therefore I can't save the changes.
And the second question is, how can I format numbers ("0.0") in a DataGrid column?
What I have so far:
There's nothing special in the XAML, only the raw definition of the DataGrid, without column definitions or something else.
And this is my source:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (!System.IO.File.Exists(dbFile[0]) | !System.IO.File.Exists(dbFile[1])) // the 2 XML data files
{
SystemSounds.Beep.Play();
MessageBox.Show("File(s)" + Environment.NewLine + " " + dbFile[0] + Environment.NewLine + "and/or" + Environment.NewLine + " " + dbFile[1] + Environment.NewLine + "don't exist." + Environment.NewLine + "The program will be closed.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(0);
}
OpenData();
}
private void OpenData()
{
DataTable dbDataTbl = new DataTable("MyData"); // name must be the same as in XML!!!
dbDataTbl.ReadXmlSchema(dbFile[1]);
dbDataTbl.ReadXml(dbFile[0]);
// DataGrid
DataView dataView = new DataView(dbDataTbl);
DataGrid1.ItemsSource = dataView;
// format column ID
DataGrid1.Columns[0].IsReadOnly = true; // ID
Style style = new Style(); // creates object of style class
style.TargetType = typeof(DataGridCell); // sets target type as DataGrid cell
Setter s = new Setter(); // create objects of setter class
s.Property = HorizontalAlignmentProperty;
s.Value = HorizontalAlignment.Right;
style.Setters.Add(s);
s = new Setter();
s.Property = ForegroundProperty;
s.Value = Brushes.LightGray;
style.Setters.Add(s);
DataGrid1.Columns[0].CellStyle = style;
// format column Date
style = new Style();
style.TargetType = typeof(DataGridCell);
s = new Setter();
s.Property = ContentStringFormatProperty;
s.Value = "yyyy-MM-dd"; // DOES NOT WORK !!!
style.Setters.Add(s);
DataGrid1.Columns[1].CellStyle = style;
// format column Weight
style = new Style();
style.TargetType = typeof(DataGridCell);
s = new Setter();
s.Property = HorizontalAlignmentProperty;
s.Value = HorizontalAlignment.Right;
style.Setters.Add(s);
s = new Setter();
s.Property = ContentStringFormatProperty;
//s.Value = "{}{0:0.0}"; // DOES NOT WORK !!!
s.Value = "0.0"; // DOES NOT WORK !!!
style.Setters.Add(s);
DataGrid1.Columns[2].CellStyle = style;
// format column Text
DataGrid1.Columns[3].MinWidth = 115;
}
private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
int row = DataGrid1.Items.IndexOf(DataGrid1.CurrentItem);
int col = DataGrid1.CurrentCell.Column.DisplayIndex;
DataRowView drv = (DataRowView)DataGrid1.SelectedItem;
int id = Convert.ToInt32(drv.Row[0]); // value of ID
string val = drv.Row.ItemArray[col].ToString(); // old value of a cell
if (e.EditAction == DataGridEditAction.Commit)
{
var t = e.EditingElement as TextBox; // assumes columns are all TextBoxes
var txt = t.Text.ToString();
// when I press Enter after editing cell data I always get the old data,
// never the edited new one
// save data
// ...
}
}
thanks

Resizing of an AutoScroll Panel affects scrolled position

When I resize the following form with the right resize handle, the contained TableLayoutPanel gets decorated with scroll bars (as intended, panel1.AutoScroll = true) for smaller form sizes, but the TableLayoutPanel also gets displaced from its original position. See images below: after resizing the form with right resize handle only, the second one has its scroll bars not leftmost and the left border of the content is cut off.
It seems somehow that this behavior is tied to the existence of the nested RadioButtons because if I remove them (or replace them by another TextBox for example), the "normal" behavior is restored (TableLayoutPanel stays in place during resize).
What properties do I have to set in order to keep the content always stationary relative to the (top)left borders?
BTW: When I replace the panel1 by a TabControl + one TabPage, the "normal" behavior is also restored.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
/// <summary>
/// Description of Form3.
/// </summary>
public partial class Form3 : Form
{
const int textBoxNameWidth = 500;
TableLayoutPanel testControl1;
Panel panel1;
TextBox textBoxName;
RadioButton radioButtonNo;
RadioButton radioButtonYes;
TableLayoutPanel tableLayoutPanelDecision;
public Form3()
{
testControl1 = new TableLayoutPanel();
panel1 = new Panel();
textBoxName = new TextBox();
radioButtonNo = new RadioButton();
radioButtonYes = new RadioButton();
tableLayoutPanelDecision = new TableLayoutPanel();
testControl1.AutoSize = true;
testControl1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
testControl1.Location = new Point(0, 0);
testControl1.Dock = DockStyle.None;
testControl1.ColumnCount = 2;
testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
testControl1.RowCount = 2;
testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
testControl1.Controls.Add(textBoxName, 1, 0);
testControl1.Controls.Add(tableLayoutPanelDecision, 1, 1);
textBoxName.Text = "New Boolean";
textBoxName.TextAlign = HorizontalAlignment.Center;
textBoxName.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
textBoxName.TabStop = false;
textBoxName.Width = textBoxNameWidth;
tableLayoutPanelDecision.AutoSize = true;
tableLayoutPanelDecision.ColumnCount = 2;
tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
tableLayoutPanelDecision.RowCount = 1;
tableLayoutPanelDecision.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanelDecision.Controls.Add(radioButtonYes, 0, 0);
tableLayoutPanelDecision.Controls.Add(radioButtonNo, 1, 0);
tableLayoutPanelDecision.Dock = DockStyle.Fill;
radioButtonNo.Checked = true;
radioButtonNo.AutoSize = true;
radioButtonNo.TabIndex = 1;
radioButtonNo.TabStop = true;
radioButtonNo.Text = "False";
radioButtonNo.UseVisualStyleBackColor = true;
radioButtonNo.Anchor = AnchorStyles.None;
radioButtonYes.AutoSize = true;
radioButtonYes.TabIndex = 0;
radioButtonYes.Text = "True";
radioButtonYes.UseVisualStyleBackColor = true;
radioButtonYes.Anchor = AnchorStyles.None;
panel1.AutoScroll = true;
panel1.Controls.Add(testControl1);
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
panel1.Location = new System.Drawing.Point(0, 0);
panel1.Name = "panel1";
panel1.Size = new System.Drawing.Size(560, 219);
panel1.TabIndex = 1;
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(560, 219);
Controls.Add(panel1);
Name = "Form3";
Text = "Form3";
}
}
}
The panel is trying to keep focusable controls within view for the user. To change that, you would have to use your own panel:
public class PanelEx : Panel {
protected override Point ScrollToControl(Control activeControl) {
return this.DisplayRectangle.Location;
}
}

DevExpress DisplayFormat in RowCellStyle

Switching application to use DevExpress XtraGrid and implemented customized color and format for row/cells.
For most part formats are being applied correctly. However when applied to a decimal 1000 following format "#,###;(#,###);0" ends up 1000.0000 instead of 1,000.
gridView.RowCellStyle += CellFormatting;
private void CellFormatting(object sender, RowCellStyleEventArgs e)
{
if (gridView.IsRowSelected(e.RowHandle))
{
e.Appearance.BackColor = SystemColors.Highlight;
e.Appearance.ForeColor = SystemColors.HighlightText;
return;
}
// get cell by its index
var gridRow = gridView.GetRow(e.RowHandle);
TLColumn columnEnum = ((BindableTextBoxColumn)e.Column).ColumnEnum;
// get new format values
T row = (T)gridRow;
e.Column.DisplayFormat.FormatString = row.GetCellFormat(columnEnum);
e.Appearance.BackColor = row.GetCellBackColor(columnEnum);
e.Appearance.ForeColor = row.GetCellColor(columnEnum);
}
For bound columns that do not use CustomColumnDisplayText need to set FormatType before setting DisplayFormatString.
e.Column.ColumnType
can show type of the bound property
private void CellFormatting(object sender, RowCellStyleEventArgs e)
{
// get cell by its index
var gridRow = gridView.GetRow(e.RowHandle);
var column = (BindableTextBoxColumn)e.Column;
TLColumn columnEnum = column.ColumnEnum;
// get new format values
T row = (T)gridRow;
e.Column.DisplayFormat.FormatType = (column.IsNumeric) ? FormatType.Numeric : column.DisplayFormat.FormatType;
e.Column.DisplayFormat.FormatString = row.GetCellFormat(columnEnum);
if (gridView.IsRowSelected(e.RowHandle))
{
e.Appearance.BackColor = SystemColors.Highlight;
e.Appearance.ForeColor = SystemColors.HighlightText;
return;
}
e.Appearance.BackColor = row.GetCellBackColor(columnEnum);
e.Appearance.ForeColor = row.GetCellColor(columnEnum);
}

Radiobuttons are invisible inside of Array of Panels

In the Form constructor I generate an array of panels (pnl_ in the code below), that include radiobuttons.
When I visualize the Form, only panels are visible, NOT radiobuttons inside.
Curiously, everything is OK, when I generate only one panel (pnl in the code below).
public partial class Radiobuttons_on_Panel : Form
{
private Panel pnl = new Panel();
private RadioButton rbtn1 = new RadioButton();
private RadioButton rbtn2 = new RadioButton();
private Panel[] pnl_ = new Panel[7];
private RadioButton[] rbtn1_ = new RadioButton[7];
private RadioButton[] rbtn2_ = new RadioButton[7];
public Radiobuttons_on_Panel()
{
InitializeComponent();
pnl.Location = new Point(10, 10);
pnl.Size = new Size(100, 100);
pnl.BorderStyle = BorderStyle.FixedSingle;
rbtn1.Location = pnl.Location;
rbtn1.Text = "AAA";
rbtn2.Location = new Point(pnl.Location.X, pnl.Location.Y + rbtn1.Height);
rbtn2.Text = "BBB";
pnl.Controls.Add(rbtn1);
pnl.Controls.Add(rbtn2);
this.Controls.Add(pnl);
for (int i = 0; i < pnl_.Length; i++)
{
pnl_[i] = new Panel();
pnl_[i].BorderStyle = BorderStyle.FixedSingle;
pnl_[i].Size = new Size(100, 100);
pnl_[i].Location = new Point(10 + i * 110, 200);
rbtn1_[i] = new RadioButton();
rbtn1_[i].Location = pnl_[i].Location;
rbtn1_[i].Text = "AAA";
rbtn2_[i] = new RadioButton();
rbtn2_[i].Location = new Point(pnl_[i].Location.X, pnl_[i].Location.Y + rbtn1_[i].Height);
rbtn2_[i].Text = "BBB";
pnl_[i].Controls.Add(rbtn1_[i]);
pnl_[i].Controls.Add(rbtn2_[i]);
this.Controls.Add(pnl_[i]);
}
}
}
Where is the problem?
Your problem with this is that your calculation of location is wrong. The location of the radio buttons are relative to their container - in this case, the panels. If you just set your locations to
rbtn1_[i] = new RadioButton();
rbtn1_[i].Location = new Point(10, 10);
rbtn1_[i].Text = "AAA";
rbtn2_[i] = new RadioButton();
rbtn2_[i].Location = new Point(rbtn1_[i].Location.X, rbtn1_[i].Location.Y + rbtn1_[i].Height);
rbtn2_[i].Text = "BBB";
the buttons will lay out like you want them to.
Thing work out in your first one because your radio button locations values are small enough to be visible in the panel. For those others, well, they are outside the bounds of their parent panels.
The long and short of it is that the Location property is always relative to the container, whether that is a Panel or a Form - the Location values for your panels are relative to the Form but the the Location values for your radio buttons are relative to whatever panel they are in.
MSDN Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location.aspx

List View doesn't render items in Details view

VariableBox = gcnew ListView();
VariableBox->Font = ScriptEditorOptions->FontSelection->Font;
VariableBox->Dock = DockStyle::Fill;
VariableBox->BorderStyle = BorderStyle::Fixed3D;
VariableBox->BackColor = ScriptEditorOptions->BCDialog->Color;
VariableBox->ForeColor = ScriptEditorOptions->FCDialog->Color;
VariableBox->DoubleClick += gcnew EventHandler(this, &ScriptEditor::VariableBox_DoubleClick);
VariableBox->View = View::Details;
VariableBox->MultiSelect = false;
VariableBox->CheckBoxes = false;
VariableBox->FullRowSelect = true;
VariableBox->HideSelection = false;
VariableBox->Tag = (int)1;
ColumnHeader^ VariableBoxName = gcnew ColumnHeader();
VariableBoxName->Text = "Variable Name";
VariableBoxName->Width = 70;
ColumnHeader^ VariableBoxType = gcnew ColumnHeader();
VariableBoxType->Text = "Type";
VariableBoxType->Width = 50;
ColumnHeader^ VariableBoxIndex = gcnew ColumnHeader();
VariableBoxIndex->Text = "Index";
VariableBoxIndex->Width = 50;
VariableBox->Columns->Add(VariableBoxName);
VariableBox->Columns->Add(VariableBoxType);
VariableBox->Columns->Add(VariableBoxIndex);
VariableBox->ColumnClick += gcnew ColumnClickEventHandler(this, &ScriptEditor::VariableBox_ColumnClick);
I have the above code in a WinForms application. The control is added to the main form directly. For some reason, it never renders any items or columns when the layout is set to Details - Only the scroll bars are visible. The following code is used to add items to its collection:
ListViewItem^ Item = gcnew ListViewItem("Qw");
Item->SubItems->Add("Int");
Item->SubItems->Add("10");
VariableBox->Items->Add(Item);
Switching to any other view (at either run-time or design-time) fixes the issue. Any ideas on why this is happening ?
EDIT: Bump! Or is that not allowed ?
Turns out that I was removing the column headers before the control was displayed.

Resources