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:

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
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
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,059
Programming Experience
10+
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
 

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
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:

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,059
Programming Experience
10+
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

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
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:
Top Bottom