Skip to content
Advertisement

Load Data into Datagridview Combobox

I have 2 field First “Name” and second “NIC” the “Name” Is text field and “NIC” Is combobox field.

My query is i want to load data which is already save into SQL and i want to load it again on my grid how i can do that LIKE the below is my sql result

name nic
FT Muhammad Ejaz 32-3242-23532
DMB Omer 654564
Yasir Wajid 35
HO Zeeshan Hussain 654987987
SC Ameen Ghulam Rasool 64654
BBBT Hafiz Adil 5464
DHA Ghayas 6456
SC Jameel Maseeh 6456
GBB Abdullah Khairat Ali 65456465
DHA Hamid Manzoor 762837

I am new in C# if Any Missing Please ignore

Here is my Code for These two field in C#

  string mainconn = ConfigurationManager.ConnectionStrings["MyCONN"].ConnectionString;
                SqlConnection sqlconn = new SqlConnection(mainconn);
                string sqlquery = "select top 5 Name,nic from salesman order by nic asc";
                sqlconn.Open();
                SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
                SqlDataAdapter sdr = new SqlDataAdapter(sqlcomm);
                DataTable dt = new DataTable();
                sdr.Fill(dt);            
                dataGridView1.Columns[1].DataPropertyName = "Name";           
                sqlconn.Close();

Regards,

Advertisement

Answer

dataGridView1.DataSource = yourData

But I would suggest using Entity Framework when working with databases. https://learn.microsoft.com/en-us/ef/core/

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement