0

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

enter image description here

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());
}
6
  • Not clear what are you asking, if you have 10 rows you need to get data related to that 10 rows only? Commented May 15, 2020 at 12:18
  • what I'm trying to say is , if 10 rows found from database I want to display them with ID 1,2,3,4,5,6,7,8,9,10 in datagirdview instead of their primary key ID from database Commented May 15, 2020 at 12:23
  • i want to change row[0].ToString() // ID into 1, 2,3, 4,5 Commented May 15, 2020 at 12:28
  • Still not clear what should be the output, so from your grid you need to get all the row[0] to comma separated string Commented May 15, 2020 at 12:41
  • i edited the question again , is it better to understand now ? Commented May 15, 2020 at 12:48

1 Answer 1

1

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());
}
Sign up to request clarification or add additional context in comments.

1 Comment

That is what I told :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.