ullas K

ullas K

  • NA
  • 55
  • 0

Row not adding automatically in datagridview when a data is fetched from other form

Oct 1 2012 2:57 AM
i have a datagridview in Form1  with columns Sl.No , Book no,Book name ,issue date, due date.
when i Enter a book no in Book No column the all the other details of the books
 are populated in other columns of datagridview and a new row is added where
 i can enter other book no to get its details displayed and so on .
For this to happen i am writing my code in Cell Value changed event of datagridview
My code is

private void dataGridViewEx1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (dataGridViewEx1.Rows[e.RowIndex].Cells[1].Value != null)
                {

                    con = db.open();
                    BookNumber = dataGridViewEx1.CurrentRow.Cells[1].Value.ToString();
                   
                    SqlDataAdapter adp = new SqlDataAdapter("SELECT E_NAME FROM BOOKS WHERE BOOK_NO='" + BookNumber.ToString() + "' ", con);

                    DataSet ds = new DataSet();
                    adp.Fill(ds, "s");
                   
                    if (ds.Tables["s"].Rows.Count >= 1)
                    {
                        dataGridViewEx1.CurrentRow.Cells[2].Value = ds.Tables["s"].Rows[0].ItemArray[0];
                       
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");

                        dataGridViewEx1.CurrentRow.Cells[3].Value = DateTime.Now;
                        dataGridViewEx1.CurrentRow.Cells[4].Value = DateTime.Now.AddDays(holddays);
                    }
                    else
                    {
                        MessageBox.Show("No Such Book Exists.Please check the Book Number", "easyLib", MessageBoxButtons.OK);
                        SendKeys.Send("{left}");
                    }
                    db.close();
                }
            }

        }

This is working fine.

Now if i do not know the book No means i have written code to display all the book No ,Book Name details
 in other form(Say FORM2)
when i press F1 key in Book No column in Form1
and from form2 i select the book no and when i press enter that book no should be displayed in my Form1 book no column.I was able to do that.For that My code in Form1 is

internal void PopulateDGVEX1Row(string prodCode )
        {
         
                    con = db.open();

                    dataGridViewEx1.CurrentRow.Cells[1].Value = prodCode.ToString();

                    SqlDataAdapter adp = new SqlDataAdapter("SELECT E_NAME FROM BOOKS WHERE BOOK_NO='" + prodCode.ToString() + "' ", con);

                    DataSet ds = new DataSet();
                    adp.Fill(ds, "s");
                    if (ds.Tables["s"].Rows.Count >= 1)
                    {

                      //  dataGridViewEx1.CurrentRow.Cells[1].Value = ds.Tables["s"].Rows[0].ItemArray[0];
                        dataGridViewEx1.CurrentRow.Cells[2].Value = ds.Tables["s"].Rows[0].ItemArray[0];
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");
                        dataGridViewEx1.CurrentRow.Cells[3].Value = DateTime.Now;
                        dataGridViewEx1.CurrentRow.Cells[4].Value = DateTime.Now.AddDays(holddays);
                    }
                    else
                    {
                        MessageBox.Show("No Such Book Exists.Please check the Book Number", "easyLib", MessageBoxButtons.OK);
                        SendKeys.Send("{left}");
                    }

                    dataGridViewEx1.RefreshEdit();

                    db.close();
               
        }

and in Form2 my code is

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                prodCode = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                f1.PopulateDGVEX1Row(prodCode);
              
                this.Dispose();
                this.Close();
             

            }
        }


I am getting the Book No in Form1 and its details in other Columns.But My problem is a new row is not added in Form1 when i am getting book no from  Form2 unless i enter any value in a datagridview cell in form1.(When i enter a Book No in Column1 a new row is added and i was able to add another book no).How can i make it add a new row automatically to datagridview in Form1 when i get book no from other form(Form2).ie i should be able to work in both ways.If i know the book no it's ok but for those which i do not know i should fetch it from other form and should get a new row added where i can enter the known book no at the  same time.
How can i do this
Thanks in advance



Answers (1)