我很难填充这个表的原因是因为表格是动态创建的:
MySql.Data.MySqlClient.MySqlDataReader selection = mySql.QuerySelect(textBox1.Text); //textBox1.Text containts the text of the query to be executed
DataTable table = new DataTable(); // create table to hold results
// depending on the query construct the needed columns
for (int i = 0; i < selection.FieldCount; i++)
{
table.Columns.Add("Column " + i);
}
// while there are rows insert them
while (selection.Read())
{
object[] o = new object[selection.FieldCount];
for(int j=0; j<selection.FieldCount; j++)
{
o[j] = selection[j].ToString();
}
table.Rows.Add(o);
}
确定到目前为止,我的桌子构建完毕。
以下是我尝试过的事情:
带有datagrid的<DataGrid AutoGenerateColumns="False" Margin="73,264,17,12" Name="dataGrid1" ItemsSource="{Binding}" />
代码背后的代码:
dataGrid1.DataContext = table.DefaultView;
不显示结果。行已填充,第二行但没有内容......
使用ListView<ListView Height="72" Margin="78,253,17,0" Name="listView1" VerticalAlignment="Top" ItemsSource="{Binding}" />
代码背后的代码:
listView1.DataContext = table.DataSet;
listView1.DataContext = table.DefaultView;
请注意显示的行数正确,但内容错误。
带有ListBox的答案 0 :(得分:0)
您需要网格中的列,因此您可能不希望AutoGenerateColumns="False"
。
要使用<ListView>
,您需要在<GridView>
中自行制作列。
要使用<ListBox>
,请将DisplayTemplate
设置为包含列表框UI的<DataTemplate>
。
答案 1 :(得分:0)
如果您使用ListBox
,则需要为列表框设置ItemTemplate
。 ItemTemplate
告诉控件如何呈现每个数据项(从表的默认视图中DataRowView
)。
一个简单的DataTemplate
将是一个文本块,其text属性绑定到表中的一列
<ListBox ItemsSource="{Binding Path=MyTable.DefaultView}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeColumn}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>