0

I'm new to SQL so this might be a basic level problem.

I had a datagridview on C# with 100 rows which has 3 columns : ID from 1 to 100, name of student and mark of Math. I had sort them descending by mark of Math and now I want to divide into 5 parts which is the first 20% of student sorted then I set the Reward column 5 candies, the next 20% of student 4 candies, so on next is 3, 2 and 1 candies.

Here's my code for the 5 candies for the first 20% student because I had sort all the mark descending:

    for (int i = 0; i < DataGridView.Rows.Count * 0.2; i++)
    {
       string sql;
       sql = "UPDATE StudentManagement SET Reward= 5 WHERE Name= N'" + DataGridView.Rows[i].Cells[1].Value + "'";
       Functions.RunSql(sql);
    }

When I run the code I expected all the first 20 rows set to 5 candies in Reward column but it just updated value 5 for only the 19th row. All the first 18 rows and the 20th rows still had NULL value in Reward columns. I think the problem came from the if condition:

i < DataGridView.Rows.Count * 0.2

How can I solve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • i < DataGridView.Rows.Count * 0.2 is not an 'if' statement. Do you know how to use the debugger? If not you should learn it.Nothing obvious in your code snippet. – Jon Vote Dec 07 '18 at 22:19
  • are you sure that you have 100 rows in the result ? please show us the value of `DataGridView.Rows.Count` – GMB Dec 07 '18 at 22:20
  • You might want to look into using SQL Server's `NTILE` function (assuming you are using that) to generate quintiles in the database directly instead of using your data grid as a mechanism for doing so https://stackoverflow.com/questions/24924591/how-can-i-group-student-scores-into-quintile-using-sql-server-2008 – Paul Abbott Dec 07 '18 at 22:25
  • @GMB yes I'm sure I have 100 rows in the result. I run DataGridView.Rows.Count and the message box show 100. – Vũ Văn Hiệp Dec 07 '18 at 22:30
  • @JonVote thank you for your help. – Vũ Văn Hiệp Dec 07 '18 at 22:33
  • @PaulAbbott thank you for your help, I'll learn them now. – Vũ Văn Hiệp Dec 07 '18 at 22:34
  • Hope it was useful :) are you sure Cells[1].Value is what you think it is? The code looks like it should work - but you will know if you use the debugger. – Jon Vote Dec 07 '18 at 22:45
  • @JonVote thank you but the second column in my datagridview is student name so I think it's has nothing wrong in Cells[1].Value. – Vũ Văn Hiệp Dec 07 '18 at 22:58
  • This will not solve your issue, but... you should search by `ID` instead of searching by `Name`; I understand that ID is the unique identifier of the student – GMB Dec 07 '18 at 23:24

0 Answers0