DataGridView in C#

 

                                  DataGridView in C#



datagridview in csharp





In this tutorial, we will learn how to use the DataGridView control and its supporting classes, in detail. The DataGridView control makes it easy to define the basic appearance of cells and the display formatting of cell values. and datagridview_mousevent () also using c#.


C# DataGridView Tutorial


Display is an SQL database table with using as DataGridView control and a DataTable. Use Windows Forms.

DataGridView displays data from SQL databases. It's a  specific table from a database  and displays it on a DataGridView. This is done with a DataAdapter 

Before this tutorial previous learn How to create login page in C#:>>How to create login form in C#  



create a database named as finacial.
create a table and named as login

create table login (id int primary key identity(1,1) not null, username varchar(50) not null, password varchar(50) not null);

create table   and named as recharge 

CREATE TABLE recharge(
id int primary key IDENTITY(1,1) NOT NULL,
mobilenetwork varchar(50) NULL,
mobilenumber varchar(50) NULL,
amount varchar(40) NULL,
date varchar(60) NULL,
status varchar(50) NULL);

And then create two empty file named as 

1.login.cs

2.rrmain.cs

1.login.cs

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 RRstudio_finacial_application
{
    public partial class Login : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=finacial;Integrated Security=True");
        public Login()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select Count(*) from login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'",con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                RR_Main rm = new RR_Main();
                rm.Show();
                this.Hide();
            }
            else { MessageBox.Show("please check user username and password"); }
            con.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
        }
    }
}


datagridview in csharp




 2.rrmain.cs


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.IO;
using System.Data.SqlClient;

namespace RRstudio_finacial_application
{
    public partial class RR_Main : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=finacial;Integrated Security=True");
        public RR_Main()
        {
            InitializeComponent();
            display();
            display2();
        }
  private void btnNew(object sender, EventArgs e)
        {
            textBox2.Text = "";
            textBox2.Clear();
            textBox3.Text = "";
            textBox3.Clear();
            comboBox1.SelectedIndex = -1;
            comboBox8.SelectedIndex = -1;
            textBox16.Clear();
       
        }

        private void btnsave(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cm = new SqlCommand("insert into recharge ( mobilenetwork, mobilenumber, amount , date , status) values ('" + comboBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + dateTimePicker2.Text + "','"+comboBox8.Text+"')", con);
            cm.ExecuteNonQuery();
            MessageBox.Show("Insert Successfully");

            textBox2.Clear();
            textBox16.Clear();
            textBox3.Clear();
            comboBox1.SelectedIndex = -1;
            comboBox8.SelectedIndex = -1;

            con.Close();
            display();
       
        }
        void display() { 
        con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from recharge",con);
           System.Data.DataTable dt = new System.Data.DataTable();
            sda.Fill(dt);
            dataGridView1.Rows.Clear();
            foreach (DataRow items in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = (n + 1).ToString();
                dataGridView1.Rows[n].Cells[1].Value = items[0].ToString();
                dataGridView1.Rows[n].Cells[2].Value = items[1].ToString();
                dataGridView1.Rows[n].Cells[3].Value = items[2].ToString();
                dataGridView1.Rows[n].Cells[4].Value = items[3].ToString();
                dataGridView1.Rows[n].Cells[5].Value = items[4].ToString();
                dataGridView1.Rows[n].Cells[6].Value = items[5].ToString();
            }
            con.Close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.TextLength <= 10)
            {

            }
            else { MessageBox.Show("mobile number will be upto 10 number"); }

        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            textBox16.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            comboBox1.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            textBox2.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            textBox3.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            comboBox8.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();

        }

        private void btnupdate(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cm = new SqlCommand("Update recharge set mobilenetwork='" + comboBox1.Text + "',mobilenumber='" + textBox2.Text + "',amount='" + textBox3.Text + "',status='"+comboBox8.Text+"' where id='" +textBox16.Text + "'", con);
            cm.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Update successfully");

            display();

        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from recharge where date like '%" + textBox15.Text + "%' or mobilenumber like '" + textBox15.Text + "%'", con);
            System.Data.DataTable dt = new System.Data.DataTable();
            sda.Fill(dt);
            dataGridView1.Rows.Clear();
            foreach (DataRow items in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = (n + 1).ToString();
                dataGridView1.Rows[n].Cells[1].Value = items[0].ToString();
                dataGridView1.Rows[n].Cells[2].Value = items[1].ToString();
                dataGridView1.Rows[n].Cells[3].Value = items[2].ToString();
                dataGridView1.Rows[n].Cells[4].Value = items[3].ToString();
                dataGridView1.Rows[n].Cells[5].Value = items[4].ToString();
                dataGridView1.Rows[n].Cells[6].Value = items[5].ToString();


            }
            con.Close();
           
        }

        private void btndelete(object sender, EventArgs e)
        {
             con.Open();
           SqlCommand cm = new SqlCommand("delete from recharge where id='"+textBox16.Text+"'", con);
           cm.ExecuteNonQuery();
           con.Close();
           MessageBox.Show("Delete successfully");
           textBox16.Clear();
           textBox2.Clear();
           textBox3.Clear();
           textBox15.Clear();
           comboBox1.SelectedIndex = -1;
           comboBox8.SelectedIndex = -1;
           display();
       
        }

private void RR_Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Change_Password cp = new Change_Password();
            cp.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

    }
}

In my datagridview is clickable and updates the data using datagridview_mousevent () and what it has in my screenshot I create the code like the way and when your data is entered automatically the data is display in Gridview table just copy and paste my code and use it.

In this step we will learn C# DataGridView it will help you with your need. The next time I will put some more interesting commands, and continue the project thank you this is helpful for you will Like That's all, You can customize this code further as per your requirement. 

And I hope you learn something from this tutorial and leave your comments on this tutorial.


                                       




Previous Post Next Post