C# tutorial: Retrieve data from SQL server based on comboBox Inside dataGridView with code
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
c# tutorial for beginners - insert update delete search in sql server database and print (With code)
Programming C#: insert, update and delete data in datagridview without using database
C#: update all data from datagridview to database at once
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
c# tutorial for beginners: Add Row Total To DataGridView Footer
c# tutorial for beginners: Insert Only Checked rows from datagridview into SQL database
C#: Get data in datagridview from two tables using inner join and (Left - right - full) outer join.
c# tutorial for beginners: Send data from datagridview to crystalreport without database in C#
c# tutorial for beginners: Print data from dataGridView In C#
c# tutorial for beginners: How to connect Microsoft access database to C#
c# tutorial for beginners: How to get the sum of checked RadioButton Values
How to get selected text and selected value of comboBox in C#
c# tutorial for beginners: Retrieve data from access database and navigation buttons
c# tutorial for beginners: Delete row from datagridview and sql server database at once in C#
c# tutorial for beginners - How to search multiple columns access database using one textBox in C#
C# Tutorial - message box exit application
c# tutorial for beginners - How to get the selected items in the combobox and show them in a listbox
C#: datagridview change cell backcolor based on value
c# tutorial for beginners - How to make validation on textbox in c#
How to fill combobox from sql server database and get selected value from a combobox in c#
How to search data in datagridview in c# without using database
c# tutorial for beginners - Insert all data of a dataGridView to access database at once with code
Programming C#: How to prevent duplicate data when insert to datagridview
C#: Search for value in DataGridView in a column(without database)
C#:Show and hide characters password
Programming in C # tutorial: Login Form in C # with SQL Server with source code
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();
}
}
}