GridView: forecolor of column header - c#

This Grid column property i can set from the designer, but not from the C # code !!! Why? Header.BackColor is working fine. I can also set any other color for the GridView elements except this.

If you are doing this at runtime you need to set the GridColumn.AppearanceHeader.Options.UserForeColor property to true as well as set the actual color. When you do this via the designer, the property is automatically set to true.
gridView1.Columns["FieldName"].AppearanceHeader.ForeColor = Color.Red;
gridView1.Columns["FieldName"].AppearanceHeader.Options.UseForeColor = true;

Ref: How do I set the color of a grid column header panel?
The header's BackColor property is ignored in the Skin paint style.
Add the following line to your code to address this issue:
gridControl1.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
gridControl1.LookAndFeel.UseDefaultLookAndFeel = false; // <<<<<<<<
gridView1.Appearance.HeaderPanel.Options.UseForeColor = true;
gridView1.Appearance.HeaderPanel.ForeColor = System.Drawing.Color.Red;
Reference these for detailed information:
Gridview - How to change a column's header background color
Why Appearance settings of DevExpress controls are not taken into account?
Devexpress column header color (each column different color), winform c#
The DevExpress skinning mechanism overrides the Appearance object settings. You have a couple options if you need a different background color for the column header:
Disable skinning on the GridControl by setting the GridControl.LookAndFeel.UseDefaultLookAndFeel property to False and the GridControl.LookAndFeel.Style property to "Flat".
Handle the GridView's CustomDrawColumnHeader and fill the column header with a different color.

Related

Change of background color in table column based on condition in Reporting

i have created table, where i need to change background color of columns based on value of first row of that column.
if first row value of particular column is 0 or hyphen(-), i need to change
1.) background color of that column rows to grey otherwise transparent
2.) All values in rows of that particular column should be filled with hyphen(-).
Below is what i have tried for, but not able to achieve my requirement.
Note: i should use Reporting API controls like XRTableCell. Other idea to achieve this is also welcomed.
using System;
using System.Drawing;
using DevExpress.XtraReports.UI;
private void tableCell12_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
{
XRTableCell tableCell = sender as XRTableCell;
double bancoAtivo = Convert.ToDouble(tableCell.Report.GetCurrentColumnValue("Campaign Count"));
if (bancoAtivo = 0)
{
tableCell.BackColor = Color.Grey;
}
else
{
tableCell.BackColor = Color.Transparent;
}
}
Your implementation is correct but there is some gap in your description of the issue. I also suggest you the same approach if you just want to change the color of a single column cells.
Handle the XRTableCell.BeforePrint event and change the Backcolor
or Text property for the sender object. To get the processed record,
use the XtraReportBase.GetCurrentRow or
XtraReport.GetCurrentColumnValue method.
You can either use FormattingRules as described in the Conditional Formatting help article or handle the BeforePrint event for the corresponding cells and set the BackColor for a sender object based on the processed value.
Refer these:
Customize the Report Appearance
Changing background of XRTableCell according to color value in data column.
How to change the XRTable background color?

horizontal autoscroll datagridview c#

I am not able to scroll horizontally in datagridview which throws the error "FirstDisplayedScrollingColumnIndex property cannot be set to an invisible column." I am binding a datatable to the datagridview and have set few columns invisible. Not sure what causes the issue.The vertical scroll is working without any issue. I have not added the scroll bars programatically and the datgridview is set inside a panel with dock property set to fill. Any specific reason for the issue,please state it down
if (hashcharges.ContainsKey("1"))
{
if (dataGridViewSummary1.Columns.Contains("Charge1"))
{
dataGridViewSummary1.Columns["Charge1"].HeaderText = hashcharges["1"].ToString().ToLower();
}
}
else
{
dataGridViewSummary1.Columns["Charge1"].Visible = false;
}
This is how i set the column invisble after binding datatable with datagridview.The error is not thrown in the same form instead it is shown in program.cs file.
You can set DataGridView.FirstDisplayedScrollingColumnIndex property to the index of the column that is the first column displayed. In your case definitely not the first one, because it is hidden.

the first cell in my table is automatic blue c#

When i populate my datagridview the first cell in the datagridview is always blue. The data grid view is populated with DataGridViewTextBoxColumn. it's like I have pressed the cell but I have not.it is like that with every table I create. i dont want it to be blue, can somebody help?
By default, there's always a selection on a DataGridView. If you don't want it, you can do:
// hides the "blue"
theDataGridView.ClearSelection();
// hides the "focus rectangle"
theDataGridView.CurrentCell = null;
As soon as the control gets focused again, you'll have a focus rect though.
This is because by default the selection mode will be singlecell you can change that. Also in code behind you can set the dgGridView.SelectedIndex = -1
or clearselection like that
Remove blue colored row from DataGridView WinForms
dataGridView.ClearSelection();
and then set the current cell property to null
dataGridView.CurrentCell = null; // for no focus

How can I display all text in Cell of GridView

I use GridView in my C# application, and the cell seems too small.
I find the prop of GridView, but I find nothing about it. How can I display all text in Cell?
Under Columns Collection property there is AutoSizeMode under Layout. And use for example DisplayedCells and see for yourself.
For Word Wrap you could set it programmatically like:
// Columns[1] for 2nd column based on your example.
dataGridView.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.true;
But don't set the AutoSizeMode as above or else the columns will still expand.
You could set it also manually under Columns Collection property and under Appearance select Default Cell Style and then set WrapMode to true.

WPF Datagrid with variable number of columns AND change background color depending on value

I'm trying to make a grid that represent bookings over a month(excel style).
For this I have used the WPF Datagrid and defined my column in C# code:
for (int i = 0; i < noOfDaysInMonth; i++)
{
DataGridTextColumn tmpColumn = new DataGridTextColumn
{
Header = (i + 1).ToString(),
Binding = new Binding("CellStrings[" + i + "]"),
};
overviewBookingsDataGrid.Columns.Add(tmpColumn);
Now this works fine. The problem I got is that I donĀ“t know how to style the background color of each cell depending on if the slot is fully booked, partially booked or empty.
All examples I have found has been in XAML and defines it togheter with the column and I don't know that translates to C#.
You need to define a datagridcell style in your XAML. Set some triggers based on the cell's Tag property. For instance if it is "Green" then color your cell green.
Once you have populated your datagrid you can iterate through your table in code, get the datagridcell for each required item, set the cell's tag to a proper value and the style triggers will take care of coloring the cell for you (if you want to clear the background color, set the Tag back to null). Or, if you want to avoid XAML, you can directly set the cells background.
There are plenty of examples on the web how to retrieve a datagridcell for a given item, but I'll give one word of warning - because the wpf datagrid is virtualized by default, you'll need to scroll your items into view and call UpdateLayout() on the item's datagridrow, before you can safely access a datagridcell for a given datagridrow.

Categories

Resources