我需要这套红色。怎么做? 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.Parse(row["price"].ToString());
}
DataRow lastRow = dtSet.NewRow();
lastRow["price"] = totalPrice;
dtSet.Rows.Add(lastRow);
datagrid.DataContext = dtSet;
答案 0 :(得分:0)
我从未使用过数据集,但这是一个使用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>
但是,我建议您在单独的TextBlock中显示总和。