我做到了以下列方式(工作,只有當你使用時綁定的數據源)
創建自定義DataGridView控件和繼承的DataGridView
public partial class MyGridView : DataGridView
聲明持有列名,summaryvalue和格式的類中顯示
[Serializable]
public class SummaryDataProperty
{
public string ColumnName { get; set; }
public string Format { get; set; }
internal decimal Value { get; set; }
}
彙總數據財產申報清單在MyDataGridView
public List<SummaryDataProperty> SummaryDataPropertyNames { get; set; }
數據綁定完成後,計算總結和在列標題處顯示它。
protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
{
base.OnDataBindingComplete(e);
if (SummaryDataPropertyNames.Count > 0)
{
if (DataSource is BindingSource)
{
var ds = (DataSource as BindingSource);
foreach (var prop in SummaryDataPropertyNames)
{
prop.Value = 0;
var col = this.Columns[prop.ColumnName];
foreach (var l in ds.List)
{
decimal val;
if (decimal.TryParse(Convert.ToString(l.GetType().GetProperty(col.DataPropertyName).GetValue(l, null)), out val))
prop.Value += val;
}
col.HeaderText = col.HeaderText.Split('[')[0].TrimEnd(' ') + " [" + prop.Value.ToString(prop.Format) + "]";
}
}
}
}
由於綁定數據源提供了數據源中對象的列表。它很容易在綁定源列表中循環。我不知道如何用對象數據源或BindingDatasource.Current做到這一點。我仍在尋找解決方案。
您應該添加C#標記。 –
我想知道爲什麼這個問題沒有足夠的重視。感謝@ Overflow012,我只是用正確的類別重新標記了這個問題。希望有人能提供出色的解決方案 – Esen