locked
adding row in datagridview RRS feed

  • Question

  • hi.....

         can anyone please help me out.......

     

      i am using datagridview in my windows form........

    i need to add row dynamically.........after adding one row.......after entering value at last cell of the datagridview ,msgbox should be displayed "asking whether to add a row"if yes.....a new row should be added to my datagridview.......

     

    thank u

    in advance......

    Friday, February 8, 2008 12:33 PM

Answers

  •  

    Hi mrkmrk,

    If your DataGridView is not data bound, you can handle the CellEndEdit event of the DataGridView and check if the cell is the last cell of your DataGridView. If it is, show the MessageBox to ask the user whether to add a new row. Here is a sample for you information.

    Code Snippet

            private void Form36_Load(object sender, EventArgs e)

            {

                this.dataGridView1.AllowUserToAddRows = false;

                this.dataGridView1.Rows.Add();

                this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

            }

     

            void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

            {

                if (e.ColumnIndex == this.dataGridView1.Columns.Count - 1 && e.RowIndex == this.dataGridView1.RowCount - 1)

                {

                    if(MessageBox.Show("Do you want to add another row?","Ask", MessageBoxButtons.YesNo) == DialogResult.Yes)

                        this.dataGridView1.Rows.Add();

                }

            }

     

    If your DataGridView is data bound, you can still take the logic, but you should not add the row to the row collection of DataGridView, you should add row to the row collection of the data source.

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, February 14, 2008 3:49 AM