Insert,Delete and Update through datagrid


Inert,Delete,Update through data grid

In this application we can insert delete and update all through data grid.One thing is to remember is that I have used WPF tool kit Data grid here so if you don't know how to use it then see this post. The main thing that would help us to do this is dataset, as we know when ever we do any editing in our grid then that editing is also done in the dataset so by using the elements in the datagrid we can update , delete or insert data though datagrid.


Below this you can see the code behind the insert button.

        private void Insert_Click(object sender, EventArgs e)
        {
            for (int i = number; i < ds.Tables[0].Rows.Count; i++)
            {
                int rg = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
                string name = ds.Tables[0].Rows[i].ItemArray[1].ToString();

Insert(Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]), ds.Tables[0].Rows[i].ItemArray[1].ToString(),
ds.Tables[0].Rows[i].ItemArray[2].ToString(), ds.Tables[0].Rows[i].ItemArray[3].ToString());

             }
        }


Here is the code of Update Button.The thing to remember in updating the data is that I have taken two data sets and one dataset is updated in the datagrid up the other will be not changed , so in the loop I check every column of each row by comparing those two data sets and if any change is found then the change is made.



private void button4_Click(object sender, EventArgs e)
 {
   bool k = false;
   for (int i = 0; i < number; i++)
   {
   if ((ds.Tables[0].Rows[i].ItemArray[1].ToString() != ps.Tables[0].Rows[i].ItemArray[1].ToString())
   || (ds.Tables[0].Rows[i].ItemArray[2].ToString() != ps.Tables[0].Rows[i].ItemArray[2].ToString())
|| (ds.Tables[0].Rows[i].ItemArray[3].ToString() != ps.Tables[0].Rows[i].ItemArray[3].ToString()))
    {
      k = UpdateData(Convert.ToInt32(ps.Tables[0].Rows[i].ItemArray[0].ToString()), ds.Tables[0].Rows[i].ItemArray[1].ToString(),
ps.Tables[0].Rows[i].ItemArray[2].ToString(), ps.Tables[0].Rows[i].ItemArray[3].ToString());
     MessageBox.Show("Updated");
          }             
    }
 }


And the Code for the Delete button is given below.



      private void DeleteButton_Click(object sender, EventArgs e)
  {
  int i = Convert.ToInt32(controlGrid1.dataGrid1.SelectedIndex.ToString());
  bool d = Delete(Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]));
     if (d)
      {
          MessageBox.Show("Deleted");
          UPDATE_dataGrid();
       } 
   else
           MessageBox.Show("Could not delete");
}


Here is the download file for this project and also data base is given.The folders to download are DBMS_Assignment and databasefiles.


download


0 comments

Post a Comment

 
|  I Code Help. Blogger Template By Lawnydesignz Powered by Blogger