这个是我写的公用函数,它将DataTable中的数据直接显示到GridView中,GV拖过来后不需要任何设置
我这个函数有几个强大的功能,一般DT中的数据是从存储过程中取出来的,有时因为各种显示的需求,
所以需要做不同的设置,
如有一个表table中有列id,name,age,address,money,car
存储过程中这样写:
select id,name as '姓名',age as '年龄',address as '地址',money as 'Sum存款',car as 'Hide车牌号'
注意:
1.别名用中文名,因为GV中的列名就是这个名字
2.别名前加一些特殊字符,表示不同的意义,如Sum表示要对这列求和,Hide表示不显示这列
我目前开发的意义有:
Sum 求和 Hide隐藏 Fixed固定列 Last置为最后一列
最后说明一个功能:
ShowTotal 为真表示不显示下面的求和Footer,而是自动产生一行记录叫“总计”,并在这行里求和 如果不懂自己试试就明白了
public void GridViewShowTable(GridView gv, DataTable dt,bool ShowTotal )
{
if (ShowTotal && dt.Columns.Count > 0)
{
bool showTotal = false;
bool showTitle = false;
DataRow dr = dt.NewRow();
dr[0] = Guid.Empty;
for (int i = 1; i < dt.Columns.Count; i++)
{
string colName = dt.Columns[i].ColumnName;
if (colName.IndexOf("Sum") >= 0)
{
decimal tmpDec = 0;
for (int j = 0; j < dt.Rows.Count; j++)
{
if (dt.Rows[j][colName] != Convert.DBNull)
tmpDec += Convert.ToDecimal(dt.Rows[j][colName]);
}
dr[colName] = tmpDec;
showTotal = true;
}
else
{
if (!showTitle && dt.Columns[i].DataType == Type.GetType("System.String"))
{
dr[colName] = "总计 (共" + dt.Rows.Count.ToString() + "条)";
showTitle = true;
}
else
dr[colName] = Convert.DBNull;
}
}
if (showTotal) dt.Rows.Add(dr);
}
int LastIndex = -1;
int Flag_Fixed = 0;
int Flag_Sum = 0;
int Flag_Hide = 0;
int Flag_NoFilter = 0;
gv.Columns.Clear();
gv.OptionsBehavior.Editable = false;
for (int i = 0; i < dt.Columns.Count || LastIndex >= 0; i++)
{
if (i >= dt.Columns.Count)
{
if (LastIndex >= 0)
i = LastIndex;
else
break;
LastIndex = -2;
}
EsGridColumn col = new EsGridColumn();
col.Name = "tmpColumn" + i.ToString();
Flag_Fixed = 0;
Flag_Sum = 0;
Flag_NoFilter = 0;
Flag_Hide = 0;
string colName = dt.Columns[i].ColumnName;
if (colName.IndexOf("Last") >= 0)
{
if (LastIndex == -1)
{
LastIndex = i;
continue;
}
colName = colName.Replace("Last", "");
}
if (colName.IndexOf("Fixed") >= 0)
{
Flag_Fixed = 1;
colName = colName.Replace("Fixed", "");
}
if (colName.IndexOf("Hide") >= 0)
{
Flag_Hide = 1;
colName = colName.Replace("Hide", "");
}
if (colName.IndexOf("NoFilter") >= 0)
{
Flag_NoFilter = 1;
colName = colName.Replace("NoFilter", "");
}
if (colName.IndexOf("SumNo") >= 0)
{
Flag_Sum = 1;
Flag_NoFilter = 1;
colName = colName.Replace("SumNo", "");
}
if (colName.IndexOf("Sum") >= 0)
{
Flag_Sum = 1;
colName = colName.Replace("Sum", "");
}
if (colName == "") colName = "无列名";
dt.Columns[i].ColumnName = colName;
col.Caption = colName;
col.FieldName = colName;
if (i == 0 || Flag_Hide == 1)
{
col.Visible = false;
col.VisibleIndex = -1;
}
else
{
col.Visible = true;
col.VisibleIndex = gv.Columns.Count;
col.OptionsColumn.AllowEdit = false;
col.OptionsColumn.AllowFocus = false;
col.OptionsColumn.ReadOnly = true;
if (Flag_Fixed == 1)
{
col.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
}
if (Flag_Sum == 1)
{
col.SummaryItem.FieldName = col.FieldName;
col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
}
else if (i == 1)
{
col.SummaryItem.FieldName = col.FieldName;
col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
}
if (Flag_NoFilter == 1)
{
col.OptionsFilter.AllowFilter = false;
}
}
gv.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { col });
if (LastIndex == -2) { LastIndex = i; break; }
}
gv.OptionsView.ShowFooter = !ShowTotal;
gv.GridControl.DataSource = dt;
gv.BestFitColumns();
}