Windows应用程序 - 使用下拉列表填充文本框/ datetimepicker

时间:2015-12-15 00:29:31

标签: c# database windows drop-down-menu

大家好日子。

我想使用连接到我的数据库的下拉列表在我的Windows应用程序中填充我的文本框等。我一直在谷歌上搜索,我得到的只是ASP.NET结果让我感到困惑。如果有人可以帮助我找到正确的方向,我就会嘲笑它。我仍然是编程的新手,所以我的代码可能看起来很乱,甚至可能对你们中的某些人都没有意义。但它到目前为止起作用:P

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ExamenIliasBelHaj
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    //Database connectie
    SqlConnection Tickets = new SqlConnection("Data                        Source=MOCHINO\\EINDEXAMEN;Initial Catalog=Ticket;Integrated Security=True");

    //Variabelen voor combobox
    SqlCommand command;
    SqlDataAdapter adapter;
    DataTable table;

    private void Form1_Load(object sender, EventArgs e)
    {

        //Dropdownlist
        string query = "SELECT Email, MelderID FROM Melder";
        fillCombo(zoekincidentCb, query, "MelderID", "Email");

    }

    private void opslaanBtn_Click(object sender, EventArgs e)
    {


        //Dit is voor Table Probleem
        SqlCommand a = new SqlCommand("Insert INTO Probleem(MeldingTitel,         Probleem, Handeling, Opgelost, BoekA, BoekB, BoekC, BoekD, Opdrachten,   Melddatum, Einddatum) Values (@MeldingTitel, @Probleem, @Handeling, @Opgelost,   @BoekA, @BoekB, @BoekC, @BoekD, @Opdrachten, @Melddatum, @Einddatum)", Tickets);

         // a.Parameters.AddWithValue("@ProbleemID", idTxt.Text);
            a.Parameters.AddWithValue("@MeldingTitel",   meldingtitelTxt.Text);
            a.Parameters.AddWithValue("@Probleem", probleemTxt.Text);
            a.Parameters.AddWithValue("@Handeling", handelingTxt.Text);
            a.Parameters.AddWithValue("@Opgelost", opgelostCb.SelectedItem);
            a.Parameters.AddWithValue("@BoekA", boekaCb.Checked);
            a.Parameters.AddWithValue("@BoekB", boekbCb.Checked);
            a.Parameters.AddWithValue("@BoekC", boekcCb.Checked);
            a.Parameters.AddWithValue("@BoekD", boekdCb.Checked);
            a.Parameters.AddWithValue("@Opdrachten", opdrachtenTxt.Text);
            a.Parameters.AddWithValue("@Melddatum", dtpStart.Value);
            a.Parameters.AddWithValue("@Einddatum", dtpEind.Value);


           //Dit is voor Table Melder

            SqlCommand b = new SqlCommand("Insert INTO Melder(Email, voornaam, achternaam) Values (@Email, @voornaam, @achternaam)", Tickets);
            b.Parameters.AddWithValue("@Email", emailTxt.Text);
            b.Parameters.AddWithValue("@voornaam", vnaamTxt.Text);
            b.Parameters.AddWithValue("@achternaam", anaamTxt.Text);

            Tickets.Open();
            a.ExecuteNonQuery();
            b.ExecuteNonQuery();
            Tickets.Close();

            emailTxt.Text = "";
            vnaamTxt.Text = "";
            anaamTxt.Text = "";
            meldingtitelTxt.Text = "";
            probleemTxt.Text = "";
            handelingTxt.Text = "";
            opdrachtenTxt.Text = "";
            boekaCb.Checked = false;
            boekbCb.Checked = false;
            boekcCb.Checked = false;
            boekdCb.Checked = false;
            opgelostCb.Items.Clear();                     
    }

在我的fillCombo中,我有以下内容。

       public void fillCombo(ComboBox combo, string query, string displayMember, string valueMember)
    {
        command = new SqlCommand(query, Tickets);
        adapter = new SqlDataAdapter(command);
        table = new DataTable();
        adapter.Fill(table);
        combo.DataSource = table;
        combo.DisplayMember = "ProbleemID";
        combo.ValueMember = valueMember;

    }

这是我根据答案得到的。

  private void zoekachternaamCb_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlCommand c = new SqlCommand("Select * from Melder, Probleem")
        vnaamTxt.Text = "voornaam";
    }

1 个答案:

答案 0 :(得分:0)

您必须在

上撰写查询
  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
          string query = "SELECT Name, DateofBirth FROM TableName Where ID ="+comboBox1.Text;

    SqlConnection sqlConn = new SqlConnection(conSTR);
    sqlConn.Open();
    SqlCommand cmd = new SqlCommand(query, sqlConn);
    SqlDataAdapter da=new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

   textBox1.Text = dt.Rows[0]["Name"].ToString();
   datePicker1.Text = dt.Rows[0]["DateOfBirth"].ToString(); 


    }