首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置单元格的颜色(DataGrid WPFtoolKIt)?

如何设置单元格的颜色(DataGrid WPFtoolKIt)?
EN

Stack Overflow用户
提问于 2010-10-08 19:36:07
回答 1查看 473关注 0票数 1

我需要这套红色的。该怎么做呢?新的dtSet = DataTable DataTable();

代码语言:javascript
复制
    string sql = @"requevst";
    using (MySqlConnection connection = ConnectToDataBase.GetConnection())
    {
        ...
        int count = adapter.Fill(dtSet);
    }

    double totalPrice = 0;
    foreach (DataRow row in dtSet.Rows)
    {
        totalPrice += Double.Parse(row["price"].ToString());
    }
    DataRow lastRow = dtSet.NewRow();
    lastRow["price"] = totalPrice;
    dtSet.Rows.Add(lastRow);

    datagrid.DataContext = dtSet;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-08 21:46:52

我从未使用过数据集,但这是一个使用clr-objects的简单任务:

代码语言:javascript
复制
public enum RowType { Ordinary, Total };
public class MyRowItem
{
    public string Name { get; set; }
    public int Price { get; set; }
    public RowType RowType { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var items = new List<MyRowItem>
        {
            new MyRowItem{Name="Jack White", Price = 2500, RowType=RowType.Ordinary},
            new MyRowItem{Name="John Black", Price = 3000, RowType=RowType.Ordinary}
        };
        items.Add(new MyRowItem { Name = null, Price = items.Sum(i => i.Price), RowType = RowType.Total });
        this.DataContext = items;
    }
}

Xaml:

代码语言:javascript
复制
<Window.Resources>
    <DataTemplate x:Key="priceTemplate">
        <TextBlock x:Name="priceText" Text="{Binding Price}" Margin="-1"/>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding RowType}" Value="Total">
                <Setter Property="Background" TargetName="priceText" Value="Red"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</Window.Resources>

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ФИО" Binding="{Binding Name}"/>
        <DataGridTemplateColumn Header="Цена" CellTemplate="{StaticResource priceTemplate}"/>
    </DataGrid.Columns>
</DataGrid>

但是,我建议您在单独的TextBlock中显示总和。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3890070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档