Devexpress xtragrid column set mask with repositoryItemTextEdit - vb.net

I'm using devexpress 11.1.4. version also xtraGrid control, my code is vb.net
I have set repositoryItemTextEdit to display mask into grid column. I whant to set date format:
"dd.mm.yyyy "
At the time of typing looks good , however when cells lose focus it changes in format:
"mm.dd.yyyy"
Also sometimes they just emptied or when i choose for example:
10.05.2015.
it change the value that it looks like this: 01.05.2015.
I do not know why
Here is my code:
Public dateWithTextEdit As RepositoryItemTextEdit = New RepositoryItemTextEdit
dateWithTextEdit .Mask.UseMaskAsDisplayFormat = True
dateWithTextEdit .Mask.AutoComplete = XtraEditors.Mask.AutoCompleteType.Strong
dateWithTextEdit .Mask.MaskType = XtraEditors.Mask.MaskType.DateTime
dateWithTextEdit .Mask.EditMask = "dd.mm.yyyy"
DGV.RepositoryItems.Add(dateWithTextEdit )
DGV.DataSource = dataTable
With dgvVIEW
.OptionsBehavior.AllowAddRows = DefaultBoolean.True
.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top
.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True
.Columns(0).Name = "PROMDDOK"
.Columns(0).FieldName = "PROMDDOK"
.Columns(0).Caption = "DATUM DOKUMENTA"
.Columns(0).ColumnEdit = dateWithTextEdit
.Columns(0).Visible = True
.Columns(0).Width = 120
End With

Problem no. 1.: your edit mask is dd.mm.yyyy. mm stands for minutes, you have to use dd.MM.yyyy.
If this doesn't help, set the column's display format as well.
Code in C#:
columns[0].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
columns[0].DisplayFormat.FormatString = "dd.MM.yyyy";

According to the DevExpress online documentation, RepositoryItemTextEdit.Mask Property is used to format cell values in edit mode.To format cell values in display mode, use solutions described in the Formatting Cell Values document.

Related

Autosize Rows for Datagridview in VB.net not working

Hi, i'm making a memo type of data grid with Dynamic data from Database
and somehow i cannot make the rows auto resize.
i tried pretty much everything, here's some snippet of my code which i put on Form Load sub:
DataGridView2.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
DataGridView2.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
DataGridView2.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView2.Columns(2).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
Which came out like this:
After That i tried using this on DataGridView2.CellPainting Event
If e.Value Is Nothing Then Return
Dim s = e.Graphics.MeasureString(e.Value.ToString(), DataGridView2.Font)
If s.Width > DataGridView2.Columns(e.ColumnIndex).Width Then
Using gridBrush As Brush = New SolidBrush(DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As Pen = New Pen(gridBrush)
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
e.Graphics.DrawString(e.Value.ToString(), DataGridView2.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault)
DataGridView2.Rows(e.RowIndex).Height = CInt((s.Height * Math.Ceiling(s.Width / DataGridView2.Columns(e.ColumnIndex).Width)))
e.Handled = True
End Using
End Using
End If
which came out like this:
Tried fiddling with all properties but i seems unable to figure it out, Thank you in advance
Here's the process I followed:
Dropped a new datagridview on a form (actually I dragged it out of the datasources window, because I already had a dataset in the project)
Chose edit columns, chose a column, clicked [...] next to DefaultCellStyle for one of the string columns, set WrapMode to True
Set AutoSizeRowsMode of the DGV to AllCells
Inserted a long string into the underlying datatable the dgv was bound to:
And the result wrapped:
You can consider centering the elements in the DataGridView:
DataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
DataGridView2.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Result looks like:

How to Add Combobox in datagridview only for header?

This is my code - I want to add combobox for my second column. So that whatever value user enter in that column I can use for further operation. I have changed Items in combo intentionally.
Dim cmbHeaderCell As New ComboBox
cmbHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList
cmbHeaderCell1.Visible = True
cmbHeaderCell1.Items.Add("India")
cmbHeaderCell1.Items.Add("China")
DGrdVLoadStb.Columns(1).Width = cmbHeaderCell1.Width
cmbHeaderCell1.Location = DGrdVLoadStb.GetCellDisplayRectangle(1, -1,True).Location
cmbHeaderCell1.Size = DGrdVLoadStb.Columns(1).HeaderCell.Size
cmbHeaderCell1.SelectedIndex = 0
Here I am getting the location (0, 0) from GetCellDisplayRectangle(1, -1, True) method. I have checked many related questions on stackOverflow but I didn't get perfect answer please help me. Thank you in advance.
As I wrote in comment, I managed to do this by using,
1. width of first column of datagridview adding 2 to it and
2. making it as X of my combobox loacation and Y as 2.
here is my code,
Dim cmbHeaderCell1 As New ComboBox
cmbHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList
cmbHeaderCell1.Visible = True
cmbHeaderCell1.Items.Clear()
cmbHeaderCell1.Items.Add("India")
cmbHeaderCell1.Items.Add("China")
DGrdVLoadStb.Columns(1).Width = cmbHeaderCell1.Width
'Dim X As Integer = DGrdVLoadStb.GetCellDisplayRectangle(1, -1, False).Location.X
'Dim Y As Integer = DGrdVLoadStb.GetCellDisplayRectangle(1, -1, True).Location.Y
cmbHeaderCell1.Location = New Point(datagridview1.Columns(0).Width + 2, 2)
cmbHeaderCell1.Size = DGrdVLoadStb.Columns(1).HeaderCell.Size
cmbHeaderCellStressRate.DropDownStyle = ComboBoxStyle.DropDownList
DGrdVLoadStb.Controls.Add(cmbHeaderCell1)
cmbHeaderCell1.SelectedIndex = 0
If any one have better solution please post it I will accept it.

Winforms - Datagridview dynamically change linkbutton text

I am using Winforms datagridview (n-tier architecture) to populate from a dataset. I want to have a linkbutton as the last column which should change its text based on the value of two columns. While I have managed to get the linkbutton, I cannot get the value to change. I need the value to change so that when the linkbutton is clicked, it should open up different windows. My code is as below
Private Sub ShowProductRequisitionsInListView(ByVal data As DataSet, ByVal dgv As DataGridView)
dgvRequisitionDetails.Columns.Clear()
dgvRequisitionDetails.DataSource = Nothing
dgvRequisitionDetails.DataSource = data.Tables(0)
dgvRequisitionDetails.Columns(0).Width = 80
dgvRequisitionDetails.Columns(0).HeaderText = "Product Code"
dgvRequisitionDetails.Columns(1).Width = 180
dgvRequisitionDetails.Columns(1).HeaderText = "Product Name"
dgvRequisitionDetails.Columns(2).Width = 150
dgvRequisitionDetails.Columns(2).HeaderText = "Sales UOM"
dgvRequisitionDetails.Columns(3).Width = 60
dgvRequisitionDetails.Columns(3).HeaderText = "Qty. Reqd."
dgvRequisitionDetails.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvRequisitionDetails.Columns(3).DefaultCellStyle.Format = "N3"
dgvRequisitionDetails.Columns(4).Width = 105
dgvRequisitionDetails.Columns(4).HeaderText = "Qty. In Stock"
dgvRequisitionDetails.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvRequisitionDetails.Columns(4).DefaultCellStyle.Format = "N3"
Dim lnk As DataGridViewLinkColumn = New DataGridViewLinkColumn
dgvRequisitionDetails.Columns.Add(lnk)
lnk.HeaderText = "Action"
lnk.Text = "Click Here"
lnk.Name = "lnkAction"
lnk.UseColumnTextForLinkValue = True
End Sub
If the difference between the QtyReqd and QtyInStock is a negative value, the link button should read as "Not Available" and if there is enough stock it should read as "Available". Based on these two values different windows will open upon clicking the link
I tried to check the condition in the DataBindingComplete event, but its not working. What am I doing wrong here?
CL
I think that the easiest way is to add a computed column to the data table and use this as the data bound item for the link column. Something like this:
Dim expr As String = "IFF((([QtyInStock] - [QtyReqd]) >= 0), 'Available', 'Not Available')"
data.Tables(0).Columns.Add("Action", GetType(String), expr)
In the datagrid designer add a handler for: OnRowDataBound="someEvent_RowDataBound"
In the code behind you will have a function:
protected void someEvent_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
If(e.Row.RowType == DataControlRowType.DataRow){
((linkbutton)e.Row.Cells[someindex].controls(0)).Text = "somevalue";
}
}

Datagridview optimization VB.Net

I just want to ask if there is another alternative for filling up the cells in the datagridview. Currently I'm using this code :
For i = DataGridView1.CurrentCell.RowIndex To x - 1
DataGridView1.Rows(i).Cells("LastName").Value = empcoll.Item(i).LastName
DataGridView1.Rows(i).Cells("FirstName").Value = empcoll.Item(i).FirstName
DataGridView1.Rows(i).Cells("MiddleName").Value = empcoll.Item(i).MiddleName
DataGridView1.Rows(i).Cells("CreatedBy").Value = empcoll.Item(i).CreatedBy
DataGridView1.Rows(i).Cells("CreateDate").Value = empcoll.Item(i).CreateDate
DataGridView1.Rows(i).Cells("Status").Value = empcoll.Item(i).Status
DataGridView1.Rows(i).Cells("DailySalary").Value = empcoll.Item(i).DailySalary
DataGridView1.Rows(i).Cells("BirthDate").Value = empcoll.Item(i).BirthDate
Next i
but when I use it for databases with a large number of records, it tends to load slow and hangs up.
You can use the SQLDataSource control (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx) and then bind the DataGridView to the SQLDataSource. It is described here: http://www.vkinfotek.com/gridview/bind-gridview-sqldatasource-control.html.
In general, you should be Data Binding the columns to the properties on the objects instead of the method you're currently using.
It would look something like this
LastNameColumn.DataPropertyname = "LastName"
FirstNameColumn.DataPropertyname = "FirstName"
....
DataGridView1.DataSource = MyListofEmployeeObjects
As far as the load speed, there are two options: pagination, and virtual mode.
With pagination, you can pull X of Y records from the database at a time and display them in the grid.
Virtual mode ( http://msdn.microsoft.com/en-us/library/ms171622.aspx ) enables the DataGridView to handle automatic pulling/pushing of the records from/to the database so that you do not have to load all records at once.
Well, I prefer to use the DatagridView.Rows.Add method for unbounded DataGridViews.
Something like this:
Dim loData(7) as object
DataGridView1.Rows.Clear()
For i = DataGridView1.CurrentCell.RowIndex To x - 1
loData(0) = empcoll.Item(i).LastName
loData(1) = empcoll.Item(i).FirstName
loData(2) = empcoll.Item(i).MiddleName
loData(3) = empcoll.Item(i).CreatedBy
loData(4) = empcoll.Item(i).CreateDate
loData(5) = empcoll.Item(i).Status
loData(6) = empcoll.Item(i).DailySalary
loData(7) = empcoll.Item(i).BirthDate
DataGridView1.Rows.Add(loData)
Next i
I usually declare en enum to give a meaning name to each array element and make the code cleaner:
private enum eCols
LastName
Firstname
MiddleName
CreatedBy
CreateDate
Status
DailySalary
BirthDate
end enum
Dim loData(7) as object
DataGridView1.Rows.Clear()
For i = DataGridView1.CurrentCell.RowIndex To x - 1
loData(eCol.LastName) = empcoll.Item(i).LastName
loData(eCol.FirstName) = empcoll.Item(i).FirstName
loData(eCol.MiddleName) = empcoll.Item(i).MiddleName
loData(eCol.CreatedBy) = empcoll.Item(i).CreatedBy
loData(eCol.CreateDate) = empcoll.Item(i).CreateDate
loData(eCol.Status) = empcoll.Item(i).Status
loData(eCol.DailySalary) = empcoll.Item(i).DailySalary
loData(eCol.BirthDate) = empcoll.Item(i).BirthDate
DataGridView1.Rows.Add(loData)
Next i

DataGridView column order does not seem to work

I have a DataGridView bound to a list of business objects:
Dim template As New IncidentTemplate
Dim temps As List(Of IncidentTemplate) = template.LoadAll
Dim bs As New BindingSource
If Not temps Is Nothing Then
bs.DataSource = temps
Me.dgvTemplates.DataSource = bs
End If
I then add an unbound button column and make some of the bound columns invisible:
Dim BtnCol As New DataGridViewButtonColumn
With BtnCol
.Name = "Edit"
.Text = "Edit"
.HeaderText = String.Empty
.ToolTipText = "Edit this Template"
.UseColumnTextForButtonValue = True
End With
.Columns.Add(BtnCol)
BtnCol = Nothing
For Each col As DataGridViewColumn In Me.dgvTemplates.Columns
Select Case col.Name
Case "Name"
col.DisplayIndex = 0
col.FillWeight = 100
col.Visible = True
Case "Description"
col.DisplayIndex = 1
col.FillWeight = 100
col.Visible = True
Case "Created"
col.DisplayIndex = 3
col.HeaderText = "Created On"
col.DefaultCellStyle.Format = "d"
col.FillWeight = 75
col.Visible = True
Case "CreatedBy"
col.DisplayIndex = 2
col.HeaderText = "Created By"
col.FillWeight = 75
col.Visible = True
Case "Edit"
col.DisplayIndex = 4
col.HeaderText = ""
col.FillWeight = 30
col.Visible = True
Case Else
col.Visible = False
End Select
Next
This seems to work reasonably well except no matter what I do the button column (Edit) always displays in the middle of the other columns instead of at the end. I've tried both DGV.Columns.Add and DGV.Columns.Insert as well as setting the DisplayIndex of the column (this works for all other columns) but I am unable to make the button column display in the correct location. I've also tried adding the button column both before and after setting the rest of the columns but this seems to make no difference Can anyone suggest what I am doing wrong? It's driving me crazy....
I stumbled on your post while looking to solve a similar problem. I finally tracked it down by doing as you mention (using the DisplayIndex property) and setting the AutoGenerateColumns property of the DataGridView to false. This property is not visible in the designer, so I just added it to the constructor for my form. Be sure to do this before setting the data source for your grid.
Hope this helps...
The AutoCreateColumn is the problem. The following article gives an example for how to fix it.
From DataDridView DisplayOrder Not Working
When setting the column’s DisplayIndex in a data grid view, some columns were out of order. To fix the issue, I had to set AutoGenerateColumns to true before setting the data source, and to FALSE after.
For Example:
dgvReservedStalls.AutoGenerateColumns = True
dgvReservedStalls.DataSource = clsReservedStalls.LoadDataTable()
dgvReservedStalls.AutoGenerateColumns = False
Same problem here, and after searching for a while, I found out that by setting the DisplayIndexes in Ascending order made it for me.
It's counter-intuitive, because it's a number, but I still had to set them in order.
This works:
With customersDataGridView
.Columns("ContactName").DisplayIndex = 0
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
.Columns("Country").DisplayIndex = 3
.Columns("CompanyName").DisplayIndex = 4
End With
While this did not:
With customersDataGridView
.Columns("ContactName").DisplayIndex = 0
.Columns("CompanyName").DisplayIndex = 4
.Columns("Country").DisplayIndex = 3
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
End With
I tried the above solutions without success. Then I found this article: It made it all better.
http://msdn.microsoft.com/en-us/library/vstudio/wkfe535h(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1