我是WPF新手。无法在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">
什么是您的XAML样子? –
@LorentzVedeler更新了代码 – Freelancer