好吧,第一个wpf程序运行不起来了....请解惑,谢谢!

CTD 2015-09-05 10:45:09
打算加个DataGrid,然后显示点什么,出错了!是什么地方出问题了呢?

前台
<Window x:Class="jxc_w.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="650" Width="1024" VerticalAlignment="Center" WindowStartupLocation="CenterScreen">
<Grid>
<Button Content="退出" HorizontalAlignment="Left" Margin="555,562,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1" RenderTransformOrigin="4.68,1.211" Name="bntExit"/>
<Button Content="刷新" HorizontalAlignment="Left" Margin="344,562,0,0" VerticalAlignment="Top" Width="75" Name="bntRefresh"/>
<DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Name="myDataGrid" AutoGenerateColumns="True"/>

</Grid>
</Window>

台台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;

namespace jxc_w
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
getDate();
}

string SqlConnectStr = "Data Source=xxxxxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=sa;Password=xxxx";

private void getDate()
{
using (SqlConnection sqlConn=new SqlConnection(SqlConnectStr))
{
using (SqlCommand sqlComm = new SqlCommand())
{
sqlComm.CommandText = "SELECT TOP(20) 编码,名称,规格,产地,单位 FROM goodList WHERE SUBSTRING(编码,1,3)='101' AND 级别=3 ORDER BY dispOrder";
DataSet myDs = new DataSet();
SqlDataAdapter myAdapter = new SqlDataAdapter(sqlComm);
myAdapter.Fill(myDs);
this.myDataGrid.ItemsSource = myDs.Tables[0].DefaultView;
}
}
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
...全文
471 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
侯大虎 2015-09-24
  • 打赏
  • 举报
回复
#4正解,具体原因你在看看
zq1564171310 2015-09-22
  • 打赏
  • 举报
回复

 myAdapter.Fill(myDs);
 this.myDataGrid.ItemsSource = myDs.Tables[0].DefaultView;
注释这两句就可以了,前面操作数据库要检查一下,应该获取数据有问题
exception92 2015-09-06
  • 打赏
  • 举报
回复
把 查看详细信息 的错误 贴出来。
51Crack 2015-09-06
  • 打赏
  • 举报
回复

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition/>
  </Grid.RowDefinitions> 
  <Button Grid.Row="0" Content="退出" HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Width="75" Click="Button_Click_1" RenderTransformOrigin="4.68,1.211" Name="bntExit"/>
  <Button Grid.Row="1" Content="刷新" HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Width="75" Name="bntRefresh"/>
  <DataGrid Grid.Row="2" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Name="myDataGrid" AutoGenerateColumns="True"/>
</Grid>
或者把<Grid>改为<StackPanel>
bl_song 2015-09-05
  • 打赏
  • 举报
回复
this.myDataGrid.ItemsSource = myDs.Tables[0].DefaultView; 这句打上一个断点,调试一下

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧