0
votes

Scenario: There is following controls in the form:
datagridview, textbox1, textbox2, button(save,edit,update,delete)

1.By clicking on save button, data should be updated into datagridview at run-time.
2.By selecting complete row and clicking on edit button, data should be retrieved into textboxes.
3.By clicking on Update button, that data should be updated.
4.By selecting a complete row, the row should be deleted.

1
And your question is? - Josh
Need code regarding this scenario. - psn

1 Answers

0
votes

This is my code. I hope it will help you

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LINQ_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        LINQtestDataContext dc = new LINQtestDataContext();

        public void show_data()
        {
            dataGridView1.DataSource = (from t in dc.LinqTests
                                        select t);

        }

        public void insert_data()
        {
            try
            {
                LinqTest tbl = new LinqTest
                {
                ID=Convert.ToInt32(textBox_id.Text),
                Name=textBox_name.Text
            };
                dc.LinqTests.InsertOnSubmit(tbl);
                dc.SubmitChanges();
                MessageBox.Show("Data Inserted!!!");
                show_data();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void update_data()
        {
            try
            {
                LinqTest tbl = dc.LinqTests.Single(x=>x.ID==Convert.ToInt32(textBox_id.Text));
                tbl.Name = textBox_new_name.Text;
                dc.SubmitChanges();
                MessageBox.Show("Data Updated!!!");
                show_data();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void delete_data()
        {
            try
            {
                LinqTest tbl = dc.LinqTests.Single(x => x.ID == Convert.ToInt32(textBox_id.Text));
                dc.LinqTests.DeleteOnSubmit(tbl);
                dc.SubmitChanges();
                MessageBox.Show("Data Deleted!!!");
                show_data();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            show_data();
        }

        private void button_insert_Click(object sender, EventArgs e)
        {
            insert_data();
        }

        private void button_update_Click(object sender, EventArgs e)
        {
            update_data();
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            delete_data();
        }
    }
}