Click here to Skip to main content
15,877,857 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am having a button on a form which adds data in a textbox to datagridview and it works.
now i want to check if the year in the textbox is allready in the datagridview then it should display a message or if not it should insert the data in the textbox to the gridview.
i tried this but didnt work. please help me out

C#
private void btnAdd_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(this.cboYear2.Text))
            {
                MessageBox.Show("Please select a year");

                return;
            }

            for (int k = 0; k < dgvAddrecords.Rows.Count -1; k++)
            if (this.cboYear2.Text == this.dgvAddrecords.Rows[k].Cells[7].Value)
            {
                MessageBox.Show("The Year exsist, Please select a new year");
                return;
            }

            else
            {
                dgvAddrecords.Rows.Add(txtCode2.Text, dtpDate.Text, txtName2.Text, txtOwnersnane.Text, cboRatetype.Text, txtDistrict.Text, txtTown.Text, cboYear2.Text, txtYearlydues.Text);


                int sum = 0;
                for (int i = 0; i < dgvAddrecords.Rows.Count; ++i)
                {
                    sum += Convert.ToInt32(dgvAddrecords.Rows[i].Cells[8].Value);
                }

                txtAmountdue.Text = sum.ToString();
            }

        }
Posted

change if :
C#
for (int k = 0; k < dgvAddrecords.Rows.Count ; k++)
            if( this.dgvAddrecords.Rows[k].Cells[7].Value!=null)
            if (this.cboYear2.Text == this.dgvAddrecords.Rows[k].Cells[7].Value.ToString() )
            {
                MessageBox.Show("The Year exists, Please select a new year");
                return;
            }
 
Share this answer
 
v2
Comments
mikeoabban 4-May-13 16:06pm    
the problem is the code runs up to this point and jumps to the end of the code
k < dgvAddrecords.Rows.Count - 1;
what should i do
Member 10194921 12-Aug-13 7:19am    
i hav 3 textboxes for journal entry 1 wil store id other wil stor narration and the 3rd one store amount.....i just want to store these items in datagriview after enter the text box repeatdlyy
Mojtaba Eng 4-May-13 22:04pm    
change if condtion to :
k < dgvAddrecords.Rows.Count

your row checking is not check last row
Member 10194921 12-Aug-13 7:20am    
my idea is to post more than one narration pls help
You could try using DataTable as a DataSource for your DataGridView. You just add row to DataTable, and then refresh DataGridView.
 
Share this answer
 
change your check partial code to this :
C#
for (int k = 0; k < dgvAddrecords.Rows.Count -1; k++)
            {
            if(this.dgvAddrecords.Rows[k].Cells[7].Value!=null)
            if (this.cboYear2.Text == this.dgvAddrecords.Rows[k].Cells[7].Value.ToString() )
            {
                MessageBox.Show("The Year exsist, Please select a new year");
                return;
            }
            }

                dgvAddrecords.Rows.Add(txtCode2.Text, dtpDate.Text, txtName2.Text, txtOwnersnane.Text, cboRatetype.Text, txtDistrict.Text, txtTown.Text, cboYear2.Text, txtYearlydues.Text);


                int sum = 0;
                for (int i = 0; i < dgvAddrecords.Rows.Count; ++i)
                {
                    sum += Convert.ToInt32(dgvAddrecords.Rows[i].Cells[8].Value);
                }

                txtAmountdue.Text = sum.ToString();
 
Share this answer
 

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