Programming for Everybody: c# comBobox
Showing posts with label c# comBobox. Show all posts
Showing posts with label c# comBobox. 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;

        }

    }

}