Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Adding Row Numbers to DataGridView

Adding Row Numbers to DataGridView

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2254591
    wrote on last edited by
    #1

    I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.

    int numberofrows = DG.Rows.Count;
    int len = numberofrows.ToString().Length;
    string NumberForRow;
    for (x = 0; x < DG.Rows.Count; x++)
    {
    NumberForRow = Convert.ToString(x + 1);
    while (NumberForRow.Length < len)
    {
    NumberForRow = "0" + NumberForRow;
    }
    DG.Rows[x].HeaderCell.Value = NumberForRow;
    }

    This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.

    Y U G R 4 Replies Last reply
    0
    • U User 2254591

      I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.

      int numberofrows = DG.Rows.Count;
      int len = numberofrows.ToString().Length;
      string NumberForRow;
      for (x = 0; x < DG.Rows.Count; x++)
      {
      NumberForRow = Convert.ToString(x + 1);
      while (NumberForRow.Length < len)
      {
      NumberForRow = "0" + NumberForRow;
      }
      DG.Rows[x].HeaderCell.Value = NumberForRow;
      }

      This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.

      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      Member 2256533 wrote:

      int numberofrows = DG.Rows.Count; int len = numberofrows.ToString().Length; string NumberForRow; for (x = 0; x < DG.Rows.Count; x++) { NumberForRow = Convert.ToString(x + 1); while (NumberForRow.Length < len) { NumberForRow = "0" + NumberForRow; } DG.Rows[x].HeaderCell.Value = NumberForRow;

      What length are you trying to get? this does not seem right. It looks you are trying to figure out how may digits are in a number. Is that right? int len = numberofrows.ToString().Length;

      int NumberForRow = 1;
      foreach (DataGridViewRow row in DG.Rows)
      {
      if(NumberForRow < 10)
      row.HeaderCell.Value = String.Format("{0:D2}", NumberForRow );
      else
      row.HeaderCell.Value = NumberForRow;

      NumberForRow = NumberForRow+ 1;
      }

      Yusuf May I help you?

      U L 2 Replies Last reply
      0
      • Y Yusuf

        Member 2256533 wrote:

        int numberofrows = DG.Rows.Count; int len = numberofrows.ToString().Length; string NumberForRow; for (x = 0; x < DG.Rows.Count; x++) { NumberForRow = Convert.ToString(x + 1); while (NumberForRow.Length < len) { NumberForRow = "0" + NumberForRow; } DG.Rows[x].HeaderCell.Value = NumberForRow;

        What length are you trying to get? this does not seem right. It looks you are trying to figure out how may digits are in a number. Is that right? int len = numberofrows.ToString().Length;

        int NumberForRow = 1;
        foreach (DataGridViewRow row in DG.Rows)
        {
        if(NumberForRow < 10)
        row.HeaderCell.Value = String.Format("{0:D2}", NumberForRow );
        else
        row.HeaderCell.Value = NumberForRow;

        NumberForRow = NumberForRow+ 1;
        }

        Yusuf May I help you?

        U Offline
        U Offline
        User 2254591
        wrote on last edited by
        #3

        True I am trying to find out how many digits are in the number so I can make the row headers all the same length. So if there are 1-9 rows the headers would look like 1,2,3,etc... and when there are 10-99 rows then 01,02...99, and 100-999 then 001,002,003...999 and this part works fine. The problem is that the row numbers don't show up at all in the first datagrid and they show up just fine in the second datagrid, using the exact same code.

        1 Reply Last reply
        0
        • U User 2254591

          I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.

          int numberofrows = DG.Rows.Count;
          int len = numberofrows.ToString().Length;
          string NumberForRow;
          for (x = 0; x < DG.Rows.Count; x++)
          {
          NumberForRow = Convert.ToString(x + 1);
          while (NumberForRow.Length < len)
          {
          NumberForRow = "0" + NumberForRow;
          }
          DG.Rows[x].HeaderCell.Value = NumberForRow;
          }

          This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.

          U Offline
          U Offline
          User 2254591
          wrote on last edited by
          #4

          An update to the problem. I took the code above and made a separte function out of it. Then Added the call to it after the GF.LoadDataGrid call with the same result. No numbers. Then I added a button to the form that called the same function. After the page was rendered and I clicked said button and the numbers appeared. So it seems that for some reason after the numbers are put there and the grid is rendered the row numbers disappear. Not sure why?

          1 Reply Last reply
          0
          • Y Yusuf

            Member 2256533 wrote:

            int numberofrows = DG.Rows.Count; int len = numberofrows.ToString().Length; string NumberForRow; for (x = 0; x < DG.Rows.Count; x++) { NumberForRow = Convert.ToString(x + 1); while (NumberForRow.Length < len) { NumberForRow = "0" + NumberForRow; } DG.Rows[x].HeaderCell.Value = NumberForRow;

            What length are you trying to get? this does not seem right. It looks you are trying to figure out how may digits are in a number. Is that right? int len = numberofrows.ToString().Length;

            int NumberForRow = 1;
            foreach (DataGridViewRow row in DG.Rows)
            {
            if(NumberForRow < 10)
            row.HeaderCell.Value = String.Format("{0:D2}", NumberForRow );
            else
            row.HeaderCell.Value = NumberForRow;

            NumberForRow = NumberForRow+ 1;
            }

            Yusuf May I help you?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            For positive numbers this will do:

            row.HeaderCell.Value = NumberForRow.ToString().PadLeft(requiredWidth, '0');

            :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            1 Reply Last reply
            0
            • U User 2254591

              I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.

              int numberofrows = DG.Rows.Count;
              int len = numberofrows.ToString().Length;
              string NumberForRow;
              for (x = 0; x < DG.Rows.Count; x++)
              {
              NumberForRow = Convert.ToString(x + 1);
              while (NumberForRow.Length < len)
              {
              NumberForRow = "0" + NumberForRow;
              }
              DG.Rows[x].HeaderCell.Value = NumberForRow;
              }

              This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.

              G Offline
              G Offline
              GenJerDan
              wrote on last edited by
              #6

              Something like this won't do? (The DataGRidView has an unbound column added at column 0 for the index)

              private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
              {
              foreach (DataGridViewRow R in dataGridView1.Rows)
              {
              R.Cells[0].Value = R.Index;
              }
              }

              EDIT: Oops. Never mind. Just noticed you want to put it in the row header cell....which doesn't work in the above code.

              ..and water fell from the sky like rain.

              1 Reply Last reply
              0
              • U User 2254591

                I have a function that loads data and configures the datagrid. I did this so that all the datagrids in the project would be the same. So I added a bit of code that is supposed to add row numbers to each of the datagrids.

                int numberofrows = DG.Rows.Count;
                int len = numberofrows.ToString().Length;
                string NumberForRow;
                for (x = 0; x < DG.Rows.Count; x++)
                {
                NumberForRow = Convert.ToString(x + 1);
                while (NumberForRow.Length < len)
                {
                NumberForRow = "0" + NumberForRow;
                }
                DG.Rows[x].HeaderCell.Value = NumberForRow;
                }

                This code is fired as so GF.LoadDataGrid(dgInvoices, DT, true,"InvoiceID,OrderID,Status,Date", "EVEN", ""); Where GF is GeneralFunctions class and LoadDataGrid is a method, the paramaters are dgInvoice is the DataGridView to load, DT is the DataTable, true/false is readonly, The first string is the Column headers, the second string is column spacing and the last string is columns that need to hidden if any. No the weird part. This line of code does not put in the Rowheader numbers. The DataGridView dgInvoices has an event tied to it that fills in another datagrid view with the detail of the invoice the call to fill that DataGridView is exactly the same GF.LoadDataGrid(dgInvoiceDetail,DT,true,"ItemID,Description,Qty,Amount","EVEN",""); and for that datagrid the number do appear. I know that there is no difference between the two datagrids as on the form, I have copied and pasted the first datagrid to make the second datagrid so they are the same. Not sure what is going on. Any help would be appreicated. Thank you.

                R Offline
                R Offline
                RobCroll
                wrote on last edited by
                #7

                Daniel Soper appears to have a nice solution which he describes here

                "You get that on the big jobs."

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups