Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing an application where teacher are allowed to enter marks for students, based on selecting a subject they teach. Now I am using a 4 columed datagrid to which I am populating all the student rollnum(1st col) and name (2nd col) and textbox control to enter Test marks(3rd col) and textbox control to enter quiz marks(4th col). I have succeded in populating data to datagrid through datatable. Now my problem is, when teacher enters all the marks and click on submit button, I want my application to check if any of the textboxes are left empty. Unless and untill all the textboxes are filled, my INSERT query(Adding marks to database query) should not execute. This is what I have tried so far

HTML
int rowcount = DTUSNName.Rows.Count;//rows count of datatable
            for (int i = 0; i < rowcount; i++)
            { 
                if (dataGridView1.Rows[i].Cells[0].Value != null)
                {
                    if (dataGridView1.Rows[i].Cells[1].Value != null)
                    {
                        var confirmResult = MessageBox.Show("Once the marks are added you wont be allowed to make changes. Do you want to proceed?","Confirm!!",MessageBoxButtons.YesNo);
                        if (confirmResult == DialogResult.Yes)
                        {
                            SqlCommand Test1query = new SqlCommand("INSERT INTO TblStudentReg (Test1, Quiz1) VALUES (@fld1,@fld2) WHERE Code=@code AND Sem = @sem AND CC=@cc AND AcademicYr = @aca", con);
                            Test1query.Parameters.Add(new SqlParameter("@fld1", dataGridView1.Rows[i].Cells[0].Value.ToString()));
                            Test1query.Parameters.Add(new SqlParameter("@fld2", dataGridView1.Rows[i].Cells[1].Value.ToString()));
                            Test1query.Parameters.Add(new SqlParameter("@code", temp));//rollnum
                            Test1query.Parameters.Add(new SqlParameter("@sem", StaffMEDrp1.SelectedItem));subject code
                            Test1query.Parameters.Add(new SqlParameter("@cc", label3.Text));//subject code
                            Test1query.Parameters.Add(new SqlParameter("@aca", StaffMETxt1.Text));//academic yr
                            con.Open();
                            Test1query.ExecuteNonQuery();
                            con.Close();
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                         MessageBox.Show("You have not set Quiz 1 Marks for USN : '" + usn + "'");
                    }
                }
                else
                {
                    MessageBox.Show("You have not set Test 1 Marks for USN : '" + usn + "'");
                }
            }

Thank you.
Posted
Updated 8-Jun-15 2:08am
v2

1 solution

Try this...Hope it help

C#
private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    string headerText = 
        dataGridView1.Columns[e.ColumnIndex].HeaderText;

    // Abort validation if cell is not in the your's(Whatever column name in datagrid) column. 
    if (!headerText.Equals("ColumnName")) return;

    // Confirm that the cell is not empty. 
    if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
    {
        dataGridView1.Rows[e.RowIndex].ErrorText =
            "Column Name must not be empty";
        e.Cancel = true;
    }
}


Happy Coding...........
 
Share this answer
 
v2
Comments
partha143 19-Jun-15 11:44am    
Thanks Nav :)
partha143 19-Jun-15 11:46am    
Nav, I am facing another problem with Crystal Report. I request you to have a look at it. Below is the link :
http://www.codeproject.com/Questions/1001924/Inserting-DataGrid-to-a-particular-section-in-crys

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