Programming for Everybody: C#:datagridview
Showing posts with label C#:datagridview. Show all posts
Showing posts with label C#:datagridview. Show all posts

C# tutorial: Retrieve data from SQL server based on comboBox Inside dataGridView with code

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 combobox_dataGridView { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=students;Integrated Security=True"); SqlCommand cmd = new SqlCommand("Select code_student From marks_students", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); code_student.DataSource = dt; code_student.DisplayMember = "code_student"; code_student.ValueMember = "code_student"; } private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=students;Integrated Security=True"); for (int i = 0; i <=dataGridView1.Rows.Count - 1; i++) { SqlCommand cmd = new SqlCommand("Select id,firstname,lastname,grade,marks From marks_students Where code_student = '" + dataGridView1.Rows[i].Cells[0].Value + "'", conn); conn.Open(); SqlDataReader myreader = cmd.ExecuteReader(); if (myreader.Read()) { dataGridView1.Rows[i].Cells[1].Value = myreader["id"].ToString(); dataGridView1.Rows[i].Cells[2].Value = myreader["firstname"].ToString(); dataGridView1.Rows[i].Cells[3].Value = myreader["lastname"].ToString(); dataGridView1.Rows[i].Cells[4].Value = myreader["grade"].ToString(); dataGridView1.Rows[i].Cells[5].Value = myreader["marks"].ToString(); } conn.Close(); } } private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dataGridView1.IsCurrentCellDirty) { dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); } } } }

Fill dataGridview from Excel's sheet depend on comBobox in C# with source code

 




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.OleDb;

namespace excel_import

{

    public partial class Form6 : Form

    {

        public Form6()

        {

            InitializeComponent();

        }

        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;" +

            @"Data Source=F:names2.Xlsx;Extended Properties = 'Excel 8.0;HDR=Yes'");

        private void Form6_Load(object sender, EventArgs e)

        {

            conn.Open();

            comboBox1.DataSource = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            comboBox1.DisplayMember = "Table_Name";

            comboBox1.SelectedIndex = -1;

            comboBox1.SelectedText = "Select sheet";

            conn.Close();

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {


        }


        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)

        {

            OleDbCommand cmd = new OleDbCommand("Select * From [" + comboBox1.GetItemText(comboBox1.SelectedItem) + "]", conn);

            OleDbDataAdapter da = new OleDbDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

        }

    }

}


Important videos c#

Programming C# : Connect SQL server database with Visual Studio C#  with source code

https://youtu.be/8_W0pEZawYg

c# tutorial for beginners - insert update delete search in sql server database and print (With code)

https://youtu.be/mSW7FFzdQMA

Programming C#: insert, update and  delete  data in datagridview without using database

https://youtu.be/Uoz4NS15lj8

C#: update all data from datagridview to database at once

https://youtu.be/_yansR7fE7o

c# tutorial for beginners: How to store images  update and delete in sql database

https://youtu.be/AwvrH8QK7DM

C# Tutorial : Retrieve data from Sql server database

https://youtu.be/lR_Ic_u8e90

C# Tutorial import data from Excel to SQL server

https://youtu.be/NUE-Sgq6SOg

c# tutorial for beginners: Add Row Total To DataGridView Footer

https://youtu.be/aL7wIUSfb-Q

c# tutorial for beginners: Insert Only Checked rows from datagridview into SQL database

https://youtu.be/XZEXOfcue8w

C#: Get data in datagridview from two tables using inner join and (Left - right - full) outer join.

https://youtu.be/eZQmFkL7Ie8

c# tutorial for beginners: Send data from datagridview to crystalreport without database in C#

https://youtu.be/TX-r99vKM3o

c# tutorial for beginners: Print data from dataGridView In C#

https://youtu.be/7EtsdSB72p4

c# tutorial for beginners: How to connect Microsoft access database to C#

https://youtu.be/wRzOkkptwVc

c# tutorial for beginners: How to get the sum of checked RadioButton Values

https://youtu.be/-lURcHfH7Qo

How to get selected text and selected value of comboBox in C#

https://youtu.be/18Z9v7jkERw

c# tutorial for beginners: Retrieve data from access database and navigation buttons

https://youtu.be/pQlhabZPZSU

c# tutorial for beginners: Delete row from datagridview and sql server database at once in C#

https://youtu.be/SUFVWlJGt8E

c# tutorial for beginners - How to search multiple columns access database using one textBox in C#

https://youtu.be/Ni4IDcufTos

C# Tutorial - message box exit application

https://youtu.be/jVNGCBQlXlI

c# tutorial for beginners - How to get the selected items in the combobox and show them in a listbox

https://youtu.be/q-FDCz7u1DM

C#: datagridview change cell backcolor based on value

https://youtu.be/lVYvDJn-r9E

c# tutorial for beginners - How to make validation on textbox in c#

https://youtu.be/wcme5LmppU8

How to fill combobox from sql server database and get selected value from a combobox in c#

https://youtu.be/ti_zVHaRT_U

How to search data in datagridview in c# without using database

https://youtu.be/iuDRFIXvKcg

c# tutorial for beginners - Insert all data of a dataGridView to access database at once  with  code

https://youtu.be/l0pPXY5shjw

Programming C#:  How to prevent duplicate data when insert to datagridview

https://youtu.be/6Ea1XGTjFnI

C#: Search for value in DataGridView in a column(without database)

https://youtu.be/1pkcAnZNAPE

C#:Show and hide characters password

https://youtu.be/mEwFk-46l7s

Programming in C # tutorial:  Login Form in C # with SQL Server with source code

https://youtu.be/NyTnhCsYlGQ 

insert update delete search and print in sql server databse using C# [with Source code]


 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 Insert_update_delete_search_and_print_in_sql

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        SqlConnection conn = new SqlConnection("Data source=.;initial catalog=database3;integrated security=true");

        private void Form2_Load(object sender, EventArgs e)

        {

            bind_data();

        }

        private void bind_data()

        {

            SqlCommand cmd1 = new SqlCommand("Select id,fname As firstname,lname As Lastname,sum from Table1", conn);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd1;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

            dataGridView1.DefaultCellStyle.Font = new Font("arial", 12);

        }


        private void button1_Click(object sender, EventArgs e)

        {

            SqlCommand cmd2 = new SqlCommand("Insert into Table1(id,fname,lname,sum)Values(@id,@firstname,@lastname,@sum)", conn);

            cmd2.Parameters.AddWithValue("id", textBox1.Text);

            cmd2.Parameters.AddWithValue("firstname", textBox2.Text);

            cmd2.Parameters.AddWithValue("lastname", textBox3.Text);

            cmd2.Parameters.AddWithValue("sum", textBox4.Text);

            conn.Open();

            cmd2.ExecuteNonQuery();

            conn.Close();

            bind_data();


        }


        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";

            textBox2.Text = "";

            textBox3.Text = "";

            textBox4.Text = "";


        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            int index;

            index = e.RowIndex;

            DataGridViewRow selectedrow = dataGridView1.Rows[index];

            textBox1.Text = selectedrow.Cells[0].Value.ToString();

            textBox2.Text = selectedrow.Cells[1].Value.ToString();

            textBox3.Text = selectedrow.Cells[2].Value.ToString();

            textBox4.Text = selectedrow.Cells[3].Value.ToString();



        }


        private void button3_Click(object sender, EventArgs e)

        {

            SqlCommand cmd3 = new SqlCommand("Update Table1 Set fname=@firstname,lname=@lastname,sum=@sum where id=@id", conn);


            

            cmd3.Parameters.AddWithValue("firstname", textBox2.Text);

            cmd3.Parameters.AddWithValue("lastname", textBox3.Text);

            cmd3.Parameters.AddWithValue("sum", textBox4.Text);

            cmd3.Parameters.AddWithValue("id", textBox1.Text);

            conn.Open();

            cmd3.ExecuteNonQuery();

            conn.Close();

            bind_data();

        }


        private void button4_Click(object sender, EventArgs e)

        {

            SqlCommand cmd4 = new SqlCommand("Delete from Table1 where id=@id", conn);

            cmd4.Parameters.AddWithValue("id", textBox1.Text);

            conn.Open();

            cmd4.ExecuteNonQuery();

            conn.Close();

            bind_data();

        }


        private void button5_Click(object sender, EventArgs e)

        {

            SqlCommand cmd1 = new SqlCommand("Select id,fname As firstname,lname As Lastname,sum from Table1 where fname Like @firstname+'%'", conn);

            cmd1.Parameters.AddWithValue("firstname", textBox5.Text);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd1;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

            dataGridView1.DefaultCellStyle.Font = new Font("arial", 12);

        }


        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

        {

            Bitmap imagebmp = new Bitmap(dataGridView1.Width, dataGridView1.Height);

            dataGridView1.DrawToBitmap(imagebmp, new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height));

            e.Graphics.DrawImage(imagebmp, 120, 20);

        }


        private void button6_Click(object sender, EventArgs e)

        {

            printPreviewDialog1.Document = printDocument1;

            printPreviewDialog1.PrintPreviewControl.Zoom = 1;

            printPreviewDialog1.ShowDialog();

        }

    }

}