enumerator in gridview

Posted on 2007-07-27
Last Modified: 2013-11-26
In VS2005 winforms C# I have a datagridview binding to a dataset.
I want to see in the gridview a new column that will be an enumerator that will show the number of the row
starting from 1. I want that if I delete a row this enumerator will update. How can I implement this?
Question by:ANAT2403
    LVL 29

    Expert Comment

    by:Gautham Janardhan
    check this out

    DataTable FTable =
                            new DataTable();
                      FTable.Columns["COL3"].Expression = "COUNT(COL1)";

                      DataRow FRow = FTable.NewRow();
                      FRow["COL1"] = "1";
                      FRow["COL2"] = "1";

                      object obj = FTable.Rows[0]["COL3"];

    col3 would give u the count
    LVL 29

    Expert Comment

    by:Gautham Janardhan
    u can bind a similar column to the grid voew so that it ill always show the count

    Author Comment

    Sorry I don't get you and I asked for an example with a gridview.

    Accepted Solution

    I solved this question in a method that I added to a user control on paging datagridviw.
    this is the user control
    and this is the code I added
     /// <summary>
            /// Sets and displays a DataTable
            /// this also add a new column with the row number
            /// </summary>
            /// <param name="dtin">The DataTable to display.</param>
            public void SetDataTablewithRowNumber(DataTable dtin)
                int indrow,indcol;
                int maxr = dtin.Rows.Count;
                DataTable tempdt = new DataTable();
                System.Data.DataRow drnew;
                int numcol = dtin.Columns.Count;
                tempdt.Columns.Add("Row", Type.GetType("System.Int32"));
                for (indcol = 0; indcol < numcol; indcol++)
                    tempdt.Columns.Add(dtin.Columns[indcol].ColumnName, dtin.Columns[indcol].DataType);

                for (indrow = 0; indrow < maxr; indrow++)
                    drnew = tempdt.NewRow();
                    drnew["Row"] = indrow+1;
                    for (indcol = 0; indcol < numcol; indcol++)
                        drnew[dtin.Columns[indcol].ColumnName] = dtin.Rows[indrow][dtin.Columns[indcol].ColumnName];
    LVL 29

    Expert Comment

    by:Gautham Janardhan
    if u bind the column 'COL3' to a column in a grid view that column will always show the count

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now