【发布时间】:2011-10-03 00:36:10
【问题描述】:
我很难填充此表的原因是因为该表是动态创建的:
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 AutoGenerateColumns="False" Margin="73,264,17,12" Name="dataGrid1" ItemsSource="{Binding}" />
后面的代码:
dataGrid1.DataContext = table.DefaultView;
不显示结果。行被填充,第二行但没有内容......
与列表视图
<ListView Height="72" Margin="78,253,17,0" Name="listView1" VerticalAlignment="Top" ItemsSource="{Binding}" />
后面的代码:
listView1.DataContext = table.DataSet;
listView1.DataContext = table.DefaultView;
注意显示的行数正确,但内容错误。
带有列表框
类似的技术....
【问题讨论】: