Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
validating checkbox which are not checked in the datagridview those record should not be save in the database.


Save code as follows;

C#
for (int i = 0; i < DGv_Session.RowCount; i++)
           {
               try
               {
                   if (DGv_Session[1, i].Value != null && DGv_Session[1, i].Value != "" && DGv_Session[2, i].Value != null && DGv_Session[2, i].Value != "" && DGv_Session[3, i].Value != null && DGv_Session[3, i].Value != "" && DGv_Session[4, i].Value != null && DGv_Session[4, i].Value != "")
                   {
                       sql = "insert into Tb_Session_Structure ([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4])";

                       sql = sql + " values('" + cb_Course_Code.Text + "',
 '" + DGv_Session.Rows[i].Cells[0].Value.ToString() + "',
 " + (Convert.ToBoolean(DGv_Session[1, i].Value) == true).ToString() + ",
 " + (Convert.ToBoolean(DGv_Session[2, i].Value) == true).ToString() + ",
 " + (Convert.ToBoolean(DGv_Session[3, i].Value) == true).ToString() + ",
 " + (Convert.ToBoolean(DGv_Session[4, i].Value) == true).ToString() + ")";
                       GFun.Error = "";
                       GFun.InsertAccessData(sql);
                       if (GFun.Error.ToString() != "")
                       {
                           MessageBox.Show(GFun.Error.ToString(), "Error");
                           return;
                       }
                       GFun.OleDbCon.Close();
                   }
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.ToString(), "Error");
                   return;
               }
           }


I am validating check box which are not checked in the datagridview that value not to be saved in the database.

Days 1 2
1 checkboxunchecked checkboxunchecked
2 checkboxchecked checkboxchecked

after enters the above records into the database and click the save button.

In the database records show as follows;

Days 1 2
1 checkboxunchecked checkboxunchecked
2 checkboxchecked checkboxchecked

but i want which checkbox which which are not checked in the datagridview do not be saved in the database.

I want the final output as follows after made entries and click the save button records in the database as follows;


Final ouput as follows;
Days 1 2
2 checkboxchecked checkboxchecked
Posted
Updated 14-Mar-13 3:27am
v2
Comments
satz_770 13-Mar-13 3:01am    
In which method or where you are writing this save code?.. Do you call this save operation by a button click? or from inside the gridview?..
[no name] 13-Mar-13 3:22am    
i have one button called Save.

in the Save button i written the code
[no name] 13-Mar-13 6:15am    
please reply answer for my above question

1 solution

You should only call the insert if the checkbox is checked i.e. is True ...
DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
ch1 = (DataGridViewCheckBoxCell)DGv_Session.Rows[i].Cells[1];
                if(ch1.Value != null && bool.Parse(ch1.Value.ToString()) == true)
...

Personally I would add a little helper function
C#
private bool IsTrue(DataGridViewCell cb)
{
    DataGridViewCheckBoxCell tcb = (DataGridViewCheckBoxCell)cb;
    bool bRet = false;
    if (tcb.Value != null && bool.Parse(tcb.Value.ToString()) == true)
        bRet = true;
    return bRet;
}}

which would make your rather large if statement easier to read...
if (IsTrue(DGv_Session[1, i]) && IsTrue(DGv_Session[2, i]) && IsTrue(DGv_Session[3, i]) && IsTrue(DGv_Session[4, i]))
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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