Tuesday, 5 May 2015

Remove or Delete DataGridView Selected Row Button Click Using in C# Window application

Remove or Delete DataGridView Selected Row

DataGridview Selected Row Remove or Delete Button Click Event in C# Window Application.

                                         DEMO



     C# CODING


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

namespace WindowsFormsApplication2
{
    public partial class Home11 : Form
    {
        public Home11()
        {
          
            InitializeComponent();
        }

        private void Home11_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Drizzles\Documents\Visual Studio 2012\WindowsFormsApplication2\WindowsFormsApplication2\Database1.mdf;Integrated Security=True");
            con.Open();

            SqlCommand cmd = new SqlCommand("select * from country", con);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.Read())
            {
                dataGridView1.DataSource = dt;
            }
          }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count>1)
            {
                if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].IsNewRow != true)
                {
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                }
            }
            else {

                MessageBox.Show("No Record(s) Found");           
            }
        }      
    }
}










0 comments:

Post a Comment