How to create an auto-incremental column in a Devexpress Xtragrid

Very often you would like a column in your Xtragrid to be entered a new value automatically whenever a new row is added to the grid. This column is usually the first column in your grid.  


This can be done by using the CustomColumnDisplayText event in your grid view.

Open your form design. Click your gridview and press F4 to open its properties Window. Choose the Events tab.


Locate the CustomColumnDisplayText event and double click it. In the handling event codes make it look like the following: 

CSharp code

private void yourGridView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) {

 if (e.Column == yourColName) { // without " before and after yourColName
   if (e.RowHandle >= 0) {
     e.DisplayText = (e.RowHandle + 1).ToString();
  }
 } 
}

VB Code

Private Sub yourGridView_CustomColumnDisplayText(ByVal sender As System.Object, ByVal e As  
 DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs) Handles gvNhanVien.CustomColumnDisplayText
    If (e.Column.FieldName = "yourColName") Then
        If e.RowHandle >= 0 Then
             e.DisplayText = (e.RowHandle + 1).ToString() 
         End If
    End If 
End Sub


1 comment:

Gochoa said...

Muchas Gracias!!!
Gloria Ochoa
Colombia

Post a Comment

Blog Archive

About Me

My photo
I am a software developer with roughly 5 years of experience in developing end-to-end solutions in C#, Java, C/C++, PHP and HTML/CSS/Javascript. At the moment, I am joining the Professional Doctorate in Engineering degree program in Software Technology at Eindhoven University of Technology. My areas of particular interest include software design, data structures and algorithms, problem solving, software security, embedded system, machine learning, and data science.