简体   繁体   中英

C# Count Rows from MySQL and display to datagridview

I'm using DataGridView to display some data from MySQL.

SELECT * FROM user where 'roles' = @roles

It is easy to display all data from database, but how can I give row count based on the rows I found?

Example: found 3 user from database.

how can I give a row count ID (1,2,3) for each rows

在此处输入图像描述

some code

private void add(int id,String name)
{
  dataGridViewTable.Rows.Add(id,name);
}

foreach (DataRow row in table.Rows)
{
  int id = 0;
  add(id + 1, row[1].ToString());
}

This is actually a debugging issue. Please learn to use the debugger as it might come in handy in the future.

The problem is the variable "id" is being initialized and set to zero with each loop. Pull the outside the loop and increment as needed.

This code should solve your immediate problem.

int id = 0;
foreach (DataRow row in table.Rows)
{
  add(id++, row[1].ToString());
}

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