zoukankan      html  css  js  c++  java
  • DataTable search keyword

      1 using System;
    2 using System.Collections.Generic;
    3 using System.ComponentModel;
    4 using System.Data;
    5 using System.Drawing;
    6 using System.Linq;
    7 using System.Text;
    8 using System.Windows.Forms;
    9 using System.Security;
    10 using System.Reflection;
    11 using System.Security.Permissions;
    12
    13 [assembly: AssemblyKeyFile("keys.snk")]
    14 [assembly: AssemblyVersion("1.1.1.0")]
    15 namespace FindDataTableDeme
    16 {
    17 [PublisherIdentityPermission(SecurityAction.InheritanceDemand,CertFile="Certificate.cer")]
    18 public partial class Form1 : Form
    19 {
    20 DataSet ds = new DataSet();
    21 private DataRow rowFound;
    22 //System.Runtime.Serialization.ISerializable
    23 //System.SerializableAttribute
    24
    25 /// <summary>
    26 ///
    27 /// </summary>
    28 public Form1()
    29 {
    30 InitializeComponent();
    31 }
    32 /// <summary>
    33 /// 塗聚文 締友計算機信息技術有限公司 Geovin Du
    34 /// </summary>
    35 /// <param name="sender"></param>
    36 /// <param name="e"></param>
    37 private void Form1_Load(object sender, EventArgs e)
    38 {
    39 //DataTable dt = findDatatble();
    40 //DataRow foundRow =dt.DefaultView.Find(this.textBox1.Text.Trim());
    41 findDatatble();
    42 }
    43
    44 private void findDatatble()
    45 {
    46
    47 DataTable table1 = new DataTable("table one");
    48 DataTable table2 = new DataTable("table two");
    49
    50 //creating columns for the tables:
    51 table1.Columns.Add(new DataColumn("id", typeof(int)));
    52 table1.Columns.Add(new DataColumn("someText", typeof(string)));
    53
    54 table2.Columns.Add(new DataColumn("id2", typeof(int)));
    55 table2.Columns.Add(new DataColumn("someOtherText", typeof(string)));
    56
    57 //populating tables, one by one and add them to dataSet:
    58 //populating table 1:
    59 DataRow dr;
    60 for (int i = 1; i < 13; i++)
    61 {
    62 dr = table1.NewRow();
    63 dr["id"] = i;
    64 dr["someText"] = "text with number " + i.ToString();
    65 table1.Rows.Add(dr);
    66 }
    67 dr = table1.NewRow();
    68 dr["id"] = 14;
    69 dr["someText"] = "涂聚文";
    70 table1.Rows.Add(dr);
    71 //populating table 2:
    72 for (int i = 101; i < 113; i++)
    73 {
    74 dr = table2.NewRow();
    75 dr["id2"] = i;
    76 dr["someOtherText"] = "other text with number " + i.ToString();
    77 table2.Rows.Add(dr);
    78 }
    79 dr = table2.NewRow();
    80 dr["id2"] = 114;
    81 dr["someOtherText"] = "涂聚文";
    82 table2.Rows.Add(dr);
    83 //adding both tables to dataSet:
    84 ds.Tables.AddRange(new DataTable[] { table1, table2 });
    85 //you could add them seperately, like:
    86 //ds.Tables.Add(table1);
    87 //ds.Tables.Add(table2);
    88
    89
    90 //Now lets loop through the dataSet and write the results out (int messageBox):
    91 for (int i = 0; i < ds.Tables.Count; i++) //LOOP THROUGH TABLES OF DATASET
    92 {
    93 string text = null;
    94 foreach (DataRow dr1 in ds.Tables[i].Rows) //LOOP TRGOUGH THE ROWS OF <strong class="highlight">DATATABLE</strong>
    95 {
    96 string a = dr1[0].ToString();
    97 string b = dr1[1].ToString();
    98 text += a + ". " + b + Environment.NewLine;
    99 }
    100 // MessageBox.Show("In dataSet is dataTable of index [" + i + "] with values:\n" + text);
    101 }
    102 ds.Tables[0].DefaultView.Sort = "id";
    103 // Set Primary Key and Sort Order
    104 DataColumn[] dcolPk = new DataColumn[1];
    105 dcolPk[0] = ds.Tables[0].Columns["someText"];
    106 ds.Tables[0].PrimaryKey = dcolPk;
    107
    108 dataGridView1.DataSource = ds.Tables[0].DefaultView;
    109
    110 }
    111 /// <summary>
    112 ///
    113 /// </summary>
    114 /// <param name="sender"></param>
    115 /// <param name="e"></param>
    116 private void button1_Click(object sender, EventArgs e)
    117 {
    118 try
    119 {
    120 int intRow;
    121 object s = textBox1.Text.Trim();
    122 // At least one row matches primary key
    123 rowFound = ds.Tables[0].Rows.Find(s); //所搜索的内容,也是设定的主键
    124 if (rowFound != null)
    125 {
    126 MessageBox.Show(rowFound[0].ToString()+","+rowFound[1].ToString());
    127 }
    128 else
    129 {
    130 MessageBox.Show("A row with the primary key of " + s + " could not be found");
    131 }
    132 DataRow[] foundRows;
    133 foundRows = ds.Tables[0].Select("someText Like '涂%'");
    134 if (foundRows != null)
    135 {
    136 MessageBox.Show(foundRows[0].ToString());
    137 }
    138
    139 //// Finds the row specified in txtFindArg
    140 //intRow = ds.Tables[0].DefaultView.Find(s);
    141 ////Debug.WriteLine(intRow);
    142 //if (intRow == -1)
    143 //{
    144 // MessageBox.Show("No PK matches " + textBox1.Text);
    145 //}
    146 //else
    147 //{
    148 //// Jump to the Row and select it
    149 ////dataGridView1.CurrentRow.Index = intRow; //CurrentRowIndex
    150 // dataGridView1.Rows[intRow].Selected=true;
    151 //}
    152 }
    153 catch (Exception ex)
    154 {
    155 MessageBox.Show(ex.ToString());
    156 }
    157
    158 }
    159
    160 private void s()
    161 {
    162
    163 //create a datatable object which will host the two column: notebookID, notebook producer
    164 DataTable o_aTable = new DataTable("Notebooks");
    165
    166
    167 //creating a datacolumn
    168 //definition and initilization of column
    169 DataColumn o_aColumn = new DataColumn();
    170 //defining column properties
    171 //caption
    172 o_aColumn.Caption = "Notebook Producers";
    173 //type of column
    174 o_aColumn.DataType = System.Type.GetType("System.String");
    175 //access name of column
    176 o_aColumn.ColumnName = "Producer";
    177 //default value
    178 o_aColumn.DefaultValue = "unknown producer";
    179
    180 //add column to the table
    181 o_aTable.Columns.Add(o_aColumn);
    182
    183 //initialize a new instance of data column for creating a new column
    184 o_aColumn = new DataColumn();
    185 //defining column properties
    186 //caption
    187 o_aColumn.Caption = "Notebook Producer ID";
    188 //type of column
    189 o_aColumn.DataType = System.Type.GetType("System.Int32");
    190 //access name of column
    191 o_aColumn.ColumnName = "ProducerID";
    192 //default value
    193 o_aColumn.DefaultValue = 0000;
    194
    195 //add new column to the table
    196 o_aTable.Columns.Add(o_aColumn);
    197
    198 //create a primary key column to use search
    199 //definition and initial.
    200 DataColumn[] o_aPrimaryKeyColumn = new DataColumn[1];
    201 //assigning notebookID column of created table to this column: it will serve as primary key column
    202 o_aPrimaryKeyColumn[0] = o_aTable.Columns["ProducerID"];
    203 //mapping primary key column of table to the created primary key holder column
    204 o_aTable.PrimaryKey = o_aPrimaryKeyColumn;
    205
    206 //adding rows-records to column
    207 //create a datarow object which serves as a record entry
    208 DataRow o_aRow;
    209
    210 //adding the records
    211 //add 1th record for producer HP
    212 //initialize a new row for table object
    213 o_aRow = o_aTable.NewRow();
    214 //assign value of 1th column ID
    215 o_aRow["ProducerID"] = 1;
    216 //assign value of 2th column producer
    217 o_aRow["Producer"] = "HP";
    218 //add 1th row to the table
    219 o_aTable.Rows.Add(o_aRow);
    220
    221 //add 2nd record for producer IBM
    222 //initialize a new row for table object
    223 o_aRow = o_aTable.NewRow();
    224 //assign value of 1th column ID
    225 o_aRow["ProducerID"] = 2;
    226 //assign value of 2th column producer
    227 o_aRow["Producer"] = "IBM";
    228 //add 2nd row to the table
    229 o_aTable.Rows.Add(o_aRow);
    230
    231 //display the records within table
    232
    233 for (int i = 0; i < o_aTable.Rows.Count;i++ )
    234 {
    235 //display ID
    236 Console.WriteLine("row " + i + ": notebook ID is: " + o_aTable.Rows[i]["ProducerID"].ToString());
    237 //display producer
    238 Console.WriteLine("row " + i + ": notebook Producer is: " + o_aTable.Rows[i]["Producer"].ToString());
    239
    240 }
    241 //Handling row with specifying a particular primary column addressed by ID
    242 //create a row object to store found row which matches criteria ID
    243 DataRow o_dRow_findedRow;
    244 //look for row with id 1
    245 if ((o_dRow_findedRow = o_aTable.Rows.Find("1")) != null)
    246 {
    247
    248 Console.WriteLine("Primary key column of Table (in memory) is being queried for notebookID 1...");
    249 Console.WriteLine("A row with notebookID 1 is found.");
    250 }
    251 else
    252 {
    253 Console.WriteLine("A record with notebookID 1 is not found.");
    254 }
    255
    256 }
    257
    258
    259 }
    260 }
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    PBRT笔记(3)——KD树
    PBRT笔记(2)——BVH
    PBRT笔记(1)——主循环、浮点误差
    《Ray Tracing in One Weekend》、《Ray Tracing from the Ground Up》读后感以及光线追踪学习推荐
    在Node.js中使用ffi调用dll
    Node.js c++ 扩展之HelloWorld
    在Qt中配置TBB以及简单实用
    对《将Unreal4打包后的工程嵌入到Qt或者桌面中》一文的补充
    QtQuick大坑笔记之Http的Get与Post操作(带cookie)
    QtQuick自定义主题以及控件样式指引
  • 原文地址:https://www.cnblogs.com/geovindu/p/2322419.html
Copyright © 2011-2022 走看看