开发者

How set color for cell (DataGrid WPFtoolKIt)?

How set color for cell (DataGrid WPFtoolKIt)?

i need this set RED color. how to do it? DataTable dtSet = new DataTable();

    string sql = @"requevst";
    using (MySqlConnection connection = ConnectToDataBase.GetConnection())
    {
        ...
        int count = adapter.Fill(dtSet);
    }

    double totalPrice = 0;
    foreach (DataRow row in dtSet.Rows)
    {
        totalPrice += Double.Pars开发者_StackOverflow中文版e(row["price"].ToString());
    }
    DataRow lastRow = dtSet.NewRow();
    lastRow["price"] = totalPrice;
    dtSet.Rows.Add(lastRow);

    datagrid.DataContext = dtSet;


I have never used datasets, but it is a simple task with clr-objects:

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:

<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>

However, I recommend you to show total sum in separate TextBlock.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜