Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am generating the gridview at runtime through select query but now i am checking one condition and as per that condition i want delete that perticular row of gridview if it fullfills the condition in window application then how i can delete row.i am giving my code below

C#
 private void LoadgvFeesData()
        {
            DBAccess objDBAccessCF = new DBAccess();
            long _SlabID=getSlabID();
            float totalfees = 0;
            try
            {
                objDBAccessCF.OpenConnection();
                string SelectCommand = "SELECT SM.ID As                SlabFeeTypeMappingID , FT.TypeName, SM.Amount, SM.Amount As PayAmount, SM.FeeSlabsID, SM.Frequency,                                     SM.FeeTypeID, FS.Name FROM tbl_Fee_Slabs FS INNER JOIN 
tbl_SlabFeeTypeMapping SM ON FS.ID = SM.FeeSlabsID INNER JOIN 
tbl_Fee_Type FT ON SM.FeeTypeID = FT.ID where FS.ID=@1"; 
                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("1", _SlabID);
                DataTable dt = new DataTable();
dt.Load((IDataReader)objDBAccessCF.ExecuteForReader(SelectCommand, param));
                gvFees.DataSource = dt;
                if (dt.Rows.Count> 0)
                {
           label14.Text = dt.Rows[0]["SlabFeeTypeMappingID"].ToString();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[0]["Frequency"].ToString() == "One Time")
                        {
                            string Selectcmd = "Select StudentID from tbl_Fee_Receipt where SlabFeeMappingId=@1 and StudentID=@2 ";
SqlParameter[] para = new SqlParameter[2];
para[0] = new SqlParameter("1", label14.Text);
para[1] = new SqlParameter("2", cmbRollNO.SelectedValue);
DataTable dt1 = new DataTable();
dt1.Load((IDataReader)objDBAccessCF.ExecuteForReader(Selectcmd, para));
                            if (dt.Rows.Count == 1)
                            {
                            }
                            else
                            {
                                //Here i want to delete particular gridview row if condition fullfills
                            }
                        }
                   }
              }
        }
    }
Posted
Updated 19-Apr-10 3:03am
v2

1 solution

After inspecting you're code there are some things that do not make sense to me at all. For example:

C#
for (int i = 0; i < dt.Rows.Count; i++)
{
   if (dt.Rows[0]["Frequency"].ToString() == "One Time")
   {
      ...
   }
}


Why are you making a for loop and then checking dt.Rows[0] all the time instead of dt.Rows[i]?

Then another thing:

C#
DataTable dt1 = new DataTable();

dt1.Load((IDataReader)objDBAccessCF.ExecuteForReader(Selectcmd, para));

if (dt.Rows.Count == 1)
{
}
else
{
   //Here i want to delete particular gridview row if condition fullfills
}


Why do you make a second datatable called dt1 and then do another check (dt.Rows.Count == 1) instead of (dt1.Rows.Count == 1)?

Right those things i was just wondering about. Seems to me like you should insert this code inside the else statement:

C#
if (dt1.Rows.Count == 1)
{
}
else
{
   dt.Rows.RemoveAt(i);
}
 
Share this answer
 
v2

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