列表框可以显示为表格

时间:2014-10-16 11:23:32

标签: c# mysql listview listbox windows-store-apps

我的Windows商店应用中有列表框项。当我从mysql数据库获取数据时,它看起来不太好看。在屏幕截图中显示

enter image description here

我希望将其显示为表格并具有清晰的格式

这是我的列表框的XAML代码

<ListBox x:Name="listbox2" HorizontalAlignment="Left" Height="253" Margin="564,60,0,0" Grid.Row="1" VerticalAlignment="Top" Width="397" d:IsHidden="True">
        <ListBoxItem x:Name="itm"/>
    </ListBox>

这是我的视图按钮的C#代码

private void Button_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            string Query = @"SELECT * FROM `bcasdb`.`tbl_department`;";
            //This is command class which will handle the query and connection object.
            MySqlConnection conn = new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
            MySqlCommand cmd = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader;
            conn.Open();
            MyReader = cmd.ExecuteReader();// this query will be executed and data saved into the database.           
            while (MyReader.Read())
            {
                //this.branchIDInput.Text = MyReader.GetString(1);
                    ListBoxItem itm = new ListBoxItem();
                    itm.Content = MyReader.GetString(0) + "         " + MyReader.GetString(1) + "       " + MyReader.GetString(2);
                   this.listbox2.Items.Add(itm);
                //ListBox a = new ListBox();
                // a.Items.Add();
                    //ListViewItem ad = new ListViewItem();


            }
            conn.Close();               
        }
        catch (Exception)
        {
            errormsgBox();
        }
    }

3 个答案:

答案 0 :(得分:0)

您可以使用Listview

<ListView Name="xlistview">

  <ListView.View>
    <GridView>
      <GridViewColumn DisplayMemberBinding="{Binding ID, UpdateSourceTrigger=PropertyChanged}" Width="80" Header="ID" />
      <GridViewColumn DisplayMemberBinding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="140" Header="Name" />
      <GridViewColumn DisplayMemberBinding="{Binding Dept, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Department" />
    </GridView>
  </ListView.View>


</ListView>

答案 1 :(得分:0)

没问题,写一个可以保存值的模型

循环while(MyReader.Read())并将它们存储到列表中,并将list作为itemssource绑定到listview。

答案 2 :(得分:0)