How to refresh DataGridView cells style - c#

I have this simple piece of code on a windows form containing said DataGridView (dgvStatsTable) :
public void LoadStatsTable(DataTable statsTable)
{
dgvStatsTable.DataSource = statsTable;
var smallFont = new Font(dgvStatsTable.Font.FontFamily, dgvStatsTable.Font.Size * 0.67f);
dgvStatsTable.Rows[0].Cells[0].Style.Font = smallFont;
dgvStatsTable.InvalidateCell(0, 0);
//dgvStatsTable.Invalidate();
dgvStatsTable.Refresh();
}
Once that function has been called, my DataGridView contains the correct data to see.
However, that style change that I wanted is not showing (first cell in top-right corner has to have smaller text).
Why?
Is it because the table is set to a DataSource rather than building rows and columns?
Thanks!

The Solution to the problem was to write a handler for the DataGridView.CellFormatting Event
found in this MSDN article in the Setting Styles Dynamically section.

Here is a very nice answer from MSDN network, it appears that in order to have greater control you will need to override some functions.
http://msdn.microsoft.com/en-us/library/7fb61s43(VS.80).aspx

Related

Hide a Column from ColumnChooser

Currently I'm developing an application with devexpress, and I had set two columns to hide like this:
gridView1.Columns[2].Visible = false;
However, when I run my program, there is an option from the DataGrid called "Selector de Columnas" (in English ColumnChooser). In this option the two columns that I set to be hidden are shown. I don't want the user to see these columns but I still need them, so I just want to hide them.
I searched in the online documentation from Devexpress and they state here that columns can be hidden with the ShowInColumnChooser property. However I'm not able to hide these columns in the ColumnChooser. They don't show you any example, just this line of code:
public bool ShowInColumnChooser { get; set; }
I guess this is a property from some Devexpress control, however they don't state how to use it.
All the links you referred to are related to the WPF DXGrid, but as far as I can see from your screenshot, you are working with the XtraGrid (WinForms). If so, you should use the OptionsColumn.ShowInCustomizationForm property as follows:
column1.OptionsColumn.ShowInCustomizationForm = false;
Related help articles:
Column and Card Field Overview
Customization Form
If you want to prevent a specific column from being showed/hidden, set the column's properties OptionsColumn.ShowInCustomizationForm and OptionsColumn.AllowShowHide to false.

Not able to hide first column in datagridview?

I have a datagridview in a windows form which contains some columns. And I want to hide the Ist column(CompanyID) through the code behind.
But Ist column is not hiding.
Have tried below 2 things :
dgvVendorDetails.Columns["CompanyID"].Visible = false;
And:
dgvVendorDetails.Columns[0].Visible = false;
I don't know the reason behind this. I have searched a lot but got no solution.
Both of these syntax are corrects and should work:
dgvVendorDetails.Columns["CompanyID"].Visible = false;
dgvVendorDetails.Columns[0].Visible = false;
My guess is that you are using the DataGridView.AutoGenerateColumns functionnality and even if you set the DataSource property, the DatagridView won't create columns until the grid is displayed.
So it's possible that:
you try to access columns that do not exist yet (but the code should raise an exception)
or you access valid columns, but they are replaced when the grid is bound again and so your code has no effect (probably your case since you do not mention an exception).
If so, the solution is to use the DataBindingComplete Event.
See also these related issues:
Why DataGridColumn not getting removed from DataGridView
Datagirdview and tabcontrol issue
Strange issue with a datagridview and a tabcontrol C#
DataGridView has no columns
EDIT
As #brikovich pointed out, another solution is not not use the AutoGenerated columns but create them and configure them at design time or at runtime.
This thread How to select visible columns in Datagridview bound to DataTable can help you to achieve this and/or make a choice between these two options.
Set autogenerate columns to false and then add each column one by one to the grid. Then set the column you don't want to see to visible = false. No need for code behind.
Try this:
VB.net:
Private Sub dgvVendorDetails_ColumnAdded(sender As Object, e As DataGridViewColumnEventArgs) Handles dgvVendorDetails.ColumnAdded
If e.Column.Name = "CompanyID" Then dgvVendorDetails.Columns("CompanyID").Visible = False
End Sub
C#:
private void dgvVendorDetails_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
if (e.Column.Name == "CompanyID")
dgvVendorDetails.Columns("CompanyID").Visible = false;
}

Enabling and disabling of columns in Infragistics UltraGrid

I have an Infragistics grid and I want to disable and enable some columns based upon some requirement. I have read some articles that say to use AllowUpdate = DefaultableBoolean.True but it did not work for me.
I suppose that when you talk of disabled columns you mean disable editing in these columns.
Also you don't specify the language, so I will use C#
UltraGridColumn c = grdWork.DisplayLayout.Bands[0].Columns["YourColumnName"];
c.CellActivation = Activation.NoEdit;
c.CellClickAction = CellClickAction.CellSelect;
The property CellActivation could also be set to Activation.Disabled or Activation.ActivateOnly.
The property CellClickAction allows to set an appropriate selection status for the cell clicked. You could use CellSelect or RowSelect. (This last one, to mimic the behavior of a ListBox)
As usual, the real difficulty is to find the correct property. Then Intellisense will give you a quick and fair explanation of the meaning of these values.
If you just want to show and hide the columns as needed then you can try the following.
UltraGrid myGrid = new UltraGrid();
//Bind to your data here
myGrid.DisplayLayout.Bands[0].Columns["ColumnName"].Hidden = true;

How to end the DataGridView Cells Edit Mode automatically?

this is a follow up question from my previous one. you can find it here.
I got another problem after finally solving my previous one, when I assign the button to add the value into a new row in DataGrid, the entire cell will be on edit mode, until I clicked on other cell and filled it and/or tabbed till the end of the row (well apparently this one doesn't work), then it will end the edit mode.
I'm using dataGridView.BeginEdit(true); to begin the edit mode so that I could parse a value into the textbox (see my previous question). So, if I insert another value and press the button, the new value will replace the old value that was inserted previously because it is currently still on edit mode.
I was trying to use dataGridView.EndEdit();, dataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);, cell.DataGridView.EndEdit() and cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit); but apparently that doesn't end the edit mode :(
What I wanted is when I press the button, the value inside the textbox will be inserted into the first textbox column (this one already worked). Then I don't have to click or fill the other column to end the edit mode. So, I'll just type anything into the textbox and press the button only until I wanted to stop. After that I begin to fill the other column.
does anyone know how to solve this?
EDIT 1: do you see the difference? look at the red circle, the top one is currently in edit mode (because it has a * after the arrow). the bottom one is not in edit mode (I did it manually by picking an item from the combobox).
Here is my code as requested from my previous question:
private void button1_Click(object sender, EventArgs e)
{
this.surat_jalanDataGridView.AllowUserToAddRows = true;
string tokNum = this.textBox1.Text;
if (this.textBox1.Text != "")
{
foreach (DataGridViewRow sjRow in this.surat_jalanDataGridView.Rows)
{
int RowIndex = surat_jalanDataGridView.RowCount - 1;
DataGridViewRow R = surat_jalanDataGridView.Rows[RowIndex];
DataTable table = new DataTable();
DataRow newRow = table.NewRow();
table.Rows.Add(newRow);
DataGridViewCell cell = R.Cells[2];
this.surat_jalanDataGridView.CurrentCell = cell;
this.surat_jalanDataGridView.BeginEdit(true);
R.Cells[2].Value = tokNum;
cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
}
}
this.surat_jalanDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
}
EDIT 2: So, I drag and drop the surat_jalan from the data sources into my windows form. then it automatically become a datagrid with the property name surat_jalanDataGridView and the data source is surat_jalanBindingSource
Sorry about the delay. After seeing how you set up your binding to your DataGridView I can definately give you better guidance on how to edit the data that the grid is bound to. When you dragged the table from the data sources view in Visual Studio and dropped it on the DataGridView, Visual Studio did several things for you. It is important that you at least understand the basics of what was done so that you understand how you can manipulate you data moving forward. This
MSDN article how to setup binding to windows forms controls from Visual Studio. The last section describes what you are doing. The last sentences "The DataGridView control is now bound to the table that you have dragged onto it. A DataSet, TableAdapter, and BindingSource appear in the component tray." are the important ones. Since Visual Studio generated code that bound your control to your table you should edit the data directly to update your data grid view. In this case you should work with the generated DataSet (I'm assuming it was named surat_jalanDataSet). Here is a description of how to edit data in a DataSet. For your specific case Adding Rows. Please let me know if this helps you acheive your goals.
My work around to force a Cell out of EditMode is to toggle the CurrentCell.ReadOnly property.
this.dataGridView.EndEdit();
this.dataGridView.CurrentCell.ReadOnly = !dataGridView.CurrentCell.ReadOnly;
this.dataGridView.CurrentCell.ReadOnly = !dataGridView.CurrentCell.ReadOnly;
Similar technique works with other components that do not have explicit and functional EndEdit or Commit type methods.
For example, you can end the DateTimePicker's text edit mode by toggling its dateTimePicker.Enabled property.
//AJ

List of notices, which control is best choice?

I want to use a control to notice users what are happening and what've done. It's like a multiline textbox, each line contains a notice, such as:
Connecting to database...done
Current datetime:
Inserting data into database 10/1000...done
Inserting data into database 20/1000...done
...
Inserted data into database
Current datetime:
I tried textbox, but it seems to be too complicated to manipulate many lines.
Is there a better control to do this? If textbox was the best choice, so how can I do this?
Thanks in advance!
I would use a listbox. Set it to SelectionMode.None. Add to the items collection. If you have enough lines to scroll off the screen you will need to adjust the scrollbar to the bottom manually. Do so by setting the listbox TopIndex to the number of lines - 1.
I've seen people use ListBoxes and TextBoxes. I usually use RichTextBoxes because then I can easily apply some simple styles and colouring, but it's probably overkill.
I found this method in one of my "just-playing-around" projects:
private void writeToLog(String text, SolidColorBrush color)
{
TextRange tr = new TextRange(logTextBox.Document.ContentEnd, logTextBox.Document.ContentEnd);
tr.Text = text;
tr.ApplyPropertyValue(TextElement.ForegroundProperty, color);
}

Categories

Resources