Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Formatting entries in DataGridView selection
 

Formatting entries in DataGridView selection

I have a DataGridView (Dgvx1) and I am trying to change the format (currency in the code below) in the selected cells after the user clicks a ToolStripManuItem. My problem is that the format doesn't take effect. When I added a color change as shown in the code, the color did indeed change after selection but not the format. Am I missing something?

Thanks,

Rene

---------------------------------------------

Private Sub CurrencyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles CurrencyToolStripMenuItem.Click

Dim counter As Integer

Dim myStyle As New DataGridViewCellStyle

myStyle.Format = "C2"

myStyle.ForeColor = Color.Red

For counter = 0 To Dgvx1.SelectedCells.Count - 1

Dgvx1.SelectedCells(counter).Style = myStyle

Next

End Sub

RMan54  Thursday, July 20, 2006 4:15 PM

I have finally solved this. Based on some checking with the debugger and playing around with the code, I deducted the following: The default ValueType for the contents of each cell in a DatagridView is "String". In order for a numerical format to become effective, the ValueType needs to be "Decimal". The following code worked (Dgvx1 is my instance of the DataGridView):

For counter = 0 To Dgvx1.SelectedCells.Count - 1

With Dgvx1.SelectedCells(counter)

.ValueType = GetType(Decimal)

.Style.Format = "C2"

.Value = CSng(.Value)

End With

Next

Note that the conversion of the original cell content (string) needs to converted to a numerical value in order for this to work. Without this, the format takes only effect when a new value is entered into the cell.

I am unsure whether this is the best way to handle this, but it works. I could not find anything anywhere in the documentation that was helping for a simple thing like this.

RMan54  Thursday, July 27, 2006 8:57 PM

Working code:

For i As Integer = 0 To Me.DataGridView1.RowCount - 2

Me.DataGridView2.Columns.Add(Me.DataGridView1.Rows(i).Cells(0).Value, _

Me.DataGridView1.Rows(i).Cells(0).Value)

Dim DataGridViewCellStyle22 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle

DataGridViewCellStyle22.Format = "N2"

DataGridViewCellStyle22.NullValue = Nothing

Me.DataGridView2.Columns(Me.DataGridView2.ColumnCount - 1).DefaultCellStyle = DataGridViewCellStyle22

Next

hrubesh  Friday, July 21, 2006 1:43 PM

Thanks for trying to help but this is not the same situation. I don't have access to row or column numbers, only to the selected cells. And SelectedCells doesn't have a DefaultCellStyle property.

The interesting part is that when I assign a new value to each cell while inmy loop, that theformat is applied correctly. However, that's not what I need since the cells have already values in them.

RMan54  Friday, July 21, 2006 7:18 PM

When I check the cells that I want formatted in the loopwith the debugger, the ValueType and FormattedValueType properties are "object" and "string", respectively. I wonder whether my problem has something to do with this? I have not been able to solve this. Should be really simple. Any help is appreciated. Thanks.

RMan54  Tuesday, July 25, 2006 10:31 PM

This worked for changing the font color

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
For x As Integer = 0 To DataGridView1.SelectedCells.Count - 1
With DataGridView1.SelectedCells.Item(x)
.Style.ForeColor = Color.Red
End With
Next
End Sub

Ken Tucker  Wednesday, July 26, 2006 11:12 AM

As I stated in the original post, changing the font color works in my loop, but the format is not changed. Actually, I tried changing both ForeColor and BackColor and they take effect immediately. The problem is with the format only.

The format is changed when I also change the value in the loop but I don't want to do this because the cells already have their value.

RMan54  Wednesday, July 26, 2006 3:32 PM

I have finally solved this. Based on some checking with the debugger and playing around with the code, I deducted the following: The default ValueType for the contents of each cell in a DatagridView is "String". In order for a numerical format to become effective, the ValueType needs to be "Decimal". The following code worked (Dgvx1 is my instance of the DataGridView):

For counter = 0 To Dgvx1.SelectedCells.Count - 1

With Dgvx1.SelectedCells(counter)

.ValueType = GetType(Decimal)

.Style.Format = "C2"

.Value = CSng(.Value)

End With

Next

Note that the conversion of the original cell content (string) needs to converted to a numerical value in order for this to work. Without this, the format takes only effect when a new value is entered into the cell.

I am unsure whether this is the best way to handle this, but it works. I could not find anything anywhere in the documentation that was helping for a simple thing like this.

RMan54  Thursday, July 27, 2006 8:57 PM

You can use google to search for other answers

Custom Search

More Threads

• Howto paint DGVCheckBoxColumn for certain rows as something else
• uploading from tab seperated file into a grid Please help me
• Validating column or row changes not in a datagrid
• Q. Updating DataSet Bound to DataGridView
• Attaching a network database in MSSQL
• Dynamic columns binding to DataGridView - all rows show the same data
• DataGridView vs DataGrid bound to DataView
• How to : Paging in a C# DataGrid Windows Application ?
• Need DataGridView ComboBox DropDownClosed
• how can we get the data from the database in the combobox