简体   繁体   English

C# 从 MySQL 计数行并显示到 datagridview

[英]C# Count Rows from MySQL and display to datagridview

I'm using DataGridView to display some data from MySQL.我正在使用 DataGridView 显示来自 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.示例:从数据库中找到3 个用户

how can I give a row count ID (1,2,3) for each rows如何为每行提供行计数ID (1,2,3)

在此处输入图像描述

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.问题是变量“id”正在被初始化并在每个循环中设置为零。 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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