Repeated calls to CellValueNeeded - c#

I have a vertically and horizontally scrollable DataGridView on a form.
I use virtual mode because the underlying datatable is huge.
When I scroll right, if the last column is not fully shown in the view, then I see repeated calls to CellValueNeeded.
How can I fix this?
My thoughts:
Why is CellValueNeed being repeatedly called for a partially visible column anyway? Perhaps I can fix the cause of this.
Within CelValueNeeded - can I detect it is partially visible and return without processing? Both "Displayed" and "Visible" are true when I check the cell values.
My code:
private void grid_Data_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
Console.WriteLine("CellValue: " + e.RowIndex + " " + e.ColumnIndex);
if (e.RowIndex > Grid.Rows.Count - 1)
return;
DataGridView gridView = sender as DataGridView;
e.Value = Grid.Rows[e.RowIndex][e.ColumnIndex];
gridView.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex).ToString();
}
EDIT1:
After Digitalsa1nt's answer I found a way to fix the issue. It is complicated because the first column is treated differently to the last column. AND it makes a difference if you are setting RowHeaders.
In CellValueNeed above, I now return if the following function is true.
private bool IsPartiallyVisible(DataGridView gridView, DataGridViewCellValueEventArgs e)
{
if (gridView.FirstDisplayedScrollingColumnIndex == e.ColumnIndex)
{
if (gridView.FirstDisplayedScrollingColumnHiddenWidth != 0)
{
return true;
}
}
bool sameWidth = gridView.GetColumnDisplayRectangle(e.ColumnIndex, false).Width == gridView.GetColumnDisplayRectangle(e.ColumnIndex, true).Width;
return !sameWidth;
}

Looking at the MSDN documentation for CellValueNeeded it reads as though it's a standard visual event, simply fires as soon as a cell becomes "visible", i don't think it defines the logic it uses to understand visual partiality. It just seems as though it tries to prepare for the cell to become fully "in-view". I suspect any intermediary states are not exposed.
That said there are some suggestions here (SO reply) and here (weird web-blog) that mention the use of DataGridView.GetColumnDisplayRectangle with the intention of determining if the rectangle of the cell is within the bounds of the screen.
Here's a snippet from the web blog:
The second parameter to GetColumnDisplayRectangle is called
CutOverFlow, which is a Boolean value that controls whether the
function returns the complete column rectangle (even if the column is
not completely visible) or only the portion of the column's rectangle
that is visible.
By calling this method twice, once with CutOverFlow set to true and
once with it set to false, you can create a function that compares the
results and returns a Boolean value when the column is only partially
visible:
Return dg.GetColumnDisplayRectangle(columnindex, False).Width = _
dg.GetColumnDisplayRectangle(columnindex, True).Width
This would allow you to stop processing when grid_Data_CellValueNeeded is called and the above returns false based on the last cells location.

Related

ToolStripMenuItem strange behavior on visible property [duplicate]

This question already has an answer here:
How to set a ToolStripMenuItem Visible in code?
(1 answer)
Closed 1 year ago.
I have encounter that issue a long time ago and again now. I cannot figure out what special thing had to be done to edit the ToolStripMenuItem control a Visible property while inside a form load.
in designer.cs
private System.Windows.Forms.ToolStripMenuItem mnuExport;
//
// mnuExport
//
this.mnuExport.Name = "mnuExport";
this.mnuExport.Size = new System.Drawing.Size(116, 22);
this.mnuExport.Text = "Export";
this.mnuExport.Visible = false;
this.mnuExport.Click += new System.EventHandler(this.mnuExport_Click);
in the form code
public partial class MainBuilder : Form
{
private void MainBuilder_Load(object sender, EventArgs e)
{
mnuExport.Visible = true;
}
}
In the code above export is a menu item of type ToolStripMenuItem which trough the property grid in the form design mode I have modified it's Visible property to false. So in the form load I want to switch to the visible state to true. I thought it be easy so I coded all the logic around and it failed. So I decided to remove all my code and simply hardcode the set to true and to my surprise this does not work.
When I put the breakpoint on the line in the form_load I clearly see it's false and if it let the line run the value is still false.
I recall seeing this issue in the past but cannot find anything about it. I also tried with 4-5 other menu item in that window and they all show the same behavior.
EDIT
just tried to put visible = true in the designer.cs instead and in the form_load still tells me the value is false. There is some major issues here.
As mentioned in the comments the property Visible getter does not reflect the actual inner property value until later in the lifecycle. It is still unavailable in the form_loaded event. My main problem is that one menu visibility state was based on if all sub menus are not visible then it should not either. To do so I was iterating on it's child and checking the Visible property. The problem is them having Visible to false due to the way it works the parent set it's own Visible to false as well.
I don't like the solution but that's the only way to make it work
// get all sub menus
var subMenus = mnuExport.DropDownItems.Cast<ToolStripItem>().ToList();
// create a list of bool that will store the visible state I want the controls to have. put them to true by default
var menusVisibleStates = Enumerable.Repeat(true, menus.Count).ToList();
// set the visibility state of each sub menus
for (int i = 0; i < menus.Count; i++)
{
// some logic is here that choose true or false but is irrelevant here
var visibleStateIWant = true;
// set the visible state stored
menusVisibleStates[i] = false;
// set the actual control value that will change later on
menus[i].Visible = false;
}
/// now here I use the state I have specified and NOT the menu object property
mnuExport.Visible = menusVisibleStates.Any(o => o);

C# Window From Autoselects Text

I have not dealt with WinForms for a long time.
Now I'm stuck with something trivial but cannot figure it out.
I have a Winform and when a Timer Tick happens I want to show a message in a new form message box:
frmMessage frmM = new frmMessage();
frmM.txtMessage.Text = ConfigurationSettings.AppSettings["Message"];
frmM.Show();
It works but the text in the textbox shows as selected(with a blue background).
I tried
txtMessage.SelectionLength = 0;
Did not help.
Also tried to set focus to a different control, did not help either.
for now, as a workaround, I will use a Label.
This is a consequence of the way TextBox Class is implemented. If a selection is not specifically set, all text will be selected when the control gets focus.
From TextBox.OnGotFocus:
Protected override void OnGotFocus(EventArgs e) {
base.OnGotFocus(e);
If (!selectionSet) {
// We get one shot at selecting when we first get focus. If we don't
// do it, we still want to act Like the selection was set.
selectionSet = true;
// If the user didn't provide a selection, force one in.
If (SelectionLength == 0 && Control.MouseButtons == MouseButtons.None) {
SelectAll();
}
}
Additionally due to the way the SelectionLength Property is implemented, setting that property to zero does not set the selectionSet` flag as it is already zero.
Instead, set the TextBox.SelectionStart Property immediately after setting the text as this will set that flag.
txtMessage.SelectionStart = 0;
However, your work-a-round of using a Label to display a message is much more appropriate than using an input control.
This is not the best answer but it works. You can try this
frmMessage frmM = new frmMessage();
frmM.txtMessage.Text = "";
frmM.txtMessage.AppendText(ConfigurationSettings.AppSettings["Message"]);
frmM.Show();

Changing column name dynamically

I have a Header / Detail custom screen where I'm manipulating which grid columns display based on a dropdown selection in the header. This works fine, but now I'd like to change a few column names as well. Using the documented syntax, I cannot get this to work. I can't see what I'm doing wrong - nothing seems to make any difference. I've attached to process and put a break point at this event, and it's hitting the line - but the system seems to just ignore it:
protected virtual void ACMappingHeader_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
{
var mh = (ACMappingHeader)e.Row;
if (mh == null) return;
if (mh.MappingType == "Option1")
{
PXUIFieldAttribute.SetDisplayName<ACMappingDetail.target1CD>(this.MappingDetail.Cache, "Target");
Thanks...
Your display name routine looks correct however to make sure the column names actually update you need to do the following:
In the page source, you need to set the "RepaintColumns=true" value on the grid. This can be done via customization manager or directly from your ASPX source. - This tells the grid to refresh the columns after a callback allowing the headers to actually redisplay.
You can refer below example to dynamically change Grid Column Header.
Below example works with Screen PM401000 – Project Transactions Inquiry
public class TransactionInquiryExt : PXGraphExtension<TransactionInquiry>
{
public void TranFilter_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected baseInvoke)
{
if (baseInvoke != null)
baseInvoke(cache, e);
PX.Objects.PM.TransactionInquiry.TranFilter row = e.Row as PX.Objects.PM.TransactionInquiry.TranFilter;
if (row == null) return;
PXUIFieldAttribute.SetDisplayName<PMTran.description>(Base.Transactions.Cache,
row.ProjectID.HasValue ? "Description for Project Tran" : "Description");
}
}
Make sure to set RepaitColumns property to True of PXGrid Control.

Display String in a Database as a Checkbox

I have a Windows Form that was mainly made using the graphical editor. It is connected to a database called, Database1. One of the tables in the database is called Table1, and contains the column CheckBox1. When I connected the form to the database, Database1DataSet.xsd and Database1DataSet.Designer.cs were automatically created.
CheckBox1 can either hold "Yes" or blank (this wasn't my decision). I would like to make a checkbox checked if the value in the CheckBox1 column is "Yes", and unchecked if the value is blank. If I drag a bound checkbox onto the form, it doesn't work because I assume that the values in the column need to be either 1 or 0. So I'm trying to work around this.
In my form, I have the following
// Form Constructor
public myForm()
{
// Initializes all the components in the form
InitializeComponent();
// Change the checkboxes checked state
this.myCheckBox.Checked = myCheckBox_Update();
}
// Method for determining if the checkbox should be checked
private bool myCheckBox_Update()
{
// This SHOULD bring in the current record of the database
DataRowView current = (DataRowView)this.Table1BindingSource.Current;
try
{
// This SHOULD determine if the value in the CheckBox1 field has a value
return current.Row["CheckBox1"].ToString().Length > 0;
}
catch (NullReferenceException ex)
{
MessageBox.Show("NullReferenceException was thrown!", "Error");
return false;
}
}
In the InitializeComponent() function, there is the following
// This line is generated when I drag a bound checkbox to the form
// this.myCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.Table1BindingSource, "CheckBox1", true));
// For the miscellaneous information about the checkbox
this.myCheckBox.AutoSize = true;
this.myCheckBox.Location = new System.Drawing.Point(3, 3);
this.myCheckBox.Name = "myCheckBox";
this.myCheckBox.Size = new System.Drawing.Size(92, 23);
this.myCheckBox.TabIndex = 0;
this.myCheckBox.Text = "Check Box";
this.myCheckBox.UseVisualStyleBackColor = true;
However, it keeps throwing a NullReferenceException. I'm assuming that this is because this.Table1BindingSource.Current cannot be cast as a DataRowView.
I have looked at several other posts on SO that are somewhat related to this problem (e.g. This post, or this post), but I haven't found anything that has worked so far. The second link's answer doesn't work because I am iterating through the records with this.Table1BindingSource.MoveNext();, and I won't know the index.
Can someone steer me in the right direction? I'd really appreciate it
EDIT: The BindingSource is initialized with this
private System.Windows.Forms.BindingSource Table1BindingSource;
this.Table1BindingSource = new System.Windows.Forms.BindingSource(this.components);
I'll put this in an answer since it's not fit for a comment.
You may also try checking the value for null in your try statement.
try
{
// This SHOULD determine if the value in the CheckBox1 field has a value
object val = current.Row["CheckBox1"];
if (val == null)
{
return false;
}
else
{
return current.Row["CheckBox1"].ToString().Length > 0;
}
}
Or at least set a breakpoint at the if (val == null) statement and make sure val has a value that you would expect.
If by 'blank' you mean a String.Empty, then your code would work as you expect it to, but if blank means null, then you'll have to check for null, you can't call ToString() on a null value.
What I ended up doing is just changing the values in my database from "Yes" and blank to 1 and 0. Then in the InitializeComponent() method, I added the following line (or it might be generated for you if you drag it from the data source panel)
this.myCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.Table1BindingSource, "CheckBox1", true));

Infragistics UltraGrid: GroupBy row ForeColor changes when clicked

Using Infragistics UltraGrid in WinForms in C#:
I am conditionally changing the color of the ForeColor of some GroupByRows on a grid. When the user clicks the row, the color changes back to the active/selected/hot tracked/whatever color until they click on something else. I'd like the text color of the rows that I have conditionally colored to never change. Here's how I'm setting the color:
Row.Appearance.ForeColor = System.Drawing.Color.Orange;
Any idea how to make it stick even when the row is clicked?
Thanks!
This can be done with a draw filter to set the fore color of the description since this would apply at all times.
A simple example is the following draw filter which will make all group by rows that have an integer value that is even orange:
public class GroupByRowDrawFilter:IUIElementDrawFilter
{
public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
{
GroupByRowDescriptionUIElement element = (GroupByRowDescriptionUIElement)drawParams.Element;
if (element.GroupByRow.Value is int)
{
int value = (int)element.GroupByRow.Value;
if (value % 2 == 0)
{
drawParams.AppearanceData.ForeColor = Color.Orange;
}
}
return false;
}
public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams)
{
if (drawParams.Element is GroupByRowDescriptionUIElement)
return DrawPhase.BeforeDrawElement;
return DrawPhase.None;
}
}
To apply the draw filter use the following line of code:
this.ultraGrid1.DrawFilter = new GroupByRowDrawFilter();
Note that this approach requires that the condition be in the draw filter. If this doesn't work for you, you could modify your logic where you are currently setting the ForeColor to set the Tag property of the GroupByRow instead and then check the Tag property in the draw filter to determine if you need to apply your logic.
I think you should also change the
grd.DisplayLayout.Override.SelectedRowAppearance.ForeColor = System.Drawning.Color.Orange;
or better
grd.DisplayLayout.Override.GroupByRowAppearance.ForeColor = System.Drawning.Color.Orange;
sorry, but I'm away from a PC where I can test.
Usually these properties could be changed effectively in the InitializeLayout event where you get the Layout object inside the event arguments.
e.Layout.Override.GroupByRowAppearance.ForeColor = Color.Orange;
EDIT: At the moment the only solution that I have found is the following
private void grd_BeforeRowActivate(object sender, RowEventArgs e)
{
// You need to add the additional logic required by you to
// determine which rows need to have the forecolo changed...
if (e.Row.IsGroupByRow == true)
grd.DisplayLayout.Override.ActiveRowAppearance.ForeColor = Color.Orange;
else
grd.DisplayLayout.Override.ResetActiveRowAppearance();
}
Infragistics says:
the "Ability to have active and selected conditional appearances for the GroupByRows" has been determined to be a new product idea

Categories