配置文件
<?xml version="1.0" encoding="utf-8" ?>
<root>
<ShipSettlementQue>
<column headerName="船舶编号" filedName="REQUEST_NO" isHide="1" width=""/>
<column headerName="船舶编号" filedName="REQUEST_NO_DISPLAY" isHide="0" width=""/>
<column headerName="中文船名" filedName="CH_NAME" isHide="0" width=""/>
<column headerName="协议公司" filedName="CGS" isHide="0" width=""/>
<column headerName="代理公司" filedName="SNAME" isHide="0" width=""/>
<column headerName="呼号" filedName="CALL_NO" isHide="0" width=""/>
<column headerName="引领时间" filedName="YLSJ" isHide="0" width=""/>
<column headerName="净吨" filedName="WEIGHT" isHide="0" width=""/>
<column headerName="总吨" filedName="GROSS_WEIGHT" isHide="0" width=""/>
<column headerName="始发地" filedName="SFDBW" isHide="0" width=""/>
<column headerName="目的地" filedName="MDDBW" isHide="0" width=""/>
<column headerName="收费里程" filedName="SFLC" isHide="0" width=""/>
<column headerName="收费总额" filedName="HJJE" isHide="0" width=""/>
</ShipSettlementQue>
</root>
public class DataGridColumnInfo
{
public DataGridColumnInfo()
{
}
/// <summary>
/// 列头名称
/// </summary>
public string HeaderName { get; set; }
/// <summary>
/// 邦定的对象名称
/// </summary>
public string FiledName { get; set; }
/// <summary>
/// 列宽
/// </summary>
public int ColumnWidth { get; set; }
/// <summary>
/// 是否隐藏1:隐藏,0:显示
/// </summary>
public bool IsHide { get; set; }
/// <summary>
/// 是否只读1:只读,0:非只读
/// </summary>
public bool IsRreadOnly { get; set; }
}
初始化列信息
public void InitColumnHeader(string fileName, string nodeName, GridView view, bool isClear = false)
{
List<DataGridColumnInfo> list = GetColumnDesign(fileName, nodeName);
InitColumnHeader(list, view, isClear);
}
public List<DataGridColumnInfo> GetColumnDesign(string fileName, string nodeName)
{
List<DataGridColumnInfo> list = new List<DataGridColumnInfo>();
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DomesticGridColumnConfig", fileName + ".xml");
if (File.Exists(filePath) == false)
return list;
XmlDocument doc = new XmlDocument();
try
{
doc.Load(filePath);
XmlNodeList nodeList = doc.SelectNodes("root/" + nodeName + "/column");
for (int i = 0; i < nodeList.Count; i++)
{
DataGridColumnInfo item = new DataGridColumnInfo();
item.HeaderName = nodeList[i].Attributes["headerName"] == null ? "" : nodeList[i].Attributes["headerName"].InnerText;
item.FiledName = nodeList[i].Attributes["filedName"] == null ? "" : nodeList[i].Attributes["filedName"].InnerText;
item.ColumnWidth = nodeList[i].Attributes["width"] == null ? 0 : nodeList[i].Attributes["width"].InnerText == "" ? 0 : Convert.ToInt32(nodeList[i].Attributes["width"].InnerText);
item.IsHide = nodeList[i].Attributes["isHide"] == null ? false : nodeList[i].Attributes["isHide"].InnerText == "1" ? true : false;
item.IsRreadOnly = nodeList[i].Attributes["isReadOnly"] == null ? false : nodeList[i].Attributes["isReadOnly"].InnerText == "1" ? true : false;
list.Add(item);
}
}
catch (Exception ex)
{
// Log.logger.Error(string.Format("Failed in {0},{1},{2}.", "CApplicationInfo", ex.Message, ex.StackTrace));
}
return list;
}
public void InitColumnHeader(List<DataGridColumnInfo> list, GridView view, bool isClear = false)
{
view.BeginUpdate(); //开始视图的编辑,防止触发其他事件
view.BeginDataUpdate(); //开始数据的编辑
if (isClear == true)
view.Columns.Clear();
for (int i = 0; i < list.Count; i++)
{
DataGridColumnInfo item = list[i];
GridColumn colInfo = new GridColumn();
colInfo.Visible = !item.IsHide;
colInfo.Name = item.FiledName;
colInfo.Caption = item.HeaderName;
colInfo.FieldName = item.FiledName;
colInfo.OptionsColumn.ReadOnly = item.IsRreadOnly;
view.Columns.Add(colInfo);
if (item.ColumnWidth > 0)
colInfo.Width = item.ColumnWidth;
colInfo.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
//colInfo.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
}
view.EndDataUpdate();//结束数据的编辑
view.EndUpdate(); //结束视图的编辑
}
我们也可以直接在gridcontrol控件中设置列信息,但是对于经常需要改动的话,不是太方便,通过配置文件配置,我们不需要修改程序就可以完成。