views:

172

answers:

1

I want to get some trick for this problem.

I have my table like this

Product (uuid, Name)

and one datagridview to display this information ( dataGridView.DataSouce = Products which is assign on runtime)

  • My problem is that I want to have "No" column on dataGridView which will show like below

    No | Name

    1 | ProdctA

    2 | ProductB

    3 | ProductC

What I do right now is create a "No" field on my product Model, and loop through all the row and assign value to it.

I think this is not a good solution.

Hope anyone can suggest better solution.

Thanks,

A: 

I have never found a trick, but I have researched several times. Following are some comments regarding this issue.

  1. Since you are adding a column, you are basically adding data. Therefore, you must specify what the row's data will be for that column.
  2. Updating a specific column is relatively fast. Approximately the same performance as adding a data column with an expression. I have done performance testing and updating an integer value while using the column's index is about as fast as you can do this update.

    Dim oData As DataTable = GatherDataTable()

    Dim oAutoIdColumn As New DataColumn("uuid", GetType(Integer))
    oData.Columns.Add(oAutoIdColumn)

    Dim iColumnIndex As Integer = oAutoIdColumn.Ordinal
    Dim oRows As DataRowCollection = oData.Rows
    Dim iRowCount As Integer = oRows.Count

    For i As Integer = 1 To iRowCount
        oRows.Item(i - 1).Item(iColumnIndex) = i
        '-or-
        oRows(i - 1)(iColumnIndex) = i
    Next

    Me.DataGridView1.DataSource = oData
AMissico