WinForm 自定义查询

需求:

      设置自定义查询功能,使用户可以根据自行关注内容在界面显示相应信息。

设计思路:

      1.首先将原本所有包含的列完整查询出来,并填充到DataGridView
      2.将DataGridView各个列的visiable值转换成0/1(ture->1,false->0)后,再按序组合成一个配置字符串
      3.使用CheckedListBox控件中的项对应配置字符串中的值(DataGridView的列)。
      4.通过应用程序配置Settings.settings存取用户自定义设置的字符串值

功能实现:Demo

    设计图如下:

    
                      图1 初始查询信息

 

  

                                      图2 自定义显示列表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomSelectDemo.Properties;

namespace CustomSelectDemo
{
    
public partial class Test : Form
    {
        
private string strCustomAwbInfo = "";
        
public Test()
        {
            InitializeComponent();
        }

        
private void Test_Load(object sender, EventArgs e)
        {
            pnlCustom.Visible 
= false;
            
//初始化查询信息
            
//读取配置文件的值
            if (Settings.Default.strCustomAwbInfo != "")
            {
                strCustomAwbInfo 
= Settings.Default.strCustomAwbInfo;
                ShowCustomDataGridView(dgvAwbInfo,strCustomAwbInfo);
            }
        }

        
/// <summary>
        
/// 根据用户设置显示DataGridView内容
        
/// </summary>
        
/// <param name="customStr"></param>
        
/// <param name="dgv"></param>
        private void ShowCustomDataGridView(DataGridView dgv,string strCustom)
        {
            
for (int i = 0; i < strCustom.Length; i++)
            {
                
switch (strCustom[i].ToString())
                {
                    
case "1":
                        dgv.Columns[i].Visible 
= true;
                        
break;
                    
case "0":
                        dgv.Columns[i].Visible 
= false;
                        
break;
                    
default:
                        dgv.Columns[i].Visible 
= true;
                        
break;
                }
            }
        }

        
/// <summary>
        
/// 查询
        
/// </summary>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
"AwbNo"typeof(String));
            dt.Columns.Add(
"FlightNo"typeof(String));
            dt.Columns.Add(
"FlightDate"typeof(String));
            dt.Columns.Add(
"Piece"typeof(String));
            dt.Columns.Add(
"Weight"typeof(String));
            dt.Columns.Add(
"Remark"typeof(String));

            dt.Rows.Add(
"784-12912900""CZ129""2009-12-9""10""100""");
            dt.Rows.Add(
"784-12912911""CZ129""2009-12-9""11""121""");

            dgvAwbInfo.AutoGenerateColumns 
= false;
            dgvAwbInfo.DataSource 
= dt;
        }

        
private void btnDgvCustom_Click(object sender, EventArgs e)
        {
            pnlCustom.Visible 
= true;
            InitializeCheckedListBox(clbAwbInfo, strCustomAwbInfo);
        }

        
/// <summary>
        
/// 根据用户设置初始化CheckedBoxList;第一次设置前全部选择
        
/// </summary>
        private void InitializeCheckedListBox(CheckedListBox clb, string strCustom)
        {
            
if (strCustom != "")
            {
                
for (int i = 0; i < strCustom.Length; i++)
                {
                    
switch (strCustom[i].ToString())
                    {
                        
case "1":
                            clb.SetItemChecked(i, 
true);
                            
break;
                        
case "0":
                            clb.SetItemChecked(i, 
false);
                            
break;
                        
default:
                            clb.SetItemChecked(i, 
true);
                            
break;
                    }
                }
            }
            
else
            {
                
for (int i = 0; i < clb.Items.Count; i++)
                {
                    clb.SetItemChecked(i, 
true);
                }
            }
        }

        
/// <summary>
        
/// 保存
        
/// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            
string str = "";
            
for (int i = 0; i < clbAwbInfo.Items.Count; i++)
            {
                
if (clbAwbInfo.GetItemChecked(i))
                {
                    str 
+= "1";
                }
                
else
                    {
                    str 
+= "0";
                }
            }
            strCustomAwbInfo
=str;
            
//修改配置文件的值
            Settings.Default.strCustomAwbInfo = strCustomAwbInfo;
            Settings.Default.Save();

            pnlCustom.Visible 
= false;
            ShowCustomDataGridView(dgvAwbInfo, strCustomAwbInfo);
        }

        
private void btnCancel_Click(object sender, EventArgs e)
        {
            pnlCustom.Visible 
= false;
        }

        
private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

PS:本人比较少写原作,语言描述能力尚未足,文章中文字描述很少,请见谅。 还好实现功能的代码相对简单,看懂应该问题不大吧.. 

原文地址:https://www.cnblogs.com/greatandforever/p/1620443.html