自定义数据视图和内部数据网格资源问题

时间:2019-07-18 11:39:21

标签: c# wpf xaml data-binding datatable

我正在尝试自定义XAML中嵌套行中的数据列,但数据视图显示所有列。我尝试添加选择性数据网格文本列,但结果是相同的,即使我尝试隐藏列但未成功,它仍显示所有数据列。

第二个问题:我通过数据表使用存储过程获取数据,但是我的内部数据网格正在发出资源问题。

第3次需要使用CS文件中的XAML代码绑定数据表。

     <UserControl x:Class="mobilewpf.views.sale"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="350" d:DesignWidth="500">
<Grid Background="#FF93BCBC" >
    <DataGrid x:Name="salegrid1" AutoGenerateColumns="False" Margin="10,201,10,10" Loaded="salegrid_load" LoadingRowDetails="salegrid1_loadingrowdetails" SelectionMode="Single">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Invoice_id}" Header="ID" Width="100"/>
        <DataGridTextColumn Binding="{Binding Invoice_No}" Header="Invoice No" Width="100"/>
        <DataGridTextColumn Binding="{Binding Invoice_Date}" Header="Invoice Date" Width="100"/>
        <DataGridTextColumn Binding="{Binding customer_Name}" Header="Customer Name" Width="100"/>
        <DataGridTextColumn Binding="{Binding customer_Mobile}" Header="Customer Mobile" Width="*"/>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <DataGrid x:Name="innerGrid" ItemsSource="{Binding DataView}" 
                           CanUserAddRows="False">
                        <DataGrid.Columns>
                            <!--<DataGridTextColumn Binding="{Binding Invoice_No}" Header="Invoice No" Width="100" Visibility="Hidden"/>
                            <DataGridTextColumn Binding="{Binding Invoice_Date}" Header="Invoice Date" Width="100" Visibility="Hidden"/>
                            <DataGridTextColumn Binding="{Binding customer_Name}" Header="Customer Name" Width="100" Visibility="Hidden"/>
                            <DataGridTextColumn Binding="{Binding customer_Mobile}" Header="Customer Mobile" Width="*" Visibility="Hidden"/>
                            <DataGridTextColumn Binding="{Binding Invoice_id}" Header="ID" Width="100" Visibility="Hidden"/>-->
                            <!--<DataGridTextColumn Binding="{Binding Invoice_id}" Header="ID" Width="100"/>-->
                            <DataGridTextColumn Binding="{Binding Item_Name}" Header="Item Name" Width="100"/>
                            <DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity" Width="100"/>
                            <DataGridTextColumn Binding="{Binding UnitPrice}" Header="Unit Price" Width="100"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </StackPanel>                   
            </DataTemplate>
        </DataGrid.RowDetailsTemplate> 
     </DataGrid>
</Grid>

public partial class sale : UserControl
{
    DataSet ds = new DataSet();
    public sale()
    {
        InitializeComponent();
        //this.saledetaildata = ds.Tables["saledetaildata"].DefaultView;
    }
    public DataView saledetaildata
    {
        get;
        set;
    }
    private void salegrid_load(object sender, RoutedEventArgs e)
    {
        salegriddata();
        //aaatest();
    }
    public void salegriddata()
    {
        SqlConnection con = new SqlConnection("Data Source=IRFAN20170323\\SQLEXPRESS;Initial Catalog=husnain;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter("allsaleview", con);
        SqlDataAdapter da1 = new SqlDataAdapter("viewsale2", con);
        DataTable dt = new DataTable("saleData");
        da.Fill(dt);
        salegrid1.ItemsSource = dt.AsDataView();
        da1.Fill(ds, "saledetaildata");
        //salegrid1.ItemsSource =  ds.Tables["saleData"].DefaultView;
        //DataGrid grid = new DataGrid("gridOrderDetails");
        //innerGrid.ItemsSource = ds.Tables["saledetaildata"].DefaultView;
        //return ds.Tables[].DefaultView;

    }
    public DataSet saleData
    {
        get;
        set;
    }


    private void salegrid1_loadingrowdetails(object sender, DataGridRowDetailsEventArgs e)
    {
        DataGrid innerGrid = e.DetailsElement as DataGrid;

        if (innerGrid != null)
        {

        }
    }

    public void aaatest()
    {
        SqlConnection con = new SqlConnection("Data Source=IRFAN20170323\\SQLEXPRESS;Initial Catalog=husnain;Integrated Security=True");
        //SqlDataAdapter da = new SqlDataAdapter("viewsale", con);
        SqlDataAdapter da1 = new SqlDataAdapter("viewsale2", con);

        DataSet ds = new DataSet();
        da1.Fill(ds, "saledetaildata");
        innerGrid.ItemsSource = ds.Tables["saledetaildata"].DefaultView;
    }

0 个答案:

没有答案