?
Solved

enumerator in gridview

Posted on 2007-07-27
7
Medium Priority
?
525 Views
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?
Thankyou
Anat
0
Comment
Question by:ANAT2403
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 19583828
check this out

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

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

                  object obj = FTable.Rows[0]["COL3"];
                  Console.WriteLine(obj.ToString());

col3 would give u the count
0
 
LVL 29

Expert Comment

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

Author Comment

by:ANAT2403
ID: 19585045
Sorry I don't get you and I asked for an example with a gridview.
Anat
0
 

Accepted Solution

by:
ANAT2403 earned 0 total points
ID: 19585908
I solved this question in a method that I added to a user control on paging datagridviw.
this is the user control
http://www.codeproject.com/useritems/Paging_DataGridView.asp
and this is the code I added
Anat
 /// <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];
                }
                tempdt.Rows.Add(drnew);
            }
 
            dsTemp.Tables.Add(tempdt.Copy());
            GoToPageNumber(1);
        }
0
 
LVL 29

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 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