【问题标题】:Populate TextBox with Text from DataTable - Combo Box使用 DataTable 中的文本填充 TextBox - 组合框
【发布时间】:2017-01-16 08:08:06
【问题描述】:

我有一个在我的窗口加载时声明的数据表(两个数据表,但我们现在假设有一个),这个数据表填充有:

  • 笔记ID
  • 笔记名称
  • 注意

NoteID:我的 SQL 数据库中的第一列给出了 Note 和 ID

NoteName:我的 SQL 数据库中的第二列为便笺命名

注意:这是第三列,也是最后一列,它给出了注释的文本(可能很长,它是一个 VarChar(MAX))

目前我的 DataTable 填充没有问题,但是我想用与在组合框中选择的 "NoteName" 关联的 "Note" 列填充我的 TextBox(Named: textResult)。

//Setup connection to server
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = "123.123.123.123";
        builder.InitialCatalog = "DiscoverThePlanet";
        builder.UserID = "TestPerm";
        builder.Password = "Test321";

        string connectionString = builder.ConnectionString;

        DataTable dtNotes = new DataTable();
        DataTable dtTemplateNotes = new DataTable();


        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();

            SqlCommand cmdNotes = new SqlCommand("SELECT NoteID, NoteName, Note FROM Notes", conn);
            SqlCommand cmdTemplateNotes = new SqlCommand("SELECT TemplateNoteID, TemplateNoteName, TemplateNote FROM TemplateNotes", conn);

            SqlDataReader readerNotes = cmdNotes.ExecuteReader();

            dtNotes.Columns.Add("NoteID", typeof(string));
            dtNotes.Columns.Add("NoteName", typeof(string));
            dtNotes.Columns.Add("Note", typeof(string));
            dtNotes.Load(readerNotes);

            SqlDataReader readerTemplateNotes = cmdTemplateNotes.ExecuteReader();

            dtTemplateNotes.Columns.Add("TemplateNoteID", typeof(string));
            dtTemplateNotes.Columns.Add("TemplateNoteName", typeof(string));
            dtTemplateNotes.Columns.Add("TemplateNote", typeof(string));
            dtTemplateNotes.Load(readerTemplateNotes);

            // Temporary loop to see if the DataTable (dt) has any data?!?
            //foreach (DataRow thisRow in dt.Rows)
            //{
            //    MessageBox.Show(thisRow["NoteName"].ToString());
            //}

            // Define the columns BEFORE setting the item source
            noteNamesList.SelectedValuePath = "NoteID";
            noteNamesList.DisplayMemberPath = "NoteName";

            templateNoteNamesList.SelectedValuePath = "TemplateNoteID";
            templateNoteNamesList.DisplayMemberPath = "TemplateNoteName";

            // Set the ItemSource to my fully loaded data table!
            noteNamesList.ItemsSource = dtNotes.DefaultView;
            templateNoteNamesList.ItemsSource = dtTemplateNotes.DefaultView;

            //DEBUG START
            //MessageBox.Show("Hello");
            //DEBUG END

            conn.Close();
        }

就像说我必须使用不同的下拉菜单,但我们现在只使用一个名为“dtNotes”的下拉菜单

我对此很陌生,如果我尝试在我的代码中的任何地方引用“dtNotes”,它就找不到它?

如果你需要的话,我的 textResult XAML:

<TextBox x:Name="textResult" HorizontalAlignment="Left" Height="760" Margin="42,141,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="1494" AcceptsReturn="True" TextChanged="textResult_TextChanged" BorderBrush="#FF00A9CF" FontFamily="DengXian" FontSize="14.667" Background="#FFF3FFFE" Grid.ColumnSpan="2"/>

如果需要,组合框:

 <ComboBox x:Name="noteNamesList" HorizontalAlignment="Left" Height="28" Margin="42,101,0,0" VerticalAlignment="Top" Width="240" BorderBrush="White" Opacity="0.985" FontFamily="DengXian" FontSize="13.333"></ComboBox>

希望我已经为您提供了足够的信息,非常感谢您的帮助。

PS 这是在 WPF 上

【问题讨论】:

  • 您正在使用哪个平台使用 Xaml?
  • 哦忘了说这是WPF,是这个意思吗? @Takarii
  • 您应该能够简单地使用ComboBox.SelectionChanged 事件并在其中使用TextBox.Text = ComboBox.SelectedValue 方法。请记住,如果您的数据表是本地的(即,在特定方法中),您将无法在任何其他方法中引用它们。您需要将 DataTable 声明移动到类范围。您的 sn-p 表明这可能是一种可能性
  • @Takarii 啊好的,感谢您的评论,那么我的 DataTables 目前在哪里是本地的吗?我如何使它们成为非本地的?
  • 在命名空间和构造函数之间的区域,放在那里

标签: c# mysql sql wpf


【解决方案1】:

看起来您可能将数据表声明为该方法的局部变量。

DataTable 声明放在Namespace 声明和类构造函数之间的空格中

Namespace MyProject
{
    public sealed partial class MyClass : Page
    {
        //Class wide variables go here

        DataTable mytable = new DataTable();

        class MyClass()
        {

        }
    }
}

之后,您应该能够在 ComboBox.SelectionChanged 事件中引用您的 DataTable 以将您的值分配给 TextBox

编辑:

ComboBox.SelectionChanged 事件内使用

TextBox.Text = ComboBox.SelectedValue.ToString();

其中TextBox 是要分配到的文本框的名称,ComboBox 是要从中获取值的组合框的名称。

或者,如果像您的情况一样,它是组合框无法直接处理的第三个值

TextBox.Text = DataTable.Rows[ComboBox.SelectedIndex]["Note"].ToString()

【讨论】:

  • 啊,太棒了,这已经让我可以在我的代码中使用我的数据表了。你能帮我看看为文本框声明什么吗?会不会像 textResult.Text = noteNamesList.SelectedValue(dtNotes);我会在哪里声明我想要数据表中的“Note”列?非常感谢
  • 只是为了确认一下,文本框的值是组合框选择中指定的值?
  • 这会打印 NoteID 像 1、2、3 等等,而不是实际的“Note”?
  • 尝试使用TextBox.Text = DataTable.Rows[ComboBox.SelectedIndex]["Note"].ToString()
  • 精彩,已排序,非常感谢您的帮助!
【解决方案2】:

您使用复杂的代码从 sql 表中加载数据。 您可以轻松使用SqlConnectionSqlDataAdapter

SqlConnection conn = new SqlConnection("Your Connection Settings");
string command="Select Note FROM Notes Where NoteName='" + comboNoteName.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(command, conn);
DataTable dt = new DataTable();
da.Fill(dt);

由此,DataTable 由 Data 和 Data 类型以及 sql Table 填充。 现在,填充文本框:

if(dt.Rows.Count>0)
   textResult.Text = dt.Rows[0]["Note"].ToString();

编辑:

您也可以使用它: 您可以在 Form_Load 事件中填写一次 DataTable 并在需要的地方经常使用它:

SqlConnection conn = new SqlConnection("Your Connection Settings");
string command="Select NoteId, NoteName, Note FROM Notes";
SqlDataAdapter da = new SqlDataAdapter(command, conn);
DataTable dt = new DataTable();
da.Fill(dt);

现在,在 ComboBox_SelectedIndexChanged 事件中放入:

if(dt.Rows.Count > 0)
    textResult.Text = dt.Select("NoteName='" + comboNoteName.Text + "'")[0]["Note"].ToString();

【讨论】:

  • 这并不能解决遇到的问题。它也不处理所选值来自组合框的事实。
  • 在第 2 行,comboNoteName 是提到的 ComboBox。
  • 请记住,这会导致每次更改选择时都会向数据库发送一个新查询。虽然它可能很小,但我肯定会避免发送超出需要的请求
  • 好的。你的权利。但它也可以访问。查看编辑。
猜你喜欢
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 2020-10-12
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多