我正在研究WPF中的销售点项目,我希望将所有记录(与当前或最后一个客户ID相关联)检索到datagrid。我在其他项目中完成了相同的工作,同时从表中选择*并检索它。但是这次不知道为什么这段代码不起作用。我的问题包括输出图像,请仔细看看。
<DataGrid x:Name="dataGrid" ItemsSource="{Binding DataSource}" HorizontalAlignment="Left" Margin="115,312,0,0" VerticalAlignment="Top" Height="376" Width="864">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#1C75BC" />
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Height" Value="20" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="BarCode" Binding="{Binding BarCode}" Width="*" />
<DataGridTextColumn Header="ProductName" Binding="{Binding ProductName}" Width="*"/>
<DataGridTextColumn Header="UnitPrice" Binding="{Binding UnitPrice}" Width="*" />
<DataGridTextColumn Header="SalePrice" Binding="{Binding SalePrice}" Width="*" />
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" Width="*" />
</DataGrid.Columns>
</DataGrid>
private void FillDataGrid(int counttt)
{
SqlConnection connn = db.getConnection();
connn.Open();
string CmdString = string.Empty;
//DateTime dateTime = DateTime.UtcNow.Date;
//String d = (dateTime.ToString("dd/MM/yyyy"));
CmdString = "SELECT BarCode, ProductName, UnitPrice, SalePrice, Quantity From CustomerSale WHERE CId = '" + counttt + "'";
SqlCommand cmd = new SqlCommand(CmdString, connn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("CustomerSale");
sda.Fill(dt);
dataGrid.ItemsSource = dt.DefaultView;
connn.Close();
}
答案 0 :(得分:2)
禁用DataGrid.AutoGenerateColumns
。您在标记(<DataGrid.Columns>
)中为每个数据属性创建了列,并且在设置ItemsSource时,DataGrid还为每个属性生成列,因为AutoGenerateColumns的默认值为true
。
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding DataSource}" ...