我必须显示用户选择的表中的每一行,每次看起来都不错,但表中填充了空行,我使用DataGrid来显示数据。谁能告诉我这里我做错了什么?
private void showSelectedTable(object sender, RoutedEventArgs e)
{
if (mysql.IsChecked == true)
{
string query = "SELECT * FROM " + tableListBox.SelectedItem.ToString();
MySqlCommand myTableContent = new MySqlCommand(query, (MySqlConnection)conn);
try
{
MySqlDataReader myReader = null;
myReader = myTableContent.ExecuteReader();
choosenDataTable = myReader.GetSchemaTable();
foreach (DataRow row in choosenDataTable.Rows)
{
DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = row.Field<String>("ColumnName");
tableDataGrid.Columns.Add(textColumn);
}
if (myReader.HasRows)
{
while (myReader.Read())
{
ObservableCollection<String> observable = new ObservableCollection<String>();
for (int i = 1; i < myReader.Columns.Count; i++)
{
observable.Add(myReader.GetString(i));
}
tableDataGrid.Items.Add(observable);
}
}
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}