Click here to Skip to main content
15,891,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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
Updated 15-Mar-13 18:25pm
v3

1 solution

Yes you did little mistake. Inside your loop
C#
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
C#
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 ++;
 }
 
Share this answer
 
Comments
[no name] 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.
[no name] 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900