设置datagridview的一些row和cell的方法,例如产生row,将值插入到一个row中并返回#region 设置datagridview的一些row和cell的方法,例如产生row,将值插入到一个row中并返回
/**//// <summary>
/// 返回一个实例化之后的DataGridViewRow对象
/// </summary>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <returns>初始化好了的datagridviewrow对象</returns>
public static DataGridViewRow GenerateDgvCommonRow(int columnCount,
AddSpecialColumnType firstColumnType)
{
DataGridViewRow tmpRow = new DataGridViewRow();
for (int i = 0; i < columnCount; i++)
{
if (i > 0)//第二列开始加入的都是文本框
{
tmpRow.Cells.Add(new DataGridViewTextBoxCell());
}
else//第一列根据用户的输入参数来决定,默认是文本框
{
switch (firstColumnType)
{
case AddSpecialColumnType.CheckBox:
//tmpRow.Cells.Add(new DataGridViewCheckBoxColumn());
DataGridViewCheckBoxCell tmpCell = new DataGridViewCheckBoxCell();
tmpCell.Value = false;
//tmpRow.Cells.Add(new DataGridViewCheckBoxCell());
tmpRow.Cells.Add(tmpCell);
break;
case AddSpecialColumnType.ComboBox:
tmpRow.Cells.Add(new DataGridViewComboBoxCell());
//tmpRow.Cells.Add(new DataGridViewComboBoxColumn());
break;
case AddSpecialColumnType.TextBox:
tmpRow.Cells.Add(new DataGridViewTextBoxCell());
//tmpRow.Cells.Add(new DataGridViewTextBoxColumn());
break;
default:
tmpRow.Cells.Add(new DataGridViewTextBoxCell());
//tmpRow.Cells.Add(new DataGridViewTextBoxColumn());
break;
}
}
}
return tmpRow;
}
/**//// <summary>
/// 点击全选的时候调用的方法
/// </summary>
/// <param name="HandledDgView">需要处理的DataGridView</param>
/// <param name="checkBosPos">checkbox的位置</param>
public static void GetAllCheckBokChecked(ref DataGridView HandledDgView,
int checkBosPos)
{
foreach (DataGridViewRow row in HandledDgView.Rows)
{
if ((row.Cells[checkBosPos].GetType().Equals(typeof(DataGridViewCheckBoxCell))))
{
DataGridViewCheckBoxCell CheckBoxCol =
(DataGridViewCheckBoxCell)row.Cells[checkBosPos];
if (CheckBoxCol.Value == null)
{
CheckBoxCol.Value = true;
}
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
{
CheckBoxCol.Value = true;
}
//else
//{
// CheckBoxCol.Value = false;
//}
}
}
}
/**//// <summary>
/// 点击反向全选的时候调用的方法
/// </summary>
/// <param name="HandledDgView">需要处理的DataGridView</param>
/// <param name="checkBosPos">checkbox的位置</param>
public static void GetAllCheckBokReverseChecked(ref DataGridView HandledDgView,
int checkBosPos)
{
foreach (DataGridViewRow row in HandledDgView.Rows)
{
if (row.Cells[checkBosPos].GetType().Equals(typeof(DataGridViewCheckBoxCell)))
{
DataGridViewCheckBoxCell CheckBoxCol =
(DataGridViewCheckBoxCell)row.Cells[checkBosPos];
if (CheckBoxCol.Value == null)
{
CheckBoxCol.Value = true;
}
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
{
CheckBoxCol.Value = true;
}
else
{
CheckBoxCol.Value = false;
}
}
}
}
/**//// <summary>
/// 反相全选的方法
/// </summary>
/// <param name="HandledDgView">需要处理的DataGridView</param>
/// <param name="checkBosPos">checkbox的位置</param>
public static void GetAllCheckBokCheckedReverse(ref DataGridView HandledDgView,
int checkBosPos)
{
foreach (DataGridViewRow row in HandledDgView.Rows)
{
DataGridViewCheckBoxCell CheckBoxCol =
(DataGridViewCheckBoxCell)row.Cells[checkBosPos];
if (CheckBoxCol.Value == null)
{
CheckBoxCol.Value = false;
}
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
{
CheckBoxCol.Value = true;
}
else
{
CheckBoxCol.Value = false;
}
}
}
/**//// <summary>
/// 根据传递的参数将一个datagridview中根据需要的值填充好row
/// </summary>
/// <param name="HandledDgView">需要被处理的datagridview</param>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <param name="value">需要插入到列中的值</param>
public static void GenerateDgViewAndInserValueToDgView(ref DataGridView HandledDgView,
int columnCount,
AddSpecialColumnType firstColumnType,
object[] value)
{
//调用本类中的方法产生一个datagridrow
DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
//定义一个是否加入了特殊列的标志
bool IsAddSpecialColumnFlag = false;
for (int i = 0; i < tmpRow.Cells.Count; i++)
{
if (i == 0)//当第一列的时候,如果不是textbox,那么不插入值
{
if (firstColumnType == AddSpecialColumnType.TextBox)
tmpRow.Cells[i].Value = value[i];
else
IsAddSpecialColumnFlag = true;
}
else
{
if (IsAddSpecialColumnFlag)//如果加入了特殊的列,那么取值就是上一次的
tmpRow.Cells[i].Value = value[i - 1];
else
tmpRow.Cells[i].Value = value[i];
}
}
HandledDgView.Rows.Add(tmpRow);
}
/**//// <summary>
/// 根据传递的参数将一个datagridview中根据需要的值填充好row
/// </summary>
/// <param name="HandledDgView">需要被处理的datagridview</param>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <param name="value">需要插入到列中的值</param>
/// <param name="rowColor">此行需要的颜色</param>
public static void GenerateDgViewAndInserValueToDgView(ref DataGridView HandledDgView,
int columnCount,
AddSpecialColumnType firstColumnType,
object[] value, Color rowColor)
{
//调用本类中的方法产生一个datagridrow
DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
//定义一个是否加入了特殊列的标志
bool IsAddSpecialColumnFlag = false;
for (int i = 0; i < tmpRow.Cells.Count; i++)
{
//当第一列的时候,如果不是textbox,那么不插入值
if (i == 0)
{
if (firstColumnType == AddSpecialColumnType.TextBox)
tmpRow.Cells[i].Value = value[i];
else
IsAddSpecialColumnFlag = true;
}
else
{
//如果加入了特殊的列,那么取值就是上一次的
if (IsAddSpecialColumnFlag)
tmpRow.Cells[i].Value = value[i - 1];
else
tmpRow.Cells[i].Value = value[i];
}
}
tmpRow.DefaultCellStyle.BackColor = rowColor;
HandledDgView.Rows.Add(tmpRow);
}
/**//// <summary>
/// 根据传递的参数将一个datagridview中根据需要的值填充好row
/// </summary>
/// <param name="HandledDgView">需要被处理的datagridview</param>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <param name="value">需要插入到列中的值</param>
/// <param name="fontColor">此行字体需要的颜色</param>
public static void GenerateDgViewSetFontColorAndInserValueToDgView(ref DataGridView HandledDgView, int columnCount, AddSpecialColumnType firstColumnType,
object[] value, Color fontColor)
{
//调用本类中的方法产生一个datagridrow
DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
//定义一个是否加入了特殊列的标志
bool IsAddSpecialColumnFlag = false;
for (int i = 0; i < tmpRow.Cells.Count; i++)
{
if (i == 0)//当第一列的时候,如果不是textbox,那么不插入值
{
if (firstColumnType == AddSpecialColumnType.TextBox)
tmpRow.Cells[i].Value = value[i];
else
IsAddSpecialColumnFlag = true;
}
else
{
if (IsAddSpecialColumnFlag)//如果加入了特殊的列,那么取值就是上一次的
tmpRow.Cells[i].Value = value[i - 1];
else
tmpRow.Cells[i].Value = value[i];
}
}
tmpRow.DefaultCellStyle.ForeColor = fontColor;
HandledDgView.Rows.Add(tmpRow);
}
/**//// <summary>
/// 向一个datagridview中添加要显示的值
/// </summary>
/// <param name="handler">要被操作的datagridview控件</param>
/// <param name="value">行的值</param>
public static void InsertValueToDataGridView(ref DataGridView handler,
string[] value)
{
handler.Rows.Add(value);
}
/**//// <summary>
/// 设置datagridview的列不排序
/// </summary>
/// <param name="handler">需要处理的datagridview</param>
public static void SetColumnNotSortable(ref DataGridView handler)
{
foreach (DataGridViewColumn dc in handler.Columns)
{
dc.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
#endregion
设置界面的显示列的一些方法#region 设置界面的显示列的一些方法
/**//// <summary>
/// 将设置好的DataGridViewColumn[]加入到要显示的DataGridView中
/// </summary>
/// <param name="HandledDgView">需要被设置的datagridview</param>
/// <param name="columnInfo">列信息</param>
public static void AddSettedColmnInfoToDataGridView(ref DataGridView HandledDgView,
DataGridViewColumn[] columnInfo)
{
测试用的代码,暂时注释#region 测试用的代码,暂时注释
//DataGridViewTextBoxColumn FlightBeginDate = new DataGridViewTextBoxColumn();
//FlightBeginDate.DataPropertyName = "FlightBeginDate";
//FlightBeginDate.Name = "FlightBeginDate";
//HandledDgView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
//FlightBeginDate});
#endregion
HandledDgView.Columns.AddRange(columnInfo);
}
/**//// <summary>
/// 设置界面显示结果的列名字
/// </summary>
public static void SetDataGridViewColumnHeader(ref DataGridView HandledDgView,
string[] columnNameArray,
string[] columnDescriptionArray)
{
从前写死的做法,现在注释了#region 从前写死的做法,现在注释了
//HandledDgView.Columns["FlightNo"].HeaderText = "航班号";
//HandledDgView.Columns["FlightBeginDate"].HeaderText = "开始日期";
//HandledDgView.Columns["FlightEndDate"].HeaderText = "结束日期";
//HandledDgView.Columns["FlightSession"].HeaderText = "班期";
//HandledDgView.Columns["FlightSegment"].HeaderText = "航段";
//HandledDgView.Columns["AirPlaneType"].HeaderText = "机型";
//HandledDgView.Columns["FlightDepTime"].HeaderText = "起飞时间";
//HandledDgView.Columns["FlightDepSepDay"].HeaderText = "起飞日期间隔";
//HandledDgView.Columns["FlightDestTime"].HeaderText = "到达时间";
//HandledDgView.Columns["FlightDestSepDay"].HeaderText = "到达日期间隔";
//HandledDgView.Columns["FlightMaxWeight"].HeaderText = "最大装货量";
//HandledDgView.Columns["FlightControlWeight"].HeaderText = "控制比例";
//HandledDgView.Columns["FlightOutWeight"].HeaderText = "开放比例";
//HandledDgView.Columns["FlightFdepArea"].HeaderText = "起飞站国别";
//HandledDgView.Columns["FlightFdestArea"].HeaderText = "到达站国别";
//HandledDgView.Columns["FlightRouting"].HeaderText = "航程";
#endregion
for (int i = 0; i < columnNameArray.Length; i++)
{
HandledDgView.Columns[columnNameArray[i]].HeaderText = columnDescriptionArray[i];
}
}
/**//// <summary>
/// 设置界面显示结果的列的显示顺序,提供一个使用列名字作为参数的重载
/// </summary>
/// <param name="HandledDgView">需要被设置的DataGridView</param>
/// <param name="columnNameArray">包含了这个DataGridView需要的列名字的数组</param>
public static void SetDataGridViewColumnHeadSequence(ref DataGridView HandledDgView,
string[] columnNameArray)
{
for (int i = 0; i < columnNameArray.Length; i++)//利用名字数组作循环
{
HandledDgView.Columns[columnNameArray[i]].DisplayIndex = i;
}
从前写死的做法,暂时注释了#region 从前写死的做法,暂时注释了
//HandledDgView.Columns["FlightNo"].DisplayIndex = 0;
//HandledDgView.Columns["FlightBeginDate"].DisplayIndex = 1;
//HandledDgView.Columns["FlightEndDate"].DisplayIndex = 2;
//HandledDgView.Columns["FlightSession"].DisplayIndex = 3;
//HandledDgView.Columns["FlightSegment"].DisplayIndex = 4;
//HandledDgView.Columns["AirPlaneType"].DisplayIndex = 5;
//HandledDgView.Columns["FlightDepTime"].DisplayIndex = 6;
//HandledDgView.Columns["FlightDepSepDay"].DisplayIndex = 7;
//HandledDgView.Columns["FlightDestTime"].DisplayIndex = 8;
//HandledDgView.Columns["FlightDestSepDay"].DisplayIndex = 9;
//HandledDgView.Columns["FlightMaxWeight"].DisplayIndex = 10;
//HandledDgView.Columns["FlightControlWeight"].DisplayIndex = 11;
//HandledDgView.Columns["FlightOutWeight"].DisplayIndex = 12;
//HandledDgView.Columns["FlightFdepArea"].DisplayIndex = 13;
//HandledDgView.Columns["FlightFdestArea"].DisplayIndex = 14;
//HandledDgView.Columns["FlightRouting"].DisplayIndex = 15;
#endregion
}
/**//// <summary>
///返回一个定义了DataGridViewColumn的名字和数据绑定字段的数组
/// 用于DataGridView的列显示
/// </summary>
/// <param name="columnNameArray">列名字的数组</param>
/// <param name="specialColumnIndex">需要特殊设置的列宽的列的索引和宽度</param>
/// <returns>定义好的DataGridViewColumn对象数组</returns>
public static DataGridViewColumn[] SetDgViewColumnProperty(string[] columnNameArray,
System.Collections.Hashtable specialColumnIndex)
{
//然后获得数组的边界,定义返回的DataGridViewColumn的数组范围
int arrCount = columnNameArray.Length;
//定义返回的DataGridViewColumn
DataGridViewColumn[] resultColumn = new DataGridViewColumn[arrCount];
//设置一个是否需要进行特殊列宽度设置的标志
bool SpecialWidthFlag = false;
if (specialColumnIndex == null)
SpecialWidthFlag = false;
else
SpecialWidthFlag = true;
//bool SpecialWidthFlag = specialColumnIndex.Equals(null)?false:true;
//循环数组
for (int i = 0; i < columnNameArray.Length; i++)
{
DataGridViewTextBoxColumn tmpColumn = new DataGridViewTextBoxColumn();
//设置列对应的数据绑定的名字
tmpColumn.DataPropertyName = columnNameArray[i].Trim().ToUpper();
//设置列的名字
tmpColumn.Name = columnNameArray[i].Trim().ToUpper();
if (SpecialWidthFlag)//如果需要特殊设置
{
if (specialColumnIndex.ContainsKey(i))//如果是特殊需要设置的列,设置为自定义的宽度
{
tmpColumn.Width = (int)specialColumnIndex[i];
if ((int)specialColumnIndex[i] == 0)//如果此列的宽度为0,说明不显示
tmpColumn.Visible = false;
}
}
else
tmpColumn.Width = commonWidth;//其余的采用普通宽度
以前写死的,现在修改为使用list进行查找#region 以前写死的,现在修改为使用list进行查找
//if (i==7||i==9||i==10||i==13||i==14)
//{
// tmpColumn.Width = 110;
//}
//else
//{
// tmpColumn.Width = 80;
//}
/**/////判断是否有需要特殊设置的列
//if (specialColumnIndex.Contains(i))//如果是特殊需要设置的列,设置为长宽度
// tmpColumn.Width = 110;
//else
// tmpColumn.Width = commonWidth;//其余的采用普通宽度
#endregion
resultColumn[i] = tmpColumn;
}
//返回结果
return resultColumn;
}
/**//// <summary>
/// 设置界面显示结果的列名字
/// </summary>
public static void SetDataGridViewColumnHeader(ref DataGridView HandledDgView,
string[] columnNameArray,
string[] columnDescriptionArray,
AddSpecialColumnType columnType)
{
从前写死的做法,现在注释了#region 从前写死的做法,现在注释了
//HandledDgView.Columns["FlightNo"].HeaderText = "航班号";
//HandledDgView.Columns["FlightBeginDate"].HeaderText = "开始日期";
//HandledDgView.Columns["FlightEndDate"].HeaderText = "结束日期";
//HandledDgView.Columns["FlightSession"].HeaderText = "班期";
//HandledDgView.Columns["FlightSegment"].HeaderText = "航段";
//HandledDgView.Columns["AirPlaneType"].HeaderText = "机型";
//HandledDgView.Columns["FlightDepTime"].HeaderText = "起飞时间";
//HandledDgView.Columns["FlightDepSepDay"].HeaderText = "起飞日期间隔";
//HandledDgView.Columns["FlightDestTime"].HeaderText = "到达时间";
//HandledDgView.Columns["FlightDestSepDay"].HeaderText = "到达日期间隔";
//HandledDgView.Columns["FlightMaxWeight"].HeaderText = "最大装货量";
//HandledDgView.Columns["FlightControlWeight"].HeaderText = "控制比例";
//HandledDgView.Columns["FlightOutWeight"].HeaderText = "开放比例";
//HandledDgView.Columns["FlightFdepArea"].HeaderText = "起飞站国别";
//HandledDgView.Columns["FlightFdestArea"].HeaderText = "到达站国别";
//HandledDgView.Columns["FlightRouting"].HeaderText = "航程";
#endregion
for (int i = 0; i < columnNameArray.Length; i++)
{
HandledDgView.Columns[columnNameArray[i]].HeaderText = columnDescriptionArray[i];
}
}
/**//// <summary>
/// 设置界面显示结果的列的显示顺序,提供一个使用列名字作为参数的重载
/// </summary>
/// <param name="HandledDgView">需要被设置的DataGridView</param>
/// <param name="columnNameArray">包含了这个DataGridView需要的列名字的数组</param>
/// <param name="columnType">需要加入的特殊列的类型</param>
public static void SetDataGridViewColumnHeadSequence(ref DataGridView HandledDgView,
string[] columnNameArray,
AddSpecialColumnType columnType)
{
for (int i = 0; i < columnNameArray.Length; i++)//利用名字数组作循环
{
HandledDgView.Columns[columnNameArray[i]].DisplayIndex = i + 1;
}
从前写死的做法,暂时注释了#region 从前写死的做法,暂时注释了
//HandledDgView.Columns["FlightNo"].DisplayIndex = 0;
//HandledDgView.Columns["FlightBeginDate"].DisplayIndex = 1;
//HandledDgView.Columns["FlightEndDate"].DisplayIndex = 2;
//HandledDgView.Columns["FlightSession"].DisplayIndex = 3;
//HandledDgView.Columns["FlightSegment"].DisplayIndex = 4;
//HandledDgView.Columns["AirPlaneType"].DisplayIndex = 5;
//HandledDgView.Columns["FlightDepTime"].DisplayIndex = 6;
//HandledDgView.Columns["FlightDepSepDay"].DisplayIndex = 7;
//HandledDgView.Columns["FlightDestTime"].DisplayIndex = 8;
//HandledDgView.Columns["FlightDestSepDay"].DisplayIndex = 9;
//HandledDgView.Columns["FlightMaxWeight"].DisplayIndex = 10;
//HandledDgView.Columns["FlightControlWeight"].DisplayIndex = 11;
//HandledDgView.Columns["FlightOutWeight"].DisplayIndex = 12;
//HandledDgView.Columns["FlightFdepArea"].DisplayIndex = 13;
//HandledDgView.Columns["FlightFdestArea"].DisplayIndex = 14;
//HandledDgView.Columns["FlightRouting"].DisplayIndex = 15;
#endregion
}
/**//// <summary>
///返回一个定义了DataGridViewColumn的名字和数据绑定字段的数组
/// 用于DataGridView的列显示
/// </summary>
/// <param name="columnNameArray">列名字的数组</param>
/// <param name="specialColumnIndex">需要特殊设置的列宽的列的索引和宽度</param>
/// <param name="columnType">加入的特殊列的类型</param>
/// <returns>定义好的DataGridViewColumn对象数组</returns>
public static DataGridViewColumn[] SetDgViewColumnProperty(string[] columnNameArray,
System.Collections.Hashtable specialColumnIndex,
AddSpecialColumnType columnType)
{
//然后获得数组的边界,定义返回的DataGridViewColumn的数组范围
int arrCount = columnNameArray.Length;
//定义返回的DataGridViewColumn
DataGridViewColumn[] resultColumn = new DataGridViewColumn[arrCount + 1];
//设置一个是否需要进行特殊列宽度设置的标志
bool SpecialWidthFlag = false;
if (specialColumnIndex == null)
SpecialWidthFlag = false;
else
SpecialWidthFlag = true;
resultColumn[0] = GenerateSpecialColumn(columnType);
//bool SpecialWidthFlag = specialColumnIndex.Equals(null)?false:true;
//循环数组
for (int i = 0; i < columnNameArray.Length; i++)
{
DataGridViewTextBoxColumn tmpColumn = new DataGridViewTextBoxColumn();
//设置列对应的数据绑定的名字
tmpColumn.DataPropertyName = columnNameArray[i].Trim().ToUpper();
//设置列的名字
tmpColumn.Name = columnNameArray[i].Trim().ToUpper();
if (SpecialWidthFlag)//如果需要特殊设置
{
if (specialColumnIndex.ContainsKey(i))//如果是特殊需要设置的列,设置为自定义的宽度
{
tmpColumn.Width = (int)specialColumnIndex[i];
}
}
else
tmpColumn.Width = commonWidth;//其余的采用普通宽度
以前写死的,现在修改为使用list进行查找#region 以前写死的,现在修改为使用list进行查找
//if (i==7||i==9||i==10||i==13||i==14)
//{
// tmpColumn.Width = 110;
//}
//else
//{
// tmpColumn.Width = 80;
//}
/**/////判断是否有需要特殊设置的列
//if (specialColumnIndex.Contains(i))//如果是特殊需要设置的列,设置为长宽度
// tmpColumn.Width = 110;
//else
// tmpColumn.Width = commonWidth;//其余的采用普通宽度
#endregion
resultColumn[i + 1] = tmpColumn;
}
//返回结果
return resultColumn;
}
/**//// <summary>
/// 根据需要加入的特殊列的类型,产生一个特殊列
/// </summary>
/// <param name="columnType">要加入的特殊列的类型</param>
/// <returns>返回一个构造好的特殊列</returns>
private static DataGridViewColumn GenerateSpecialColumn(AddSpecialColumnType columnType)
{
DataGridViewColumn specialColumn = new DataGridViewColumn();
switch (columnType)
{
case AddSpecialColumnType.CheckBox:
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Frozen = true;
checkColumn.Width = 30;
checkColumn.TrueValue = "true";
checkColumn.FalseValue = "false";
specialColumn = checkColumn;
break;
case AddSpecialColumnType.ComboBox:
DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
comboColumn.Frozen = true;
comboColumn.Width = 30;
specialColumn = comboColumn;
break;
default:
DataGridViewCheckBoxColumn defaultCheckColumn = new DataGridViewCheckBoxColumn();
defaultCheckColumn.Frozen = true;
defaultCheckColumn.Width = 30;
defaultCheckColumn.TrueValue = "true";
defaultCheckColumn.FalseValue = "false";
specialColumn = defaultCheckColumn;
break;
}
return specialColumn;
}
/**//// <summary>
/// 设置需要特别显示宽度的列的index和需要设置的宽度
/// </summary>
/// <param name="ColumnIndex">包含了需要特殊设置的列的索引</param>
/// <param name="ColumnWidth">包含了需要特殊设置的列的宽度</param>
/// <returns>需要特殊设置列的索引和设置的宽度</returns>
public static System.Collections.Hashtable WidthEnlargeColumn(int[] ColumnIndex,
int[] ColumnWidth)
{
以前的做法,现在注释了#region 以前的做法,现在注释了
//int[] ColumnIndex = { 7, 9, 10, 13, 14 };
//List<int> specialColumnIndex = new List<int>();
//specialColumnIndex.AddRange(ColumnIndex);
//return (specialColumnIndex);
#endregion
System.Collections.Hashtable resultTable = new System.Collections.Hashtable();
if (ColumnIndex.Length != ColumnWidth.Length)//如果列索引与列的宽度的数组中包含的数量不相等,返回null
{
//return new System.Collections.Hashtable();
return resultTable;
}
//循环操作,将索引和对应的宽度加入到返回的Hashtable中
for (int i = 0; i < ColumnIndex.Length; i++)
{
resultTable.Add(ColumnIndex[i], ColumnWidth[i]);
}
return resultTable;
}
#endregion
一些关于文本框输入的可输入性的共用方法#region 一些关于文本框输入的可输入性的共用方法
/**//// <summary>
/// 只输入字母的判断
/// ASC://48-57(数字),65-90(字母)
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyLetterAndInteger(char e)
{
if ((e >= 65 && e <= 90) ||
(e >= 48 && e <= 57) || e == 8 ||
(e >= 97 && e <= 122) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入字母和数字还有-/的判断
/// ASC://48-57(数字),65-90(字母)
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyLetterAndIntegerAndSpeChar(char e)
{
if ((e >= 65 && e <= 90) ||
(e >= 48 && e <= 57) || e == 8 || e == 45 || e == 47 ||
(e >= 97 && e <= 122) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入字母的判断
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyLetter(char e)
{
if ((e >= 65 && e <= 90) ||
e == 8 ||
(e >= 97 && e <= 122) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入数字和小数点的判断
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyNumberAndDot(char e)
{
if ((e >= 48 && e <= 57) || (e == 46) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入数字的判断
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyNumber(char e)
{
if ((e >= 48 && e <= 57) || (e == (char)Keys.Back))
return true;
else
return false;
}
#endregion
/**//// <summary>
/// 返回一个实例化之后的DataGridViewRow对象
/// </summary>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <returns>初始化好了的datagridviewrow对象</returns>
public static DataGridViewRow GenerateDgvCommonRow(int columnCount,
AddSpecialColumnType firstColumnType)
{
DataGridViewRow tmpRow = new DataGridViewRow();
for (int i = 0; i < columnCount; i++)
{
if (i > 0)//第二列开始加入的都是文本框
{
tmpRow.Cells.Add(new DataGridViewTextBoxCell());
}
else//第一列根据用户的输入参数来决定,默认是文本框
{
switch (firstColumnType)
{
case AddSpecialColumnType.CheckBox:
//tmpRow.Cells.Add(new DataGridViewCheckBoxColumn());
DataGridViewCheckBoxCell tmpCell = new DataGridViewCheckBoxCell();
tmpCell.Value = false;
//tmpRow.Cells.Add(new DataGridViewCheckBoxCell());
tmpRow.Cells.Add(tmpCell);
break;
case AddSpecialColumnType.ComboBox:
tmpRow.Cells.Add(new DataGridViewComboBoxCell());
//tmpRow.Cells.Add(new DataGridViewComboBoxColumn());
break;
case AddSpecialColumnType.TextBox:
tmpRow.Cells.Add(new DataGridViewTextBoxCell());
//tmpRow.Cells.Add(new DataGridViewTextBoxColumn());
break;
default:
tmpRow.Cells.Add(new DataGridViewTextBoxCell());
//tmpRow.Cells.Add(new DataGridViewTextBoxColumn());
break;
}
}
}
return tmpRow;
}
/**//// <summary>
/// 点击全选的时候调用的方法
/// </summary>
/// <param name="HandledDgView">需要处理的DataGridView</param>
/// <param name="checkBosPos">checkbox的位置</param>
public static void GetAllCheckBokChecked(ref DataGridView HandledDgView,
int checkBosPos)
{
foreach (DataGridViewRow row in HandledDgView.Rows)
{
if ((row.Cells[checkBosPos].GetType().Equals(typeof(DataGridViewCheckBoxCell))))
{
DataGridViewCheckBoxCell CheckBoxCol =
(DataGridViewCheckBoxCell)row.Cells[checkBosPos];
if (CheckBoxCol.Value == null)
{
CheckBoxCol.Value = true;
}
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
{
CheckBoxCol.Value = true;
}
//else
//{
// CheckBoxCol.Value = false;
//}
}
}
}
/**//// <summary>
/// 点击反向全选的时候调用的方法
/// </summary>
/// <param name="HandledDgView">需要处理的DataGridView</param>
/// <param name="checkBosPos">checkbox的位置</param>
public static void GetAllCheckBokReverseChecked(ref DataGridView HandledDgView,
int checkBosPos)
{
foreach (DataGridViewRow row in HandledDgView.Rows)
{
if (row.Cells[checkBosPos].GetType().Equals(typeof(DataGridViewCheckBoxCell)))
{
DataGridViewCheckBoxCell CheckBoxCol =
(DataGridViewCheckBoxCell)row.Cells[checkBosPos];
if (CheckBoxCol.Value == null)
{
CheckBoxCol.Value = true;
}
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
{
CheckBoxCol.Value = true;
}
else
{
CheckBoxCol.Value = false;
}
}
}
}
/**//// <summary>
/// 反相全选的方法
/// </summary>
/// <param name="HandledDgView">需要处理的DataGridView</param>
/// <param name="checkBosPos">checkbox的位置</param>
public static void GetAllCheckBokCheckedReverse(ref DataGridView HandledDgView,
int checkBosPos)
{
foreach (DataGridViewRow row in HandledDgView.Rows)
{
DataGridViewCheckBoxCell CheckBoxCol =
(DataGridViewCheckBoxCell)row.Cells[checkBosPos];
if (CheckBoxCol.Value == null)
{
CheckBoxCol.Value = false;
}
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
{
CheckBoxCol.Value = true;
}
else
{
CheckBoxCol.Value = false;
}
}
}
/**//// <summary>
/// 根据传递的参数将一个datagridview中根据需要的值填充好row
/// </summary>
/// <param name="HandledDgView">需要被处理的datagridview</param>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <param name="value">需要插入到列中的值</param>
public static void GenerateDgViewAndInserValueToDgView(ref DataGridView HandledDgView,
int columnCount,
AddSpecialColumnType firstColumnType,
object[] value)
{
//调用本类中的方法产生一个datagridrow
DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
//定义一个是否加入了特殊列的标志
bool IsAddSpecialColumnFlag = false;
for (int i = 0; i < tmpRow.Cells.Count; i++)
{
if (i == 0)//当第一列的时候,如果不是textbox,那么不插入值
{
if (firstColumnType == AddSpecialColumnType.TextBox)
tmpRow.Cells[i].Value = value[i];
else
IsAddSpecialColumnFlag = true;
}
else
{
if (IsAddSpecialColumnFlag)//如果加入了特殊的列,那么取值就是上一次的
tmpRow.Cells[i].Value = value[i - 1];
else
tmpRow.Cells[i].Value = value[i];
}
}
HandledDgView.Rows.Add(tmpRow);
}
/**//// <summary>
/// 根据传递的参数将一个datagridview中根据需要的值填充好row
/// </summary>
/// <param name="HandledDgView">需要被处理的datagridview</param>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <param name="value">需要插入到列中的值</param>
/// <param name="rowColor">此行需要的颜色</param>
public static void GenerateDgViewAndInserValueToDgView(ref DataGridView HandledDgView,
int columnCount,
AddSpecialColumnType firstColumnType,
object[] value, Color rowColor)
{
//调用本类中的方法产生一个datagridrow
DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
//定义一个是否加入了特殊列的标志
bool IsAddSpecialColumnFlag = false;
for (int i = 0; i < tmpRow.Cells.Count; i++)
{
//当第一列的时候,如果不是textbox,那么不插入值
if (i == 0)
{
if (firstColumnType == AddSpecialColumnType.TextBox)
tmpRow.Cells[i].Value = value[i];
else
IsAddSpecialColumnFlag = true;
}
else
{
//如果加入了特殊的列,那么取值就是上一次的
if (IsAddSpecialColumnFlag)
tmpRow.Cells[i].Value = value[i - 1];
else
tmpRow.Cells[i].Value = value[i];
}
}
tmpRow.DefaultCellStyle.BackColor = rowColor;
HandledDgView.Rows.Add(tmpRow);
}
/**//// <summary>
/// 根据传递的参数将一个datagridview中根据需要的值填充好row
/// </summary>
/// <param name="HandledDgView">需要被处理的datagridview</param>
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
/// <param name="firstColumnType">第一个列的类型是什么</param>
/// <param name="value">需要插入到列中的值</param>
/// <param name="fontColor">此行字体需要的颜色</param>
public static void GenerateDgViewSetFontColorAndInserValueToDgView(ref DataGridView HandledDgView, int columnCount, AddSpecialColumnType firstColumnType,
object[] value, Color fontColor)
{
//调用本类中的方法产生一个datagridrow
DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
//定义一个是否加入了特殊列的标志
bool IsAddSpecialColumnFlag = false;
for (int i = 0; i < tmpRow.Cells.Count; i++)
{
if (i == 0)//当第一列的时候,如果不是textbox,那么不插入值
{
if (firstColumnType == AddSpecialColumnType.TextBox)
tmpRow.Cells[i].Value = value[i];
else
IsAddSpecialColumnFlag = true;
}
else
{
if (IsAddSpecialColumnFlag)//如果加入了特殊的列,那么取值就是上一次的
tmpRow.Cells[i].Value = value[i - 1];
else
tmpRow.Cells[i].Value = value[i];
}
}
tmpRow.DefaultCellStyle.ForeColor = fontColor;
HandledDgView.Rows.Add(tmpRow);
}
/**//// <summary>
/// 向一个datagridview中添加要显示的值
/// </summary>
/// <param name="handler">要被操作的datagridview控件</param>
/// <param name="value">行的值</param>
public static void InsertValueToDataGridView(ref DataGridView handler,
string[] value)
{
handler.Rows.Add(value);
}
/**//// <summary>
/// 设置datagridview的列不排序
/// </summary>
/// <param name="handler">需要处理的datagridview</param>
public static void SetColumnNotSortable(ref DataGridView handler)
{
foreach (DataGridViewColumn dc in handler.Columns)
{
dc.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
#endregion
设置界面的显示列的一些方法#region 设置界面的显示列的一些方法
/**//// <summary>
/// 将设置好的DataGridViewColumn[]加入到要显示的DataGridView中
/// </summary>
/// <param name="HandledDgView">需要被设置的datagridview</param>
/// <param name="columnInfo">列信息</param>
public static void AddSettedColmnInfoToDataGridView(ref DataGridView HandledDgView,
DataGridViewColumn[] columnInfo)
{
测试用的代码,暂时注释#region 测试用的代码,暂时注释
//DataGridViewTextBoxColumn FlightBeginDate = new DataGridViewTextBoxColumn();
//FlightBeginDate.DataPropertyName = "FlightBeginDate";
//FlightBeginDate.Name = "FlightBeginDate";
//HandledDgView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
//FlightBeginDate});
#endregion
HandledDgView.Columns.AddRange(columnInfo);
}
/**//// <summary>
/// 设置界面显示结果的列名字
/// </summary>
public static void SetDataGridViewColumnHeader(ref DataGridView HandledDgView,
string[] columnNameArray,
string[] columnDescriptionArray)
{
从前写死的做法,现在注释了#region 从前写死的做法,现在注释了
//HandledDgView.Columns["FlightNo"].HeaderText = "航班号";
//HandledDgView.Columns["FlightBeginDate"].HeaderText = "开始日期";
//HandledDgView.Columns["FlightEndDate"].HeaderText = "结束日期";
//HandledDgView.Columns["FlightSession"].HeaderText = "班期";
//HandledDgView.Columns["FlightSegment"].HeaderText = "航段";
//HandledDgView.Columns["AirPlaneType"].HeaderText = "机型";
//HandledDgView.Columns["FlightDepTime"].HeaderText = "起飞时间";
//HandledDgView.Columns["FlightDepSepDay"].HeaderText = "起飞日期间隔";
//HandledDgView.Columns["FlightDestTime"].HeaderText = "到达时间";
//HandledDgView.Columns["FlightDestSepDay"].HeaderText = "到达日期间隔";
//HandledDgView.Columns["FlightMaxWeight"].HeaderText = "最大装货量";
//HandledDgView.Columns["FlightControlWeight"].HeaderText = "控制比例";
//HandledDgView.Columns["FlightOutWeight"].HeaderText = "开放比例";
//HandledDgView.Columns["FlightFdepArea"].HeaderText = "起飞站国别";
//HandledDgView.Columns["FlightFdestArea"].HeaderText = "到达站国别";
//HandledDgView.Columns["FlightRouting"].HeaderText = "航程";
#endregion
for (int i = 0; i < columnNameArray.Length; i++)
{
HandledDgView.Columns[columnNameArray[i]].HeaderText = columnDescriptionArray[i];
}
}
/**//// <summary>
/// 设置界面显示结果的列的显示顺序,提供一个使用列名字作为参数的重载
/// </summary>
/// <param name="HandledDgView">需要被设置的DataGridView</param>
/// <param name="columnNameArray">包含了这个DataGridView需要的列名字的数组</param>
public static void SetDataGridViewColumnHeadSequence(ref DataGridView HandledDgView,
string[] columnNameArray)
{
for (int i = 0; i < columnNameArray.Length; i++)//利用名字数组作循环
{
HandledDgView.Columns[columnNameArray[i]].DisplayIndex = i;
}
从前写死的做法,暂时注释了#region 从前写死的做法,暂时注释了
//HandledDgView.Columns["FlightNo"].DisplayIndex = 0;
//HandledDgView.Columns["FlightBeginDate"].DisplayIndex = 1;
//HandledDgView.Columns["FlightEndDate"].DisplayIndex = 2;
//HandledDgView.Columns["FlightSession"].DisplayIndex = 3;
//HandledDgView.Columns["FlightSegment"].DisplayIndex = 4;
//HandledDgView.Columns["AirPlaneType"].DisplayIndex = 5;
//HandledDgView.Columns["FlightDepTime"].DisplayIndex = 6;
//HandledDgView.Columns["FlightDepSepDay"].DisplayIndex = 7;
//HandledDgView.Columns["FlightDestTime"].DisplayIndex = 8;
//HandledDgView.Columns["FlightDestSepDay"].DisplayIndex = 9;
//HandledDgView.Columns["FlightMaxWeight"].DisplayIndex = 10;
//HandledDgView.Columns["FlightControlWeight"].DisplayIndex = 11;
//HandledDgView.Columns["FlightOutWeight"].DisplayIndex = 12;
//HandledDgView.Columns["FlightFdepArea"].DisplayIndex = 13;
//HandledDgView.Columns["FlightFdestArea"].DisplayIndex = 14;
//HandledDgView.Columns["FlightRouting"].DisplayIndex = 15;
#endregion
}
/**//// <summary>
///返回一个定义了DataGridViewColumn的名字和数据绑定字段的数组
/// 用于DataGridView的列显示
/// </summary>
/// <param name="columnNameArray">列名字的数组</param>
/// <param name="specialColumnIndex">需要特殊设置的列宽的列的索引和宽度</param>
/// <returns>定义好的DataGridViewColumn对象数组</returns>
public static DataGridViewColumn[] SetDgViewColumnProperty(string[] columnNameArray,
System.Collections.Hashtable specialColumnIndex)
{
//然后获得数组的边界,定义返回的DataGridViewColumn的数组范围
int arrCount = columnNameArray.Length;
//定义返回的DataGridViewColumn
DataGridViewColumn[] resultColumn = new DataGridViewColumn[arrCount];
//设置一个是否需要进行特殊列宽度设置的标志
bool SpecialWidthFlag = false;
if (specialColumnIndex == null)
SpecialWidthFlag = false;
else
SpecialWidthFlag = true;
//bool SpecialWidthFlag = specialColumnIndex.Equals(null)?false:true;
//循环数组
for (int i = 0; i < columnNameArray.Length; i++)
{
DataGridViewTextBoxColumn tmpColumn = new DataGridViewTextBoxColumn();
//设置列对应的数据绑定的名字
tmpColumn.DataPropertyName = columnNameArray[i].Trim().ToUpper();
//设置列的名字
tmpColumn.Name = columnNameArray[i].Trim().ToUpper();
if (SpecialWidthFlag)//如果需要特殊设置
{
if (specialColumnIndex.ContainsKey(i))//如果是特殊需要设置的列,设置为自定义的宽度
{
tmpColumn.Width = (int)specialColumnIndex[i];
if ((int)specialColumnIndex[i] == 0)//如果此列的宽度为0,说明不显示
tmpColumn.Visible = false;
}
}
else
tmpColumn.Width = commonWidth;//其余的采用普通宽度
以前写死的,现在修改为使用list进行查找#region 以前写死的,现在修改为使用list进行查找
//if (i==7||i==9||i==10||i==13||i==14)
//{
// tmpColumn.Width = 110;
//}
//else
//{
// tmpColumn.Width = 80;
//}
/**/////判断是否有需要特殊设置的列
//if (specialColumnIndex.Contains(i))//如果是特殊需要设置的列,设置为长宽度
// tmpColumn.Width = 110;
//else
// tmpColumn.Width = commonWidth;//其余的采用普通宽度
#endregion
resultColumn[i] = tmpColumn;
}
//返回结果
return resultColumn;
}
/**//// <summary>
/// 设置界面显示结果的列名字
/// </summary>
public static void SetDataGridViewColumnHeader(ref DataGridView HandledDgView,
string[] columnNameArray,
string[] columnDescriptionArray,
AddSpecialColumnType columnType)
{
从前写死的做法,现在注释了#region 从前写死的做法,现在注释了
//HandledDgView.Columns["FlightNo"].HeaderText = "航班号";
//HandledDgView.Columns["FlightBeginDate"].HeaderText = "开始日期";
//HandledDgView.Columns["FlightEndDate"].HeaderText = "结束日期";
//HandledDgView.Columns["FlightSession"].HeaderText = "班期";
//HandledDgView.Columns["FlightSegment"].HeaderText = "航段";
//HandledDgView.Columns["AirPlaneType"].HeaderText = "机型";
//HandledDgView.Columns["FlightDepTime"].HeaderText = "起飞时间";
//HandledDgView.Columns["FlightDepSepDay"].HeaderText = "起飞日期间隔";
//HandledDgView.Columns["FlightDestTime"].HeaderText = "到达时间";
//HandledDgView.Columns["FlightDestSepDay"].HeaderText = "到达日期间隔";
//HandledDgView.Columns["FlightMaxWeight"].HeaderText = "最大装货量";
//HandledDgView.Columns["FlightControlWeight"].HeaderText = "控制比例";
//HandledDgView.Columns["FlightOutWeight"].HeaderText = "开放比例";
//HandledDgView.Columns["FlightFdepArea"].HeaderText = "起飞站国别";
//HandledDgView.Columns["FlightFdestArea"].HeaderText = "到达站国别";
//HandledDgView.Columns["FlightRouting"].HeaderText = "航程";
#endregion
for (int i = 0; i < columnNameArray.Length; i++)
{
HandledDgView.Columns[columnNameArray[i]].HeaderText = columnDescriptionArray[i];
}
}
/**//// <summary>
/// 设置界面显示结果的列的显示顺序,提供一个使用列名字作为参数的重载
/// </summary>
/// <param name="HandledDgView">需要被设置的DataGridView</param>
/// <param name="columnNameArray">包含了这个DataGridView需要的列名字的数组</param>
/// <param name="columnType">需要加入的特殊列的类型</param>
public static void SetDataGridViewColumnHeadSequence(ref DataGridView HandledDgView,
string[] columnNameArray,
AddSpecialColumnType columnType)
{
for (int i = 0; i < columnNameArray.Length; i++)//利用名字数组作循环
{
HandledDgView.Columns[columnNameArray[i]].DisplayIndex = i + 1;
}
从前写死的做法,暂时注释了#region 从前写死的做法,暂时注释了
//HandledDgView.Columns["FlightNo"].DisplayIndex = 0;
//HandledDgView.Columns["FlightBeginDate"].DisplayIndex = 1;
//HandledDgView.Columns["FlightEndDate"].DisplayIndex = 2;
//HandledDgView.Columns["FlightSession"].DisplayIndex = 3;
//HandledDgView.Columns["FlightSegment"].DisplayIndex = 4;
//HandledDgView.Columns["AirPlaneType"].DisplayIndex = 5;
//HandledDgView.Columns["FlightDepTime"].DisplayIndex = 6;
//HandledDgView.Columns["FlightDepSepDay"].DisplayIndex = 7;
//HandledDgView.Columns["FlightDestTime"].DisplayIndex = 8;
//HandledDgView.Columns["FlightDestSepDay"].DisplayIndex = 9;
//HandledDgView.Columns["FlightMaxWeight"].DisplayIndex = 10;
//HandledDgView.Columns["FlightControlWeight"].DisplayIndex = 11;
//HandledDgView.Columns["FlightOutWeight"].DisplayIndex = 12;
//HandledDgView.Columns["FlightFdepArea"].DisplayIndex = 13;
//HandledDgView.Columns["FlightFdestArea"].DisplayIndex = 14;
//HandledDgView.Columns["FlightRouting"].DisplayIndex = 15;
#endregion
}
/**//// <summary>
///返回一个定义了DataGridViewColumn的名字和数据绑定字段的数组
/// 用于DataGridView的列显示
/// </summary>
/// <param name="columnNameArray">列名字的数组</param>
/// <param name="specialColumnIndex">需要特殊设置的列宽的列的索引和宽度</param>
/// <param name="columnType">加入的特殊列的类型</param>
/// <returns>定义好的DataGridViewColumn对象数组</returns>
public static DataGridViewColumn[] SetDgViewColumnProperty(string[] columnNameArray,
System.Collections.Hashtable specialColumnIndex,
AddSpecialColumnType columnType)
{
//然后获得数组的边界,定义返回的DataGridViewColumn的数组范围
int arrCount = columnNameArray.Length;
//定义返回的DataGridViewColumn
DataGridViewColumn[] resultColumn = new DataGridViewColumn[arrCount + 1];
//设置一个是否需要进行特殊列宽度设置的标志
bool SpecialWidthFlag = false;
if (specialColumnIndex == null)
SpecialWidthFlag = false;
else
SpecialWidthFlag = true;
resultColumn[0] = GenerateSpecialColumn(columnType);
//bool SpecialWidthFlag = specialColumnIndex.Equals(null)?false:true;
//循环数组
for (int i = 0; i < columnNameArray.Length; i++)
{
DataGridViewTextBoxColumn tmpColumn = new DataGridViewTextBoxColumn();
//设置列对应的数据绑定的名字
tmpColumn.DataPropertyName = columnNameArray[i].Trim().ToUpper();
//设置列的名字
tmpColumn.Name = columnNameArray[i].Trim().ToUpper();
if (SpecialWidthFlag)//如果需要特殊设置
{
if (specialColumnIndex.ContainsKey(i))//如果是特殊需要设置的列,设置为自定义的宽度
{
tmpColumn.Width = (int)specialColumnIndex[i];
}
}
else
tmpColumn.Width = commonWidth;//其余的采用普通宽度
以前写死的,现在修改为使用list进行查找#region 以前写死的,现在修改为使用list进行查找
//if (i==7||i==9||i==10||i==13||i==14)
//{
// tmpColumn.Width = 110;
//}
//else
//{
// tmpColumn.Width = 80;
//}
/**/////判断是否有需要特殊设置的列
//if (specialColumnIndex.Contains(i))//如果是特殊需要设置的列,设置为长宽度
// tmpColumn.Width = 110;
//else
// tmpColumn.Width = commonWidth;//其余的采用普通宽度
#endregion
resultColumn[i + 1] = tmpColumn;
}
//返回结果
return resultColumn;
}
/**//// <summary>
/// 根据需要加入的特殊列的类型,产生一个特殊列
/// </summary>
/// <param name="columnType">要加入的特殊列的类型</param>
/// <returns>返回一个构造好的特殊列</returns>
private static DataGridViewColumn GenerateSpecialColumn(AddSpecialColumnType columnType)
{
DataGridViewColumn specialColumn = new DataGridViewColumn();
switch (columnType)
{
case AddSpecialColumnType.CheckBox:
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Frozen = true;
checkColumn.Width = 30;
checkColumn.TrueValue = "true";
checkColumn.FalseValue = "false";
specialColumn = checkColumn;
break;
case AddSpecialColumnType.ComboBox:
DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
comboColumn.Frozen = true;
comboColumn.Width = 30;
specialColumn = comboColumn;
break;
default:
DataGridViewCheckBoxColumn defaultCheckColumn = new DataGridViewCheckBoxColumn();
defaultCheckColumn.Frozen = true;
defaultCheckColumn.Width = 30;
defaultCheckColumn.TrueValue = "true";
defaultCheckColumn.FalseValue = "false";
specialColumn = defaultCheckColumn;
break;
}
return specialColumn;
}
/**//// <summary>
/// 设置需要特别显示宽度的列的index和需要设置的宽度
/// </summary>
/// <param name="ColumnIndex">包含了需要特殊设置的列的索引</param>
/// <param name="ColumnWidth">包含了需要特殊设置的列的宽度</param>
/// <returns>需要特殊设置列的索引和设置的宽度</returns>
public static System.Collections.Hashtable WidthEnlargeColumn(int[] ColumnIndex,
int[] ColumnWidth)
{
以前的做法,现在注释了#region 以前的做法,现在注释了
//int[] ColumnIndex = { 7, 9, 10, 13, 14 };
//List<int> specialColumnIndex = new List<int>();
//specialColumnIndex.AddRange(ColumnIndex);
//return (specialColumnIndex);
#endregion
System.Collections.Hashtable resultTable = new System.Collections.Hashtable();
if (ColumnIndex.Length != ColumnWidth.Length)//如果列索引与列的宽度的数组中包含的数量不相等,返回null
{
//return new System.Collections.Hashtable();
return resultTable;
}
//循环操作,将索引和对应的宽度加入到返回的Hashtable中
for (int i = 0; i < ColumnIndex.Length; i++)
{
resultTable.Add(ColumnIndex[i], ColumnWidth[i]);
}
return resultTable;
}
#endregion
一些关于文本框输入的可输入性的共用方法#region 一些关于文本框输入的可输入性的共用方法
/**//// <summary>
/// 只输入字母的判断
/// ASC://48-57(数字),65-90(字母)
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyLetterAndInteger(char e)
{
if ((e >= 65 && e <= 90) ||
(e >= 48 && e <= 57) || e == 8 ||
(e >= 97 && e <= 122) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入字母和数字还有-/的判断
/// ASC://48-57(数字),65-90(字母)
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyLetterAndIntegerAndSpeChar(char e)
{
if ((e >= 65 && e <= 90) ||
(e >= 48 && e <= 57) || e == 8 || e == 45 || e == 47 ||
(e >= 97 && e <= 122) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入字母的判断
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyLetter(char e)
{
if ((e >= 65 && e <= 90) ||
e == 8 ||
(e >= 97 && e <= 122) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入数字和小数点的判断
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyNumberAndDot(char e)
{
if ((e >= 48 && e <= 57) || (e == 46) || (e == (char)Keys.Back))
return true;
else
return false;
}
/**//// <summary>
/// 只输入数字的判断
/// </summary>
/// <param name="e">按键的对象</param>
/// <returns>符合要求返回true,否则返回false</returns>
public static bool InputOnlyNumber(char e)
{
if ((e >= 48 && e <= 57) || (e == (char)Keys.Back))
return true;
else
return false;
}
#endregion