http://tech.it168.com/msoft/2008-06-12/200806120637662.shtml
using System; 2using System.Collections.Generic; 3using System.Text; 4using System.Windows.Forms; 5using System.Data; 6using System.Drawing; 7using System.ComponentModel; 8 9namespace OtherTools 10{ 11 class DataGridViewAddSumRow 12 { 13 private DataGridView dgv = null; 14 private DataTable dt = null; 15 public DataGridViewAddSumRow() 16 { 17 } 18 /**//// <summary> 19 /// 设置表格的数据源 20 /// </summary> 21 public DataTable dataTableName 22 { 23 set 24 { 25 this.dt = value; 26 } 27 } 28 /**//// <summary> 29 ///传递表格的名称 30 /// </summary> 31 public DataGridView DgvName 32 { 33 set 34 { 35 dgv = value; 36 } 37 } 38 /**//// <summary> 39 /// 开始添加合计行 40 /// </summary> 41 public void begin() 42 { 43 initDgv(); 44 } 45 private void initDgv() 46 { 47 if (dgv != null) 48 { 49 50 this.dgv.DataSourceChanged += new EventHandler(dataGridView_DataSourceChanged); 51 this.dgv.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView_ColumnHeaderMouseClick); 52 this.dgv.CellValueChanged += new DataGridViewCellEventHandler(dataGridView_CellValueChanged); 53 this.dgv.AllowUserToAddRows = false; 54 dgv.DataSource = dt; 55 } 56 } 57 /**//// <summary> 58 /// 计算合计算 59 /// </summary> 60 /// <param name="dgv">要计算的DataGridView</param>
private void SumDataGridView(DataGridView dgv) 62 { 63 if (dgv.DataSource == null) return; 64 //DataTable dt = (DataTable)dgv.DataSource; 65 if (dt.Rows.Count < 1) return; 66 decimal[] tal = new decimal[dt.Columns.Count]; 67 68 DataRow ndr = dt.NewRow(); 69 70 string talc = ""; 71 72 int number = 1; 73 foreach (DataRow dr in dt.Rows) 74 { 75 dr["@xu.Hao"] = number++; 76 int n = 0; 77 foreach (DataColumn dc in dt.Columns) 78 { 79 80 81 if (talc == "" && dc.DataType.Name.ToUpper().IndexOf("STRING") >= 0) 82 { talc = dc.ColumnName; } 83 84 85 if (dc.DataType.IsValueType) 86 { 87 string hej = dr[dc.ColumnName].ToString(); 88 try 89 { 90 if (hej != string.Empty) tal[n] += decimal.Parse(hej); 91 } 92 catch (Exception) { } 93 //if (hej != string.Empty) tal[n] += decimal.Parse(hej); 94 } 95 96 97 n++; 98 } 99 } 100 101 ndr.BeginEdit(); 102 for (int i = 0; i < dt.Columns.Count; i++) 103 { 104 if (tal[i] != 0) 105 ndr[i] = tal[i]; 106 } 107 ndr["@xu.Hao"] = ((int)(dt.Rows.Count + 1)).ToString(); 108 if (talc != "") ndr[talc] = "合计"; 109 ndr.EndEdit(); 110 dt.Rows.Add(ndr); 111 112 dgv.Rows[dgv.Rows.Count - 1].DefaultCellStyle.BackColor = Color.FromArgb(255, 222, 210); 113 dgv.Rows[dgv.Rows.Count - 1].ReadOnly = true; 114 115 if (dgv.Tag == null) 116 { 117 foreach (DataGridViewColumn dgvc in dgv.Columns) 118 { 119 dgvc.SortMode = DataGridViewColumnSortMode.Programmatic; 120 } 121 } 122 dgv.Tag = ndr; 123 } 124 private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) 125 { 126 DataGridView sortDgv = (DataGridView)sender; 127 int fx = 0; 128 if (sortDgv.AccessibleDescription == null) 129 { 130 fx = 1; 131 } 132 else 133 { 134 fx = int.Parse(sortDgv.AccessibleDescription); 135 fx = (fx == 0 ? 1 : 0); 136 } 137 sortDgv.AccessibleDescription = fx.ToString(); 138 if (sortDgv.Columns[e.ColumnIndex].Name == "@xu.Hao") return; 139 DataGridViewColumn nCol = sortDgv.Columns[e.ColumnIndex]; 140 141 if (nCol.DataPropertyName == string.Empty) return; 142 143 if (nCol != null) 144 { 145 sortDgv.Sort(nCol, fx == 0 ? ListSortDirection.Ascending : ListSortDirection.Descending); 146 147 } 148 //-- 149 DataRow dr = (DataRow)sortDgv.Tag; 150 DataTable dt = (DataTable)sortDgv.DataSource; 151 DataRow ndr = dt.NewRow(); 152 ndr.BeginEdit(); 153 for (int i = 0; i < dt.Columns.Count; i++) 154 { 155 ndr[i] = dr[i]; 156 } 157 dt.Rows.Remove(dr); 158 159 160 //if (e.ColumnIndex != 0) 161 { 162 int n = 1; 163 for (int i = 0; i < sortDgv.Rows.Count; i++) 164 { 165 DataGridViewRow dgRow = sortDgv.Rows[i]; 166 DataRowView drv = (DataRowView)dgRow.DataBoundItem; 167 DataRow tdr = drv.Row; 168 tdr.BeginEdit(); 169 tdr["@xu.Hao"] = n; 170 n++; 171 tdr.EndEdit(); 172 173 } 174 sortDgv.Refresh(); 175 sortDgv.RefreshEdit(); 176 177 } 178 ndr["@xu.Hao"] = ((int)(dt.Rows.Count + 1)).ToString(); 179 ndr.EndEdit(); 180 dt.Rows.Add(ndr); 181 sortDgv.Tag = ndr; 182 183 //-- 184 sortDgv.Sort(sortDgv.Columns["@xu.Hao"], ListSortDirection.Ascending); 185 sortDgv.Columns["@xu.Hao"].HeaderCell.SortGlyphDirection = SortOrder.None; 186 nCol.HeaderCell.SortGlyphDirection = fx == 0 ? SortOrder.Ascending : SortOrder.Descending; 187 sortDgv.Rows[sortDgv.Rows.Count - 1].DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 210); 188 189 }
190 private void dataGridView_DataSourceChanged(object sender, EventArgs e) 191 { 192 DataGridView dgv = (DataGridView)sender; 193 //DataTable dt = (DataTable)dgv.DataSource; 194 if (dt == null) return; 195 decimal[] tal = new decimal[dt.Columns.Count]; 196 if (dt.Columns.IndexOf("@xu.Hao") < 0) 197 { 198 DataColumn dc = new DataColumn("@xu.Hao", System.Type.GetType("System.Int32")); 199 dt.Columns.Add(dc); 200 dgv.Columns["@xu.Hao"].DisplayIndex = 0; 201 dgv.Columns["@xu.Hao"].HeaderText = "序号"; 202 203 dgv.Columns["@xu.Hao"].SortMode = DataGridViewColumnSortMode.Programmatic; 204 dgv.AutoResizeColumn(dgv.Columns["@xu.Hao"].Index); 205 206 dgv.Columns["@xu.Hao"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 207 dgv.Columns["@xu.Hao"].Visible = true; 208 } 209 SumDataGridView(dgv); 210 } 211 private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) 212 { 213 214 DataGridView dgv = (DataGridView)sender; 215 if (dgv.Tag == null || e.RowIndex < 0 || e.RowIndex == dgv.Rows.Count - 1) return; 216 217 string col = dgv.Columns[e.ColumnIndex].DataPropertyName; 218 if (col == string.Empty) return; 219 if (((DataRowView)dgv.Rows[e.RowIndex].DataBoundItem).Row.Table.Columns[col].DataType.IsValueType) 220 { 221 decimal tal = 0; 222 foreach (DataGridViewRow dgvr in dgv.Rows) 223 { 224 if (dgvr.Index != dgv.Rows.Count - 1) 225 { 226 string hej = dgvr.Cells[e.ColumnIndex].Value.ToString(); 227 if (hej != string.Empty) tal += decimal.Parse(hej); 228 } 229 } 230 if (tal == 0) 231 dgv[e.ColumnIndex, dgv.Rows.Count - 1].Value = DBNull.Value; 232 else 233 dgv[e.ColumnIndex, dgv.Rows.Count - 1].Value = tal; 234 } 235 } 236 237 } 238}