Programming for Everybody: combobox
Showing posts with label combobox. Show all posts
Showing posts with label 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); } } } }

How to get value selected item from combobox in VB.NET-with source code

 


Imports System.Data.SqlClient


Public Class Form5


    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim con As New SqlConnection(" data source=.;initial catalog=sports1;integrated security=true")


        Dim cmd As New SqlCommand("select id,name from sports", con)


        Dim da As New SqlDataAdapter


        da.SelectCommand = cmd


        Dim table1 As New DataTable


        da.Fill(table1)


        Dim row As DataRow = table1.NewRow


        row(0) = 0

        row(1) = "Select sport"


        table1.Rows.InsertAt(row, 0)


        ComboBox1.DataSource = table1

        ComboBox1.DisplayMember = "name"

        ComboBox1.ValueMember = "id"








    End Sub


    

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

        Label1.Text = ComboBox1.SelectedValue


    End Sub

End Class

How to populate combobox based on another combobox selection item in vb.net With Source Code



Imports System.Data.SqlClient

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim constr1 As String = "data source=.; initial catalog=names;integrated security=true"

        Using con1 As SqlConnection = New SqlConnection(constr1)


            Using sda1 As SqlDataAdapter = New SqlDataAdapter(" Select id,name from table1", con1)


                Dim dt As DataTable = New DataTable

                sda1.Fill(dt)



                Dim row As DataRow = dt.NewRow()

                row(0) = 0

                row(1) = "Select"

                dt.Rows.InsertAt(row, 0)

                ComboBox1.DataSource = dt

                ComboBox1.DisplayMember = "name"

                ComboBox1.ValueMember = "id"


            End Using


        End Using






    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged


        Dim con1 As SqlConnection = New SqlConnection("Data source=.\;initial catalog=names;integrated security=true")

        con1.Open()
        Dim strsql As String
        strsql = "Select tel from table1 where name='" + ComboBox1.Text + "'"

        Dim cmd As New SqlCommand(strsql, con1)

        Dim myreader As SqlClient.SqlDataReader

        myreader = cmd.ExecuteReader

        ComboBox2.Items.Clear()
        While myreader.Read


            ComboBox2.Items.Add(myreader("tel"))


        End While


        con1.Close()


    End Sub
End Class

تحميل برنامج صغير لتسجيل اصناف في قاعدة بيانات SQl server وعرضها في combobox مع امكانية الاضافة والحذف والتعديل vb.net

نموذج بسيط  لتسجيل أصناف وعرضها في أداة Combobox مع عدام تكرار الاصناف مع امكانية  الحذف وكذلك تم اضافة قاعدة البيانات (SQl Server (products.


تحميل