public class ClassDataGridRefresh
{
public ClassDataGridRefresh()
{
}
#region DataGrid刷新
/// <summary>
/// DataGrid刷新
/// </summary>
/// <param name="DG">DataGrid控件</param>
/// <param name="DV">DataView数据源</param>
/// <param name="m_strWidth">各列列宽,使用分号“;”分隔</param>
/// <param name="m_strHidden">隐藏列,使用分号“;”分隔</param>
public static void RefreshDataGrid(System.Windows.Forms.DataGrid DG, System.Data.DataView DV, string m_strWidth, string m_strHidden)
{
try
{
RefreshDataGrid(DG, DV, m_strWidth, m_strHidden, true);
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message.ToString(),"DataGrid刷新错误");
}
}
/// <summary>
/// DataGrid刷新
/// </summary>
/// <param name="DG">DataGrid控件</param>
/// <param name="DV">DataView数据源</param>
/// <param name="m_strWidth">各列列宽,使用分号“;”分隔</param>
/// <param name="m_strHidden">隐藏列,使用分号“;”分隔</param>
/// <param name="m_blnIsReDraw">是否每次都重新刷新列头</param>
public static void RefreshDataGrid(System.Windows.Forms.DataGrid DG, System.Data.DataView DV, string m_strWidth, string m_strHidden, bool m_blnIsReDraw)
{
try
{
int i;
int numCols;
System.Windows.Forms.DataGridTableStyle ts = new System.Windows.Forms.DataGridTableStyle(); //就是它决定了datagrid是什么样的
System.Windows.Forms.DataGridTextBoxColumn aColumnTextColumn; //要重写的东东
System.Windows.Forms.DataGridBoolColumn aColumnBoolColumn; //要重写的东东
string[] ColumnWidths = m_strWidth.Split(';');
string[] strHiddens = m_strHidden.Split(';');
numCols = DV.Table.Columns.Count;
if (m_blnIsReDraw == true)
{
foreach(string strHiddenTemp in strHiddens)
{
if (strHiddenTemp.Length > 0)
{
DV.Table.Columns[strHiddenTemp].ColumnMapping = System.Data.MappingType.Hidden;
}
}
DG.Font = new System.Drawing.Font("宋体",10,System.Drawing.FontStyle.Regular);
}
DG.DataSource = DV;
if (m_blnIsReDraw == true)
{
ts.MappingName = DV.Table.TableName;
for(i=0;i<numCols;i++) // 重绘所有的列
{
switch (DV.Table.Columns[i].DataType.Name)
{
case "Boolean": //bool Column
aColumnBoolColumn =new System.Windows.Forms.DataGridBoolColumn();
//要更改列头名,请改下句的HeaderText值
aColumnBoolColumn.HeaderText = DV.Table.Columns[i].ColumnName;
aColumnBoolColumn.MappingName = DV.Table.Columns[i].ColumnName;
if (ColumnWidths.Length > i)
{
aColumnBoolColumn.Width = (int) (Convert.ToDouble(ColumnWidths[i]) * 50);
}
ts.GridColumnStyles.Add(aColumnBoolColumn); //增加一种自定义的column风格
break;
default: //Default (Text) Column
aColumnTextColumn =new System.Windows.Forms.DataGridTextBoxColumn();
//要更改列头名,请改下句的HeaderText值
aColumnTextColumn.HeaderText = DV.Table.Columns[i].ColumnName;
aColumnTextColumn.MappingName = DV.Table.Columns[i].ColumnName;
if (ColumnWidths.Length > i)
{
aColumnTextColumn.Width = (int) (Convert.ToDouble(ColumnWidths[i]) * 50);
}
ts.GridColumnStyles.Add(aColumnTextColumn); //增加一种自定义的column风格
break;
}
}
DG.TableStyles.Clear();
DG.TableStyles.Add(ts);
}
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message.ToString(),"DataGrid刷新错误");
}
}
#endregion
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/09/09/1563035.html,如需转载请自行联系原作者