Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Datagridview row order numbering

Posted on 2012-12-22
15
Medium Priority
?
443 Views
Last Modified: 2013-01-12
Software: VB-Net & SQL Server 2005

I have a databound grid with a special column that maintains row numerical order. When inserting text into a new blank grid the column numbers start with 0 - how can I change the code so the column starts with 1?

I don't know if this can be answered without the code....

Thanks for your help!
0
Comment
Question by:dave_sky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38716960
is this column has auto incremental id?
do you set the row numerical order value by yourself?
0
 

Author Comment

by:dave_sky
ID: 38717188
sedgwick, thanks for your response:

1. Yes, the column has auto incremental id.

2. The row numerical order is set by code.

The column is auto numbered when the grid is first opened:
0
1
2
3
4

Should be:
1
2
3
4
5

When a blank grid is opened, there is no number in the column. When a row is added by user,  the first column number is 0.
 When I insert/delete a row the number changes to 1. This change shifts the order of the rows one time. After that the column numbering works fine.
If the column number was 1 when first row was added. Everything would work great.
0
 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 38717819
simply add 1 to the row number order from the code
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38717982
can u post the code of row adding?
you can use dataGridView_RowsAdded event handler to modify the numerical value of the column once new row is added.
0
 

Author Comment

by:dave_sky
ID: 38718947
I tried this code with no luck:
Private Sub TblBasicUpgradesDetailDataGridView_RowsAdded(ByVal sender As Object, _
            ByVal e As DataGridViewRowsAddedEventArgs) _
            Handles TblBasicUpgradesDetailDataGridView.RowsAdded

        Dim messageBoxVB As New System.Text.StringBuilder()
        messageBoxVB.AppendFormat("{0} = {1}", "sysAppOrder", e.RowIndex)
        messageBoxVB.AppendLine()

    End Sub

Open in new window

Other related code:
Private Sub TblBasicUpgradesDetailDataGridView_UserAddedRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles TblBasicUpgradesDetailDataGridView.UserAddedRow
        ResetOrdering(e.Row.Index - 1)

    End Sub


    Private Sub ResetOrdering(ByVal rowIndex As Integer, Optional ByVal forceReorder As Boolean = False)
        'Set the sort order column.  Once again, not relying on column order but use name
        '
        Dim rowCount As Integer
        Dim rowLocation As Integer
        rowCount = TblBasicUpgradesDetailDataGridView.RowCount - 1
        rowLocation = rowIndex
        If (rowCount - 1 > rowLocation) Then
            Dim BasicUpgradesKey As String = BasicUpgradesKeyTextBox.Text
            Dim idx As Integer
            '
            ' Create a view with the proper order
            '
            Dim dataView As New DataView(MpSQLDataSet.Tables("TblBasicUpgradesDetail"), "", "", DataViewRowState.CurrentRows)
            dataView.RowFilter = " BasicUpgradesKey = " + BasicUpgradesKey
            dataView.Sort = "sysAppOrder"
            '
            ' if we work off the view, it will constantly re-sort.  So copy the data into a new table
            '
            Dim tempTable As DataTable
            tempTable = MpSQLDataSet.Tables("TblBasicUpgradesDetail").Clone()
            For idx = 0 To dataView.Count - 1
                Console.WriteLine(String.Format("{0} {1} {2}", dataView(idx)("sysAppOrder").ToString(), _
                                                 dataView(idx)("Description"), idx + 1))
                tempTable.ImportRow(dataView(idx).Row)
            Next
            '
            ' Loop through the new table and sort it correctly
            '
            idx = 1
            For Each row As DataRow In tempTable.Rows
                row("sysAppOrder") = idx
                idx += 1
            Next
            '
            ' Update the primary table
            '
            dataView.Sort = String.Empty
            For Each row As DataRow In tempTable.Rows
                dataView.RowFilter = String.Format("BasicUpgradesDetailKey = {0}", row("BasicUpgradesDetailKey"))
                dataView(0)("sysAppOrder") = row("sysAppOrder")
            Next
        Else
            Dim cell As DataGridViewCell
            cell = TblBasicUpgradesDetailDataGridView("DataGridViewTextBoxColumn15", rowIndex) '"sysAppOrder"
            cell.Value = rowLocation
        End If
    End Sub

Open in new window

0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38719222
why not changing the table schema to have another field set as auto increment which starts with 1?
the grid columns are automatically generated in the control to match the schema information provided by the data source.
in case you already have one you may use it or add new field as suggested.
0
 

Author Comment

by:dave_sky
ID: 38719243
>why not changing the table schema to have another field set as auto increment which starts with 1?

Can you give me an example of this... I am only a copy and paste developer
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38719260
U need to change the table schema in the database.
Get in the sql management that u use to create the tables and add the field to the desired table.
You need to rebind the datagrid with the table.
0
 

Author Comment

by:dave_sky
ID: 38719269
I'm afraid that will cause many other problems. I have a lot of code that is using the existing column as an indexing column
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38719290
so add new field for backward compatibility no sweat
its easier faster and no code mess
0
 

Author Comment

by:dave_sky
ID: 38719299
I don't understand how to do your last comment so I am going to try a sql query to change the numbering from 0 to 1
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38719304
assume u have that now ids are from 1 not 0
once u commit after adding row for instance you will overwtite your ordering in your db table
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38719307
i can post sql query which add field like that if u want
0
 

Author Comment

by:dave_sky
ID: 38719387
Thanks, I would appreciate you posting the sql query
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 38719396
alter table tablename add columnname int identity(1,1)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question