简体   繁体   中英

Counting rows on a datagrid and adding it as a new column

I want to basically go this is row 1, row 2, 3, 4...until the last row and add that as a new column on my datagrid table, I am not trying to find the total amount of rows.

Here is my current code (I think this is terribly wrong but this is a lot of new code):

for (int i = 0; i < table.Rows.Count; i++)
        {

            string rowNum = table.Rows[i].ToString();
            table.Columns.Add("Book num", typeof(string), rowNum);

        }

我不想用BookingID来代替它,而要用预订号替换它将使用行号。

The following code will add 1 new column to your table, and then populate each row in that column with the index number of that row.

If you don't want to start with 0 , just change int i = 0; to int i = 1;

table.Columns.Add("Book num", typeof(string));
int i = 0;
foreach (DataRow dr in table.Rows)
{
  dr["Book num"] = i;
  i++;
}

Try for total rows,

     int TotalRows = 0;
     for (int i = 0; i < table.Rows.Count; i++)
        {
            if(i == 0)
            {
              TotalRows = 1;
            }
             else
             {
               TotalRows + = i;  
             }        
            string rowNum = table.Rows[i].ToString();
            table.Columns.Add("Book num", typeof(string), rowNum);
           }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM