Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Note it is windows application.

Run mode screen as follows;

Course Code Combo box

Datagridview as follows;

Days sess1 sess 2 sess3 sess4
1 checkbox checkbox checkbox checkbox

In Database record as follows;

courseCode Days sess1 sess2 sess3 sess4
REO 1 checkboxchecked checkboxchecked checkboxchecked checkboxchecked
REO 2 checkboxchecked checkboxchecked checkboxchecked checkboxchecked

 
Then i have one button in the run mode called Search Button when click the REO Course in the combo box, in the datagridview that particular course details to be displayed in the datagridview.
 
int irows;
try
{
    sql = "select [Days],[Session1],[Session2],[Session3],[Session4] from Tb_Session_Structure where Cmn_Minor_code = '" + cb_Course_Code.Text.ToString() + "' order by [Days]";
    dr = GFun.ReadAcessSql1(sql);
    while (dr.Read())
    {
        for (irows = 0; irows < DGv_Session.RowCount; irows++)
        {
            DGv_Session.Rows[irows].Cells[1].Value = dr[1].ToString().Trim();
            DGv_Session.Rows[irows].Cells[2].Value = dr[2].ToString().Trim();
            DGv_Session.Rows[irows].Cells[3].Value = dr[3].ToString().Trim();
            DGv_Session.Rows[irows].Cells[4].Value = dr[4].ToString().Trim();
        }
    }
    dr.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString(), "Error");
    return;
}
when i click the search button, the output as follows in datagridview
 
courseCode Days sess1 sess2 sess3 sess4
REO 1 checkboxchecked checkboxchecked checkboxchecked checkboxchecked

 
but in the database two records are there, for the REO Course in Database days 1 and 2 record is there. but when i search for the REO Course the Days 1 record only shows.
 
why what is the problem?
 
from my search code what is the mistake? i tried but it is not working.
 
Regards,
Narasiman P.
Posted 15-Mar-13 18:20pm
Edited 15-Mar-13 18:25pm
SoMad125.4K
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Yes you did little mistake. Inside your loop
for (irows = 0; irows < DGv_Session.RowCount; irows++)
{
   DGv_Session.Rows[irows]
}
irows variable did not represent actual row index which came from datareader. Your could should be
 int currentRowIndex =0;
 while (dr.Read())
 {       
        //for (irows = 0; irows < DGv_Session.RowCount; irows++)
        //{
            DGv_Session.Rows[currentRowIndex].Cells[1].Value = dr[1].ToString().Trim();
            DGv_Session.Rows[currentRowIndex].Cells[2].Value = dr[2].ToString().Trim();
            DGv_Session.Rows[currentRowIndex].Cells[3].Value = dr[3].ToString().Trim();
            DGv_Session.Rows[currentRowIndex].Cells[4].Value = dr[4].ToString().Trim();
        //}
          currentRowIndex ++;
  }
  Permalink  
Comments
Member 8054539 at 16-Mar-13 8:36am
   
why you comment the for loop line in the while loop
 
And put the for loop first.
My below code is correct,
 
int currentRowIndex =0;
while (dr.Read())
{
for (currentRowIndex = 0; currentRowIndex < DGv_Session.RowCount; currentRowIndex ++)
//{
DGv_Session.Rows[currentRowIndex].Cells[1].Value = dr[1].ToString().Trim();
DGv_Session.Rows[currentRowIndex].Cells[2].Value = dr[2].ToString().Trim();
DGv_Session.Rows[currentRowIndex].Cells[3].Value = dr[3].ToString().Trim();
DGv_Session.Rows[currentRowIndex].Cells[4].Value = dr[4].ToString().Trim();
//}
currentRowIndex ++;
}
 
whether my above code is correct?
 
Please help me.
 
Regards,
 
Narasiman P.
Member 8054539 at 16-Mar-13 8:37am
   
My below code is correct,
 
int currentRowIndex =0;
while (dr.Read())
{
for (currentRowIndex = 0; currentRowIndex < DGv_Session.RowCount; currentRowIndex ++)
{
DGv_Session.Rows[currentRowIndex].Cells[1].Value = dr[1].ToString().Trim();
DGv_Session.Rows[currentRowIndex].Cells[2].Value = dr[2].ToString().Trim();
DGv_Session.Rows[currentRowIndex].Cells[3].Value = dr[3].ToString().Trim();
DGv_Session.Rows[currentRowIndex].Cells[4].Value = dr[4].ToString().Trim();
}
currentRowIndex ++;
}
 
whether my above code is correct?
 
Please help me.
 
Regards,
 
Narasiman P.
S. M. Ahasan Habib at 16-Mar-13 8:50am
   
DGv_session object structure create little confusion to me. Instead of assign value to existing session grid view DGv_session. I suggest you create a new DataTable inside the loop and store that session if you need letter on and assign that to any gridview for display purpose.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 682
1 ChauhanAjay 135
2 Maciej Los 130
3 ChintanShukla 90
4 ashok rathod 90
0 Sergey Alexandrovich Kryukov 10,200
1 OriginalGriff 6,987
2 CPallini 5,850
3 George Jonsson 4,015
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 16 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100