First of all, please, read carefully OriginalGriff's solution.
Second of all, your query is for one student at time:
"SELECT * FROM student WHERE s_id = '" + stID + "'";
BTW: such of query is
sql injection[
^] vulnerable!
You have to create a query which accepts several id's to be able to display selected cards. See:
IN (Transact-SQL) - SQL Server | Microsoft Docs[
^]
StringBuilder sb = new StringBuilder();
foreach (DataGridViewRow item in dgPrintIDCard.Rows)
{
if (item.Cells[0].Value != null)
{
var stID = item.Cells[1].Value.ToString();
sb.Append($"{stID},");
}
}
string query = $"SELECT * FROM student WHERE s_id IN ({sb.ToString().TrimEnd(',')})";
con = new SqlConnection(cs.DBcon);
con.Open();
cmd = new SqlCommand(query, con);
printPreviewGlobalVar.mReport = "prntStudentCard";
printPreviewGlobalVar.mSqlstr = query;
prntPreview prnt = new prntPreview();
prnt.Show();
this.Close();
Note:
I'm using StringBuilder class, because string object is immutable! See:
Strings - C# Programming Guide | Microsoft Docs[
^]
Links:
StringBuilder Class (System.Text) | Microsoft Docs[
^]
String.TrimEnd Method (System) | Microsoft Docs[
^]