【问题标题】:How select table from database with combobox?如何使用组合框从数据库中选择表?
【发布时间】:2019-10-06 20:26:49
【问题描述】:

我制作 wpf 数据库应用程序,我想使用组合框从我的数据库中选择表。

我有一个包含十个表的数据库。我可以连接到数据库,并且可以从表中选择/更新/插入...项目。我需要在表之间切换。例如,如果我点击 Table1,Table1 将被选中,如果我点击 Table2,Table2 将被选中。我相信 Combobox 对我的应用程序有好处。这是我的选择代码:

public MainWindow()
        {
            InitializeComponent();
            loadData();
        }

        public void loadData()
        {
            // vytvoření spojení
            MySqlConnection con = new MySqlConnection(spojeni);
            con.Open();

            MySqlCommand vybrat = con.CreateCommand();     
            vybrat.CommandText = "SELECT * FROM barva";        
            MySqlDataAdapter adapt = new MySqlDataAdapter(vybrat);        
            DataSet data = new DataSet();

            adapt.Fill(data);           
            dtGrid.ItemsSource = data.Tables[0].DefaultView;
          }

PS。我为我的英语道歉

【问题讨论】:

  • 在你的 C# 中创建一个表名的公共列表,并为选定项创建另一个公共字符串,将一个组合框绑定到它们,并在 SQL 中使用它。
  • 我知道我必须这样做,但我不知道该怎么做。我是一个非常糟糕的程序员。这是我的组合框: konzolevýrobceplatforma
  • 这是一个如何将组合框绑定到列表的示例:stackoverflow.com/questions/21898022/…
  • 我阅读了示例,但我仍然不明白。如果我给你发一个我的项目的链接,你能给我解释一下吗?

标签: c# database wpf combobox


【解决方案1】:

这是一个非常简单的示例,将组合框绑定到字符串列表,并在按下按钮时使用选定的字符串。

这是 C# 代码隐藏文件:

using System.Collections.Generic;
using System.Windows;

namespace WpfCombobox
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        public string MySimpleStringProperty { get; set; }

        public List<string> MyListProperty { get; set; } = new List<string>() { "one", "two", "three" };

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show($"Item is {this.MySimpleStringProperty}");
        }
    }
}

显然,您将在 SQL 中使用它,而不是只在消息框中显示所选项目。

这是 WPF:

<Window x:Class="WpfCombobox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfCombobox"
        mc:Ignorable="d"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <ComboBox ItemsSource="{Binding MyListProperty}" 
                  SelectedItem="{Binding MySimpleStringProperty}" 
                  IsSynchronizedWithCurrentItem="True" 
                  Text="Select Option"
                  Margin="5"/>
        <Button Click="ButtonBase_OnClick" Content="Click Me!" Margin="5" />
    </StackPanel>
</Window>

请注意,组合框有一个ItemSource,它绑定到字符串列表one, two, three,还有一个SelectedItem,当用户选择一个项目时会发生变化。

【讨论】:

  • 哦,我明白了。谢谢,我试试看。
  • 效果很好,谢谢。我有最后一个问题:如何创建动态文本框?我尝试了一些方法,但效果不正确。
  • “动态”是什么意思?您想在运行时创建控件,而不是在 XAML 中编写代码吗?此示例可能会有所帮助:stackoverflow.com/questions/12468658/… 否则,您应该提出一个新问题。
  • 是的,我的意思是这样的,但它对我不起作用。我会尝试提出一个新问题。再次感谢您的帮助。
【解决方案2】:

我猜你正在寻找这样的东西(连接字符串属于 web.config 上的配置):

<connectionStrings>
  <add name="YOUR CONNECTION" connectionString="Data Source= ;Initial Catalog= ; User ID= ;Password= ;" providerName="System.Data.SqlClient" />
</connectionStrings>
//Connection to Web.config connectionStrings
DataTable database = new DataTable();
string dbString = ConfigurationManager.ConnectionStrings["YOUR CONNECTION"].ConnectionString;
using (SqlConnection con = new SqlConnection(dbString))
{
    try
    {
        //SQL query
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM sys.tables", con);
        adapter.Fill(database);

        //Populate ddlTable DropDownList
        ddlTable.DataSource = database;
        ddlTable.DataTextField = "name";
        ddlTable.DataValueField = "name";
        ddlTable.DataBind();
        ddlTable.Items.Insert(0, new ListItem("-- Select Table --", "0"));
    }
    catch (Exception)
    {

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2019-08-28
    相关资源
    最近更新 更多