【发布时间】: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 目前在哪里是本地的吗?我如何使它们成为非本地的?
-
在命名空间和构造函数之间的区域,放在那里