DataTable search keyword

 

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 System.Security;
using System.Reflection;
using System.Security.Permissions;

[assembly: AssemblyKeyFile("keys.snk")]
[assembly: AssemblyVersion("1.1.1.0")]
namespace FindDataTableDeme
{
    [PublisherIdentityPermission(SecurityAction.InheritanceDemand,CertFile="Certificate.cer")]
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        private DataRow rowFound;
        //System.Runtime.Serialization.ISerializable
        //System.SerializableAttribute

        /// <summary>
        ///
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 塗聚文  締友計算機信息技術有限公司 Geovin Du
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //DataTable dt = findDatatble();
            //DataRow foundRow =dt.DefaultView.Find(this.textBox1.Text.Trim());
            findDatatble();
        }

        private void findDatatble()
        {

            DataTable table1 = new DataTable("table one");
            DataTable table2 = new DataTable("table two");

            //creating columns for the tables:
            table1.Columns.Add(new DataColumn("id", typeof(int)));
            table1.Columns.Add(new DataColumn("someText", typeof(string)));

            table2.Columns.Add(new DataColumn("id2", typeof(int)));
            table2.Columns.Add(new DataColumn("someOtherText", typeof(string)));

            //populating tables, one by one and add them to dataSet:
            //populating table 1:
            DataRow dr;
            for (int i = 1; i < 13; i++)
            {
                dr = table1.NewRow();
                dr["id"] = i;
                dr["someText"] = "text with number " + i.ToString();
                table1.Rows.Add(dr);
            }
            dr = table1.NewRow();
            dr["id"] = 14;
            dr["someText"] = "涂聚文";
            table1.Rows.Add(dr);
            //populating table 2:
            for (int i = 101; i < 113; i++)
            {
                dr = table2.NewRow();
                dr["id2"] = i;
                dr["someOtherText"] = "other text with number " + i.ToString();
                table2.Rows.Add(dr);
            }
            dr = table2.NewRow();
            dr["id2"] = 114;
            dr["someOtherText"] = "涂聚文";
            table2.Rows.Add(dr);
            //adding both tables to dataSet:
            ds.Tables.AddRange(new DataTable[] { table1, table2 });
            //you could add them seperately, like:
            //ds.Tables.Add(table1);
            //ds.Tables.Add(table2);

            //Now lets loop through the dataSet and write the results out (int messageBox):
            for (int i = 0; i < ds.Tables.Count; i++)      //LOOP THROUGH TABLES OF DATASET
            {
                string text = null;
                foreach (DataRow dr1 in ds.Tables[i].Rows) //LOOP TRGOUGH THE ROWS OF <strong class="highlight">DATATABLE</strong>
                {
                    string a = dr1[0].ToString();
                    string b = dr1[1].ToString();
                    text += a + ". " + b + Environment.NewLine;
                }
               // MessageBox.Show("In dataSet is dataTable of index [" + i + "] with values:\n" + text);
            }
            ds.Tables[0].DefaultView.Sort = "id";
            // Set Primary Key and Sort Order
            DataColumn[] dcolPk = new DataColumn[1];
            dcolPk[0] = ds.Tables[0].Columns["someText"];
            ds.Tables[0].PrimaryKey = dcolPk;

            dataGridView1.DataSource = ds.Tables[0].DefaultView;

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int intRow;
                object s = textBox1.Text.Trim();
                // At least one row matches primary key
                rowFound = ds.Tables[0].Rows.Find(s); //所搜索的内容,也是设定的主键
                if (rowFound != null)
                {
                    MessageBox.Show(rowFound[0].ToString()+","+rowFound[1].ToString());
                }
                else
                {
                    MessageBox.Show("A row with the primary key of " + s + " could not be found");
                }
                DataRow[] foundRows;
                foundRows = ds.Tables[0].Select("someText Like '涂%'");
                if (foundRows != null)
                {
                    MessageBox.Show(foundRows[0].ToString());
                }

                //// Finds the row specified in txtFindArg
                //intRow = ds.Tables[0].DefaultView.Find(s);
                ////Debug.WriteLine(intRow);
                //if (intRow == -1)
                //{
                //    MessageBox.Show("No PK matches " + textBox1.Text);
                //}
                //else
                //{
                //    // Jump to the Row and select it
                //    //dataGridView1.CurrentRow.Index = intRow;  //CurrentRowIndex
                //    dataGridView1.Rows[intRow].Selected=true;
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void s()
        {

            //create a datatable object which will host the two column: notebookID, notebook producer
            DataTable o_aTable = new DataTable("Notebooks");

            //creating a datacolumn
                //definition and initilization of column
                DataColumn o_aColumn = new DataColumn();
                //defining column properties
                    //caption
                    o_aColumn.Caption = "Notebook Producers";
                    //type of column
                    o_aColumn.DataType = System.Type.GetType("System.String");
                    //access name of column
                    o_aColumn.ColumnName = "Producer";
                    //default value
                    o_aColumn.DefaultValue = "unknown producer";

            //add column to the table
            o_aTable.Columns.Add(o_aColumn);

                //initialize a new instance of data column for creating a new column
                o_aColumn = new DataColumn();
                //defining column properties
                    //caption
                    o_aColumn.Caption = "Notebook Producer ID";
                    //type of column
                    o_aColumn.DataType = System.Type.GetType("System.Int32");
                    //access name of column
                    o_aColumn.ColumnName = "ProducerID";
                    //default value
                    o_aColumn.DefaultValue = 0000;

            //add new column to the table
            o_aTable.Columns.Add(o_aColumn);

            //create a primary key column to use search
                //definition and initial.
                DataColumn[] o_aPrimaryKeyColumn = new DataColumn[1];
                //assigning notebookID column of created table to this column: it will serve as primary key column
                o_aPrimaryKeyColumn[0] = o_aTable.Columns["ProducerID"];
                //mapping primary key column of table to the created primary key holder column
            o_aTable.PrimaryKey = o_aPrimaryKeyColumn;

            //adding rows-records to column
                //create a datarow object which serves as a record entry
                DataRow o_aRow;

                //adding the records
                    //add 1th record for producer HP
                        //initialize a new row for table object
                        o_aRow = o_aTable.NewRow();
                        //assign value of 1th column ID
                        o_aRow["ProducerID"] = 1;
                        //assign value of 2th column producer
                        o_aRow["Producer"] = "HP";
                        //add 1th row to the table
                        o_aTable.Rows.Add(o_aRow);

                    //add 2nd record for producer IBM
                        //initialize a new row for table object
                        o_aRow = o_aTable.NewRow();
                        //assign value of 1th column ID
                        o_aRow["ProducerID"] = 2;
                        //assign value of 2th column producer
                        o_aRow["Producer"] = "IBM";
                        //add 2nd row to the table
                        o_aTable.Rows.Add(o_aRow);

            //display the records within table

            for (int i = 0; i < o_aTable.Rows.Count;i++ )
            {
                //display ID
                Console.WriteLine("row " + i + ": notebook ID is: " + o_aTable.Rows[i]["ProducerID"].ToString());
                //display producer
                Console.WriteLine("row " + i + ": notebook Producer is: " +  o_aTable.Rows[i]["Producer"].ToString());

            }
            //Handling row with specifying a particular primary column addressed by ID
                //create a row object to store found row which matches criteria ID
                DataRow o_dRow_findedRow;
                //look for row with id 1
                if ((o_dRow_findedRow = o_aTable.Rows.Find("1")) != null)
                {

                    Console.WriteLine("Primary key column of Table (in memory) is being queried for notebookID 1...");
                    Console.WriteLine("A row with notebookID 1 is found.");
                }
                else
                {
                    Console.WriteLine("A record with notebookID 1 is not found.");
                }

         }

    }
}
时间: 2024-08-30 19:41:23

DataTable search keyword的相关文章

jquery datatable search 能不能记忆

问题描述 jquery datatable search 能不能记忆 JQ datatable里面的search功能,我想在已经搜索的基础上在使用搜索,.search(.search)如何实现呢 解决方案 你需要用localstorage去存储. 解决方案二: 可以详细点么,搜索存储这段的

基于SpringMVC+Bootstrap+DataTables实现表格服务端分页、模糊查询_javascript技巧

前言 基于SpringMVC+Bootstrap+DataTables实现数据表格服务端分页.模糊查询(非DataTables Search),页面异步刷新. 说明:sp:message标签是使用了SpringMVC国际化 效果 DataTable表格 关键字查询 自定义关键字查询,非DataTable Search 代码 HTML代码 查询条件代码 <!-- 查询.添加.批量删除.导出.刷新 --> <div class="row-fluid"> <di

一个php作的文本留言本的例子(一)

大家知道,数据库对于网络来说的重要性.由于cgi的复杂,现在asp和php+mysql已经成为主流.几乎所有的个人网页都要用到留言本,可是申请的留言本很不稳定.这为网上的交流带来了诸多不便.所以,希望拥有自己的留言本的朋友越来越多. 但是,免费的个人主页支持asp和php的很少.笔者现在向您推荐奥索网,(http://www.oso.com.cn)支持php.这样您便有了能够拥有自己留言本的基础.现在,我就通过一个文本留言本的例子来讲述php的简单使用. 首先,我们先确定,留言的几个过程:写留言

一年纯手工打造的Java老A上册开始预售了

Java老A这本书写了很久昨天终于开始在china-pub.京东.天猫上开始预售了不过既然叫预售就肯定还没到货. 有兴趣的人可以去看看哈后续其它网站地址也会在这里公开 china-pub http://search.china-pub.com/s/?key1=java%cc%d8%d6%d6%b1%f8&type=&pz=1 京东 http://search.jd.com/Search?keyword=Java%E7%89%B9%E7%A7%8D%E5%85%B5&enc=utf-

http request乱码的真相

当然,终极原因http协议里没有规定request一定要指定编码,导致浏览器,web服务器都各搞一套-- 下面一一理清. 首先,从浏览器端看下有多少种情况: 1.在浏览器的地址栏,或者搜索框里输入地址:http://www.test.com/衣服/search?keyword=T恤  2.在一个指定了编码的网页中,提交一个form,如: <html> <head> <meta charset="gbk"> </head> <body

elk收集分析nginx access日志

elk收集分析nginx access日志 首先elk的搭建按照这篇文章使用elk+redis搭建nginx日志分析平台说的,使用redis的push和pop做队列,然后有个logstash_indexer来从队列中pop数据分析插入elasticsearch.这样做的好处是可扩展,logstash_agent只需要收集log进入队列即可,比较可能会有瓶颈的log分析使用logstash_indexer来做,而这个logstash_indexer又是可以水平扩展的,我可以在单独的机器上跑多个in

shell正则表达式及grepfindawksed详解

shell 正则表达式和grep awk find sed sort uniq cut 一,grep 1,基础参数 grep-[acinv]'搜索内容串'filename -a以文本文件方式搜索 -c计算找到的符合行的次数 -i忽略大小写 -n顺便输出行号 -v反向选择,即找没有搜索字符串的行 [root@test3 ~]# cat Travel.doc Travel is a good way to refresh and broaden our horizon. During your tr

Linux的yum命令用法简介

1.什么是 yum yum 是 yellowdog updater modified 的缩写.yellowdog 是一个 Linux 的 distribution,RH 将这种升级技术利用到自己的 distribution 形成了现在的 yum,感觉上 yum 和 apt 的原理类似,但是 apt 是编译代码,执行效率远高于使用 python 写成的 yum. yum 的理念是使用一个中心仓库(repository)管理一部分甚至一个 distribution 的应用程序相互关系,根据计算出来的

HTML5移动应用开发第2章:移动web应用的本地存储

在本文中,您将使用最新 Web 技术开发 Web 应用程序.这里的大多数代码只是 HTML.JavaScript 和 CSS - 任何 Web 开发人员的核心技术.需要的最重要的东西是用于测试代码的浏览器.本文中的大多数代码将运行在最新的桌面浏览器上,例外的情况会指出来.当然,还必须在移动浏览器上进行测试,您肯定希望最新的 iPhone 和 Android SDK 支持这些代码.本文中使用的是 iPhone SDK 3.1.3 和 Android SDK 2.1. 本地存储基础 Web 开发人员