DataColumn Expression Serial No Generating

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
Dear All,

I have datatable with a Column as "Sno".

This DataTable is bound with DataGrid

Now I want to generate Row Numbers for 'Sno' DataColumn through expression


VB.NET:
Public Function Tempinvoice(ByVal DataTableName As String) As DataTable
dt = New DataTable(DataTableName)
 
Dim dcsno As DataColumn
 
dcsno = New DataColumn("Sno", GetType(Integer))
 
dcsno.Expression =  ' Here I need your help to finish
 
dt.Columns.Add(dcsno)
dt.Columns.Add(New DataColumn("ProdID", GetType(String)))
dt.Columns.Add(New DataColumn("Price", GetType(String)))
 
Return dt
End Function

can anyone help me please?
 
Last edited:
The problem is that you have declared the array one item too long, Count is the number of items, arrays are zero-based so you have to declare it one item less than Count:
VB.NET:
Dim AutoNum(nrows - 1) As Integer

OMG, I'd forgotten completely about that.. The number to use in a VB.NET array initializer is the index of the last element, not the total number of elements.. Ugh! Thanks for the reminder John.. On the rare occasions I use arrays I probably forget this.

yousuf, I have to ask.. Why do you dim an array solely for using its length as a loop limiter?

VB.NET:
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        For i as Integer = 0 To tblTempInvoice.Rows.Count -1
                     dataGrid1.Item(i, 0) = i + 1
        Next
 End Sub
PPS, you really probably should put this in the datatable itself, not the grid that is showing the table
 
Thanks cjard,

I have tried all options those I know to generate itemNo column to autoincrement. then I come up with this. Also I tried to put this into DataTable instead of putting into form directly as below but it does not update values as I add rows to the datatable.

If you can figureout the problem, then definitely I would use it in DataTable.:p


Here is the failed code:-

VB.NET:
Public Function Tempinvoice(ByVal DataTableName As String) As DataTable

         dt = New DataTable(DataTableName)
         

        Dim dcSno As DataColumn
        dcSno = New DataColumn("Sno", GetType(Integer))

        Dim nrows As Integer = dt.Rows.Count
        Dim AutoNum(nrows - 1) As Integer
        Dim i As Integer

        For i = 0 To UBound(AutoNum)
            'Dim dr As DataRow = dt.NewRow
            dcSno.Expression = "i + 1"


        Next
        dt.Columns.Add(dcSno)

        dt.Columns.Add(New DataColumn("ProdID", GetType(String)))
        Return dt

    End Function

Thanks in advance
 
Merged threads, it doesn't look as the array was the problem after all, cjard probably have a clever solution for this mess ;)
 
To be perfectly honest, we dont do this in the client at all. At the time of insertion into the database, the next serial number is worked out and when the insert has finished the new number is sent back to the client.

Trying to do this in the client is usually a dumb idea for multi user systems, because clients arent centrally intelligent enough to hand out unique numbers
 
Thanks for your reply cjard,

This autonumber in my app. won't be sent to the server(or database) this datagrid and its autonumber is used just for display purpose only. once displayed and printed this tempdatatable will be disposed.

This ItemNo column is just to identify to the user how many items he had entered or to count items(perhaps if there is a problem with an item customer will notify them with itemNo, not by productID bcz on same print there may be repeated item).

Thanks for your advice
 
I begin to understand. I read an article on ASP.NET line numbering in a grid and it had a cool solution.. Define a custom column for your datagrid (I hope this is possible) that gets its value from the Container.ItemIndex (the container of the column being the datagrid) rather than from a data source

I dont have any good tutorials on customising your own datagrid column, but you might be able to find something in the DW1 link, or have a google. If you dont succeed, let me know and I'll see what I can turn up
 
Thanks cjard,

I have googled for it long before posting into the forum. But all I have found was related with Database (sql server , oracle....) and answered with identity column of sql server, sequence of oracle or Autonumber field of Access DB. not with my problem of TempDataTable.

This code below works perfect, but with a click. What I want is,We should not have this autonumbering with user's interaction(rather system should do it automatically).

VB.NET:
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim nrows As Integer = tblTempInvoice.Rows.Count

Dim AutoNum(nrows - 1) As Integer

Dim i As Integer


For i = 0 To UBound(AutoNum)

Dim dr As DataRow = tblTempInvoice.NewRow

dataGrid1.Item(i, 0) = i + 1

Next
End Sub

Thanks for sharing your knowledge.

Is there any solution???
 
Last edited:
I think you missed what I was inferring.

Read this:
http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/#S7

About creating a comboboxcolumn. DONT DO THAT. Just know that it is possible to make your own column styles.
Create a column called DataGridRowNumberColumn. It is not editable, it does not data bind (but you will need a blank column for it in your datatable), it does nothing other than show the row number in each cell.


Here is the crucial thing:

VB.NET:
Public Class DataGridRowNumberColumn
  Inherits System.Windows.Forms.DataGridTextBoxColumn

  Protected Overrides Function GetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object
    [B]Return rowNum.ToString()[/B]
  End Function
End Class

Its a custom column, that DOESNT go and get the data from the underlying source.. it just returns the row number it is given!

Here is how I added it. I amde a DataSet called DataSet1. It has a DataTable called DataTable1. The table has 2 columns, Column1 and Column2. Column2 is needed even though it will always be blank, because I didnt find any significant info on the net as to how to add a non-databound-column into a databound grid. If the grid is operating in bound mode, it is easier to just add a blank column to the data source and never use it, given that the custom column will return a value anyway:

VB.NET:
    Dim myGridStyle As DataGridTableStyle = _
                    New DataGridTableStyle()
    myGridStyle.MappingName = "DataTable1"

    Dim nameColumnStyle As DataGridTextBoxColumn = _
        New DataGridTextBoxColumn()
    nameColumnStyle.MappingName = "Column1"
    nameColumnStyle.HeaderText = "Column1"
    myGridStyle.GridColumnStyles.Add(nameColumnStyle)

    Dim rownColumnStyle As [B]DataGridRowNumberColumn [/B]= _
      New DataGridRowNumberColumn()
    rownColumnStyle.MappingName = "[B]Column2[/B]"
    rownColumnStyle.HeaderText = "RowN"
    rownColumnStyle.ReadOnly = True
    myGridStyle.GridColumnStyles.Add(rownColumnStyle)


    myDataGrid.TableStyles.Add(myGridStyle)

Here is a video of it working:
 

Attachments

  • cjard_does_datagridrownumber.zip
    40.2 KB · Views: 22
Your code is so big in its clevernessss....

Thanks a lot cjard,

The code is so small, But So big in its clevernesssssss...

I wanted to give u some roses instead of this thumpsup. But unfortunately our forum does not have Roses ( emoticons)

Special thanks for your explaination and video demo.

my Suggestion to Arg81. Please try this code instead of yours.
 
Last edited:
Back
Top