using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Microsoft.Office.Tools.Excel;
using System.Data.SqlClient;
using System.Collections;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 生成数据源
private DataSet LoadData()
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("PassWord", typeof(string));
DataRow dr = null;
dr = dt.NewRow();
dr["UserName"] = "hetao";
dr["PassWord"] = "hetaoPWD";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserName"] = "zhouyu";
dr["PassWord"] = "zhouyPWD";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserName"] = "jianghui";
dr["PassWord"] = "00";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserName"] = "ZhanGuoXin";
dr["PassWord"] = "00";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserName"] = "DingXue";
dr["PassWord"] = "DX";
dt.Rows.Add(dr);
DataSet ds = new DataSet();
ds.Tables.Add(dt);//dt是你需要添加的DataTable;
return ds;
}
catch (Exception ex)
{
MessageBox.Show("生成数据错误" + ex.Message);
return null;
}
}
#endregion
#region 显示数据
private void btnSelectExc_Click(object sender, EventArgs e)
{
try
{
dataGridView1.DataSource = LoadData().Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("显示数据Button" + ex.Message);
}
}
#endregion
/// <summary>
/// 根据字段和字段的条件,赛选数据[数组存条件]
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="Fields">字段</param>
/// <param name="pids">查找条件</param>
/// <returns></returns>
private DataTable SortTable(DataTable dt,string Fields, string[] pids)
{
DataTable dt0 = dt.Clone(); //复制原表结构
for (int i = 0; i < pids.Length; i++)
{
if (pids[i] != string.Empty)
{
DataRow[] drs = dt.Select(Fields+"='" + pids[i] + "'");
if (drs.Length > 0)
{
foreach (DataRow dr in drs)
{
dt0.ImportRow(dr); //导入行
}
}
}
}
return dt0;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//ArrayList alist = new ArrayList();
string[] str = new string[4];
str[0] = "00";
str[1] = "DX";
dataGridView2.DataSource = SortTable(LoadData().Tables[0], "PassWord", str);
}
catch (Exception ex)
{
MessageBox.Show("Button"+ex.Message);
}
}
#region 根据字段,字段条件筛选数据[ArrayList存条件]
/// <summary>
/// 根据字段,字段条件筛选数据[ArrayList存条件]
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="Fields">字段</param>
/// <param name="alist">集合存条件</param>
/// <returns></returns>
private DataTable SortTable(DataTable dt ,String Fields,ArrayList alist)
{
try
{
DataTable dt1=null;
dt1 = dt.Clone();
if (dt != null && dt.Rows.Count > 0)
{
for(int i =0;i < alist.Count;i++)
{
if((alist[i]).ToString() != string.Empty)
{
DataRow[] drs = dt.Select(Fields + "='" + alist[i] + "'");
if(drs.Length>0)
{
foreach (DataRow dr in drs)
{
dt1.ImportRow(dr);
}
}
}
}
}
return dt1;
}
catch (Exception ex)
{
MessageBox.Show("ArrayList存条件函数"+ex.Message);
return null;
}
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
try
{
ArrayList alist = new ArrayList();
alist.Add("00");
alist.Add("DX");
dataGridView2.DataSource = SortTable(LoadData().Tables[0], "PassWord", alist);
}
catch (Exception ex)
{
MessageBox.Show("Button"+ex.Message);
}
}
}
}