WinForm的一些共用方法

本文详细介绍了DataGridView控件的各种操作方法,包括创建行、插入值、设置列属性及特殊列的生成等,提供了丰富的代码示例帮助开发者更好地掌握DataGridView的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

ContractedBlock.gifExpandedBlockStart.gif        设置datagridview的一些row和cell的方法,例如产生row,将值插入到一个row中并返回#region 设置datagridview的一些row和cell的方法,例如产生row,将值插入到一个row中并返回
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 返回一个实例化之后的DataGridViewRow对象
        
/// </summary>
        
/// <param name="columnCount">DataGridViewRow对象包含的列的数量</param>
        
/// <param name="firstColumnType">第一个列的类型是什么</param>
        
/// <returns>初始化好了的datagridviewrow对象</returns>

        public static DataGridViewRow GenerateDgvCommonRow(int columnCount,
                                                           AddSpecialColumnType firstColumnType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            DataGridViewRow tmpRow 
= new DataGridViewRow();
            
for (int i = 0; i < columnCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (i > 0)//第二列开始加入的都是文本框
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    tmpRow.Cells.Add(
new DataGridViewTextBoxCell());
                }

                
else//第一列根据用户的输入参数来决定,默认是文本框
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    
switch (firstColumnType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
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;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 点击全选的时候调用的方法
        
/// </summary>
        
/// <param name="HandledDgView">需要处理的DataGridView</param>
        
/// <param name="checkBosPos">checkbox的位置</param>

        public static void GetAllCheckBokChecked(ref DataGridView HandledDgView,
            
int checkBosPos)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach (DataGridViewRow row in HandledDgView.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if ((row.Cells[checkBosPos].GetType().Equals(typeof(DataGridViewCheckBoxCell))))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DataGridViewCheckBoxCell CheckBoxCol 
=
                                (DataGridViewCheckBoxCell)row.Cells[checkBosPos];
                    
if (CheckBoxCol.Value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        CheckBoxCol.Value 
= true;
                    }

                    
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    {
                        CheckBoxCol.Value 
= true;
                    }

                    
//else
                    
//{
                    
//    CheckBoxCol.Value = false;
                    
//
                }

            }

        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 点击反向全选的时候调用的方法
        
/// </summary>
        
/// <param name="HandledDgView">需要处理的DataGridView</param>
        
/// <param name="checkBosPos">checkbox的位置</param>

        public static void GetAllCheckBokReverseChecked(ref DataGridView HandledDgView,
            
int checkBosPos)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach (DataGridViewRow row in HandledDgView.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (row.Cells[checkBosPos].GetType().Equals(typeof(DataGridViewCheckBoxCell)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DataGridViewCheckBoxCell CheckBoxCol 
=
                                (DataGridViewCheckBoxCell)row.Cells[checkBosPos];

                    
if (CheckBoxCol.Value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        CheckBoxCol.Value 
= true;
                    }

                    
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    {
                        CheckBoxCol.Value 
= true;
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        CheckBoxCol.Value 
= false;
                    }

                }

            }

        }

        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 反相全选的方法
        
/// </summary>
        
/// <param name="HandledDgView">需要处理的DataGridView</param>
        
/// <param name="checkBosPos">checkbox的位置</param>

        public static void GetAllCheckBokCheckedReverse(ref DataGridView HandledDgView,
            
int checkBosPos)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach (DataGridViewRow row in HandledDgView.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                DataGridViewCheckBoxCell CheckBoxCol 
=
                    (DataGridViewCheckBoxCell)row.Cells[checkBosPos];
                
if (CheckBoxCol.Value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    CheckBoxCol.Value 
= false;
                }

                
if (CheckBoxCol.Value.ToString() == "False")//如果被选择了,加入航班执行日期
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    CheckBoxCol.Value 
= true;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    CheckBoxCol.Value 
= false;
                }

            }

        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//调用本类中的方法产生一个datagridrow
            DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
            
//定义一个是否加入了特殊列的标志
            bool IsAddSpecialColumnFlag = false;
            
for (int i = 0; i < tmpRow.Cells.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (i == 0)//当第一列的时候,如果不是textbox,那么不插入值
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    
if (firstColumnType == AddSpecialColumnType.TextBox)
                        tmpRow.Cells[i].Value 
= value[i];
                    
else
                        IsAddSpecialColumnFlag 
= true;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (IsAddSpecialColumnFlag)//如果加入了特殊的列,那么取值就是上一次的
                        tmpRow.Cells[i].Value = value[i - 1];
                    
else
                        tmpRow.Cells[i].Value 
= value[i];
                }

            }

            HandledDgView.Rows.Add(tmpRow);
        }



ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//调用本类中的方法产生一个datagridrow
            DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
            
//定义一个是否加入了特殊列的标志
            bool IsAddSpecialColumnFlag = false;
            
for (int i = 0; i < tmpRow.Cells.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//当第一列的时候,如果不是textbox,那么不插入值
                if (i == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (firstColumnType == AddSpecialColumnType.TextBox)
                        tmpRow.Cells[i].Value 
= value[i];
                    
else
                        IsAddSpecialColumnFlag 
= true;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//如果加入了特殊的列,那么取值就是上一次的
                    if (IsAddSpecialColumnFlag)
                        tmpRow.Cells[i].Value 
= value[i - 1];
                    
else
                        tmpRow.Cells[i].Value 
= value[i];
                }

            }

            tmpRow.DefaultCellStyle.BackColor 
= rowColor;
            HandledDgView.Rows.Add(tmpRow);
        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//调用本类中的方法产生一个datagridrow
            DataGridViewRow tmpRow = GenerateDgvCommonRow(columnCount, firstColumnType);
            
//定义一个是否加入了特殊列的标志
            bool IsAddSpecialColumnFlag = false;
            
for (int i = 0; i < tmpRow.Cells.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (i == 0)//当第一列的时候,如果不是textbox,那么不插入值
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    
if (firstColumnType == AddSpecialColumnType.TextBox)
                        tmpRow.Cells[i].Value 
= value[i];
                    
else
                        IsAddSpecialColumnFlag 
= true;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (IsAddSpecialColumnFlag)//如果加入了特殊的列,那么取值就是上一次的
                        tmpRow.Cells[i].Value = value[i - 1];
                    
else
                        tmpRow.Cells[i].Value 
= value[i];
                }

            }

            tmpRow.DefaultCellStyle.ForeColor 
= fontColor;
            HandledDgView.Rows.Add(tmpRow);
        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 向一个datagridview中添加要显示的值
        
/// </summary>
        
/// <param name="handler">要被操作的datagridview控件</param>
        
/// <param name="value">行的值</param>

        public static void InsertValueToDataGridView(ref DataGridView handler,
                                                     
string[] value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            handler.Rows.Add(value);
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置datagridview的列不排序
        
/// </summary>
        
/// <param name="handler">需要处理的datagridview</param>

        public static void SetColumnNotSortable(ref DataGridView handler)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach (DataGridViewColumn dc in handler.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                dc.SortMode 
= DataGridViewColumnSortMode.NotSortable;
            }

        }

        
#endregion


ContractedBlock.gifExpandedBlockStart.gif        
设置界面的显示列的一些方法#region 设置界面的显示列的一些方法

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 将设置好的DataGridViewColumn[]加入到要显示的DataGridView中
        
/// </summary>
        
/// <param name="HandledDgView">需要被设置的datagridview</param>
        
/// <param name="columnInfo">列信息</param>

        public static void AddSettedColmnInfoToDataGridView(ref DataGridView HandledDgView,
                                                     DataGridViewColumn[] columnInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
测试用的代码,暂时注释#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);
        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置界面显示结果的列名字
        
/// </summary>

        public static void SetDataGridViewColumnHeader(ref DataGridView HandledDgView,
                                                       
string[] columnNameArray,
                                                       
string[] columnDescriptionArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
从前写死的做法,现在注释了#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++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                HandledDgView.Columns[columnNameArray[i]].HeaderText 
= columnDescriptionArray[i];
            }

        }

        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置界面显示结果的列的显示顺序,提供一个使用列名字作为参数的重载

        
/// </summary>
        
/// <param name="HandledDgView">需要被设置的DataGridView</param>
        
/// <param name="columnNameArray">包含了这个DataGridView需要的列名字的数组</param>

        public static void SetDataGridViewColumnHeadSequence(ref DataGridView HandledDgView,
                                                             
string[] columnNameArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i < columnNameArray.Length; i++)//利用名字数组作循环
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                HandledDgView.Columns[columnNameArray[i]].DisplayIndex 
= i;
            }

ContractedSubBlock.gifExpandedSubBlockStart.gif            
从前写死的做法,暂时注释了#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

        }

        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
//然后获得数组的边界,定义返回的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++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                DataGridViewTextBoxColumn tmpColumn 
= new DataGridViewTextBoxColumn();
                
//设置列对应的数据绑定的名字

                tmpColumn.DataPropertyName 
= columnNameArray[i].Trim().ToUpper();
                
//设置列的名字
                tmpColumn.Name = columnNameArray[i].Trim().ToUpper();
                
if (SpecialWidthFlag)//如果需要特殊设置
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    
if (specialColumnIndex.ContainsKey(i))//如果是特殊需要设置的列,设置为自定义的宽度
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    {
                        tmpColumn.Width 
= (int)specialColumnIndex[i];
                        
if ((int)specialColumnIndex[i] == 0)//如果此列的宽度为0,说明不显示
                            tmpColumn.Visible = false;
                    }

                }

                
else
                    tmpColumn.Width 
= commonWidth;//其余的采用普通宽度


ContractedSubBlock.gifExpandedSubBlockStart.gif                
以前写死的,现在修改为使用list进行查找#region 以前写死的,现在修改为使用list进行查找
                
//if (i==7||i==9||i==10||i==13||i==14)
                
//{
                
//    tmpColumn.Width = 110;
                
//}
                
//else
                
//{
                
//    tmpColumn.Width = 80;
                
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
                /**/////判断是否有需要特殊设置的列

                
//if (specialColumnIndex.Contains(i))//如果是特殊需要设置的列,设置为长宽度
                
//    tmpColumn.Width = 110;
                
//else
                
//    tmpColumn.Width = commonWidth;//其余的采用普通宽度

                
#endregion

                resultColumn[i] 
= tmpColumn;
            }

            
//返回结果
            return resultColumn;
        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置界面显示结果的列名字
        
/// </summary>

        public static void SetDataGridViewColumnHeader(ref DataGridView HandledDgView,
                                                       
string[] columnNameArray,
                                                       
string[] columnDescriptionArray,
                                                       AddSpecialColumnType columnType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
从前写死的做法,现在注释了#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++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                HandledDgView.Columns[columnNameArray[i]].HeaderText 
= columnDescriptionArray[i];
            }

        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i < columnNameArray.Length; i++)//利用名字数组作循环
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                HandledDgView.Columns[columnNameArray[i]].DisplayIndex 
= i + 1;
            }

ContractedSubBlock.gifExpandedSubBlockStart.gif            
从前写死的做法,暂时注释了#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

        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
//然后获得数组的边界,定义返回的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++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                DataGridViewTextBoxColumn tmpColumn 
= new DataGridViewTextBoxColumn();
                
//设置列对应的数据绑定的名字

                tmpColumn.DataPropertyName 
= columnNameArray[i].Trim().ToUpper();
                
//设置列的名字
                tmpColumn.Name = columnNameArray[i].Trim().ToUpper();
                
if (SpecialWidthFlag)//如果需要特殊设置
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    
if (specialColumnIndex.ContainsKey(i))//如果是特殊需要设置的列,设置为自定义的宽度
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    {
                        tmpColumn.Width 
= (int)specialColumnIndex[i];
                    }

                }

                
else
                    tmpColumn.Width 
= commonWidth;//其余的采用普通宽度


ContractedSubBlock.gifExpandedSubBlockStart.gif                
以前写死的,现在修改为使用list进行查找#region 以前写死的,现在修改为使用list进行查找
                
//if (i==7||i==9||i==10||i==13||i==14)
                
//{
                
//    tmpColumn.Width = 110;
                
//}
                
//else
                
//{
                
//    tmpColumn.Width = 80;
                
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
                /**/////判断是否有需要特殊设置的列

                
//if (specialColumnIndex.Contains(i))//如果是特殊需要设置的列,设置为长宽度
                
//    tmpColumn.Width = 110;
                
//else
                
//    tmpColumn.Width = commonWidth;//其余的采用普通宽度

                
#endregion

                resultColumn[i 
+ 1= tmpColumn;
            }

            
//返回结果
            return resultColumn;
        }

       
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 根据需要加入的特殊列的类型,产生一个特殊列
        
/// </summary>
        
/// <param name="columnType">要加入的特殊列的类型</param>
        
/// <returns>返回一个构造好的特殊列</returns>

        private static DataGridViewColumn GenerateSpecialColumn(AddSpecialColumnType columnType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            DataGridViewColumn specialColumn 
= new DataGridViewColumn();
            
switch (columnType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
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;
        }


        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置需要特别显示宽度的列的index和需要设置的宽度
        
/// </summary>
        
/// <param name="ColumnIndex">包含了需要特殊设置的列的索引</param>
        
/// <param name="ColumnWidth">包含了需要特殊设置的列的宽度</param>
        
/// <returns>需要特殊设置列的索引和设置的宽度</returns>

        public static System.Collections.Hashtable WidthEnlargeColumn(int[] ColumnIndex,
                                                                      
int[] ColumnWidth)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
以前的做法,现在注释了#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
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                
//return new System.Collections.Hashtable();
                return resultTable;
            }


            
//循环操作,将索引和对应的宽度加入到返回的Hashtable中

            
for (int i = 0; i < ColumnIndex.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                resultTable.Add(ColumnIndex[i], ColumnWidth[i]);
            }

            
return resultTable;
        }



        
#endregion


ContractedBlock.gifExpandedBlockStart.gif        
一些关于文本框输入的可输入性的共用方法#region 一些关于文本框输入的可输入性的共用方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 只输入字母的判断
        
/// ASC://48-57(数字),65-90(字母)
        
/// </summary>
        
/// <param name="e">按键的对象</param>
        
/// <returns>符合要求返回true,否则返回false</returns>

        public static bool InputOnlyLetterAndInteger(char e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if ((e >= 65 && e <= 90||
                (e 
>= 48 && e <= 57|| e == 8 ||
                (e 
>= 97 && e <= 122|| (e == (char)Keys.Back))
                
return true;
            
else
                
return false;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 只输入字母和数字还有-/的判断

        
/// ASC://48-57(数字),65-90(字母)
        
/// </summary>
        
/// <param name="e">按键的对象</param>
        
/// <returns>符合要求返回true,否则返回false</returns>

        public static bool InputOnlyLetterAndIntegerAndSpeChar(char e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 只输入字母的判断
        
/// </summary>
        
/// <param name="e">按键的对象</param>
        
/// <returns>符合要求返回true,否则返回false</returns>

        public static bool InputOnlyLetter(char e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if ((e >= 65 && e <= 90||
                 e 
== 8 ||
                (e 
>= 97 && e <= 122|| (e == (char)Keys.Back))
                
return true;
            
else
                
return false;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 只输入数字和小数点的判断
        
/// </summary>
        
/// <param name="e">按键的对象</param>
        
/// <returns>符合要求返回true,否则返回false</returns>

        public static bool InputOnlyNumberAndDot(char e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if ((e >= 48 && e <= 57|| (e == 46|| (e == (char)Keys.Back))
                
return true;
            
else
                
return false;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 只输入数字的判断
        
/// </summary>
        
/// <param name="e">按键的对象</param>
        
/// <returns>符合要求返回true,否则返回false</returns>

        public static bool InputOnlyNumber(char e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if ((e >= 48 && e <= 57|| (e == (char)Keys.Back))
                
return true;
            
else
                
return false;
        }



        
#endregion

转载于:https://www.cnblogs.com/greatandforever/archive/2009/05/19/1460371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值