Quantcast
Viewing all articles
Browse latest Browse all 12583

Datagridview row not adding automatically when data fetched from other Datagridview in another form

Hi,
      Can you please help me to solve out this

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, then all the  details of the books
 are populated in other columns of datagridview and a new row is added  automatically (Datagridview changes to edit mode)where i can enter other book no in book no column of that row  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 consider a situation where  i do not know the book No to enter.In such a situation  i have written code to display all thebook No ,Book Name details in a datagridview
 in other form(Say FORM2)
. when i press F1 key in Book No column in Form1
 form2 is displayed. i select the required book no from Form2 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 in this case 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 Form1when 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 in case if there is more book details to be entered

How can i do this .
Thanks in advance

Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>