Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:如何在DataGrid中添加自定义列?_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# WPF:如何在DataGrid中添加自定义列?

C# WPF:如何在DataGrid中添加自定义列?,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我对这个问题做了一些搜索,发现在WPF中使用DataGrid有两种主要方法。我仍然在学习WPF和“绑定”数据,所以任何提示都非常感谢 图中的代码是功能性的,但我觉得我对数据没有太多的控制权。我在windows窗体和数据网格方面的大部分经验都与GridView有关 编辑:关于我的问题实际上是什么,有些人感到困惑。让我澄清一下:注释掉的表单代码将创建自定义列。我如何用我的ObservableCollection中的数据以编程方式填充这些定制coulmns。我想在这里: foreach(客户列表中的客

我对这个问题做了一些搜索,发现在WPF中使用DataGrid有两种主要方法。我仍然在学习WPF和“绑定”数据,所以任何提示都非常感谢

图中的代码是功能性的,但我觉得我对数据没有太多的控制权。我在windows窗体和数据网格方面的大部分经验都与GridView有关

编辑:关于我的问题实际上是什么,有些人感到困惑。让我澄清一下:注释掉的表单代码将创建自定义列。我如何用我的ObservableCollection中的数据以编程方式填充这些定制coulmns。我想在这里:

foreach(客户列表中的客户cmr)
{
customerGridView.Rows.Add(cmr.customerID、cmr.customerName、cmr.customerArea);
}
有没有更简单的方法来处理我不知道的数据网格

我就在这里:

命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
客户=新客户();
ObservableCollection customerList=新的ObservableCollection();
公共主窗口()
{
初始化组件();
}
私有无效窗口\u加载\u 1(对象发送方,路由目标)
{
//DataGridTextColumn c1=新DataGridTextColumn();
//c1.表头=“客户ID”;
//c1.绑定=新绑定(“客户ID”);
//customerDg.Columns.Add(c1);
//DataGridTextColumn c2=新的DataGridTextColumn();
//c2.Header=“客户名称”;
//c2.绑定=新绑定(“客户名称”);
//customerDg.Columns.Add(c2);
//DataGridTextColumn c3=新的DataGridTextColumn();
//c3.页眉=“客户区域”;
//c3.绑定=新绑定(“客户区域”);
//customerDg.Columns.Add(c3);
customerList=customer.GetAllCustomers();
customerDg.ItemsSource=客户列表;
//foreach(客户列表中的客户)
//{
//客户物料添加(客户);
//}
}
}
}
命名空间WpfApplication1.Data\u层
{
类客户
{
静态连接字符串设置=ConfigurationManager.ConnectionString[“MSAccess 1”];
//返回所有产品的列表
公共静态ObservableCollection GetAllCustomers()
{
顾客;
ObservableCollection AllCustomers=新的ObservableCollection();
string p=“从客户中选择*”;
使用(OleDbConnection dbConn=新OleDbConnection(settings.ConnectionString))
{
尝试
{
dbConn.Open();
使用(OleDbCommand cmd=dbConn.CreateCommand())
{
cmd.CommandText=p;
使用(OleDbDataReader dbReader=cmd.ExecuteReader())
{
//确保数据集中有1行或多行
while(dbReader.Read())
{
客户=新客户();
customer.customerID=Convert.ToInt32(dbReader[“customerID”]);
customer.customerName=dbReader[“customerName”].ToString();
customer.customerArea=Convert.ToChar(dbReader[“customerArea”].ToString());
所有客户。添加(客户);
}
}
}
}
捕获(例外情况除外)
{
Console.WriteLine(“出了点问题:+ex”);
}
最后
{
dbConn.Close();
}
}
退回所有客户;
}
}
}
命名空间WpfApplication1.Business\u层
{
类客户
{
public int customerID{get;set;}
公共字符串customerName{get;set;}
公共字符customerArea{get;set;}
公众客户()
{
customerID=0;
客户名称=”;
customerArea='X';
}
公共客户(int customerID、字符串customerName、字符customerArea)
{
this.customerID=customerID;
this.customerName=客户名称;
this.customerArea=customerArea;
}
公共可观测集合GetAllCustomers()
{
return Customers.GetAllCustomers();
}
}
}

不确定,但这可能会对您有所帮助(这会添加类似复选框的列):

使用DataGridTemplateColumn可以创建自定义列


不确定,但这可能会对您有所帮助(这会添加类似复选框的列):

使用DataGridTemplateColumn可以创建自定义列



那么问题是什么?我假设问题在标题中,但我不知道实际问题是什么。在
窗口\u Loaded\u 1
中注释掉的代码应该可以工作。那么真正的问题在哪里呢?@Anothershubery取消注释说,代码会生成新的列标题,我无法用数据填充这些标题。如何使用可观察集合中的数据填充这些列?图片:那么问题是什么?我假设问题在标题中,但我不知道实际问题是什么。在
窗口\u Loaded\u 1
中注释掉的代码应该可以工作。那么真正的问题在哪里呢?@Anothershubery取消注释说,代码会生成新的列标题,我无法用数据填充这些标题。如何使用可观察集合中的数据填充这些列?图:这无疑给了我更多的行控制。你能给我一个关于所有XAML和模板如何工作的阅读资料吗
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="305.263" Loaded="Window_Loaded_1">
<Grid Margin="0,0,-8,0">
    <TextBox x:Name="customerNumberTxt" HorizontalAlignment="Left" Height="23" Margin="163,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="customerNameTxt" HorizontalAlignment="Left" Height="23" Margin="163,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="customerAreaTxt" HorizontalAlignment="Left" Height="23" Margin="163,66,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Label x:Name="customerNumberLbl" Content="Cutomer number:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.242,-2.146" Width="130"/>
    <Label x:Name="customerNameLbl" Content="Cutomer name:" HorizontalAlignment="Left" Margin="10,35,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.242,-2.146" Width="130"/>
    <Label x:Name="customerAreaLbl" Content="Cutomer area:" HorizontalAlignment="Left" Margin="10,63,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.242,-2.146" Width="130"/>
    <DataGrid x:Name="customerDg" AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="10,105,0,0" VerticalAlignment="Top" Height="205" Width="273">
        <!--<DataGrid.Columns>
            <DataGridTextColumn Header="Some header" ></DataGridTextColumn>
            <DataGridTextColumn Header="Some header 2"></DataGridTextColumn>
            <DataGridTextColumn Header="Some header 3"></DataGridTextColumn>
        </DataGrid.Columns>-->
    </DataGrid>

</Grid>
</Window>
<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>