0

私はWPFが初めてです。

WPF でデータグリッドをバインドしようとしています。

私のコード:

con = new SqlConnection(conClass.conSTR);
            try
            {
                con.Open();
                cmd = new SqlCommand("select * from login",con);
                da = new SqlDataAdapter(cmd);
                DataTable dt=new DataTable("login"); //DataSet ds = new DataSet(); 
                da.Fill(dt);
                gv.ItemsSource = dt.DefaultView;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

XAML:

<Window x:Class="WpfTestApp.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="428" Width="503" Loaded="Window_Loaded" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Grid>
        <TextBlock Margin="10" Text="This is a text block" Foreground="Red" TextTrimming="CharacterEllipsis" FontStretch="Normal" FontWeight="Bold"></TextBlock>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="10,29,0,0" Name="txtLoginID" VerticalAlignment="Top" Width="120" />
        <PasswordBox Name="txtPassword" PasswordChar="*" Margin="146,28,221,337"></PasswordBox>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="100,83,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"  />
        <DataGrid AutoGenerateColumns="False" Height="247" HorizontalAlignment="Left" Margin="37,130,0,0" Name="gv" VerticalAlignment="Top" Width="303" />
    </Grid>
</Window>

このコードにはエラーはありません。

しかし、それでも私のグリッドはテーブルにバインドされていません。

水平線が表示されるだけです。

スクリーンショット:

ここに画像の説明を入力

なぜこの問題に直面しているのかわかりません。

私を案内してください。

編集:

後のグリッド<DataGrid AutoGenerateColumns="True">

ここに画像の説明を入力

4

3 に答える 3

2

次のように変更してみてください。

<DataGrid AutoGenerateColumns="True" DataContext="{Binding}"> 
于 2013-06-03T08:38:46.817 に答える
1

Xaml を変更して設定してみてくださいAutoGenerateColumns="True".

これにより、グリッドはデータの列を作成できます。true に設定しない場合は、表示する列の列定義を追加する必要があります。

于 2013-06-03T08:34:19.043 に答える
1

<DataGrid AutoGenerateColumns="False">に変更

<DataGrid AutoGenerateColumns="True">

RowHeight="25" DataGridに追加

于 2013-06-03T08:34:23.863 に答える