C# .NET(VS2019) DeskTopApplication [DataGridView + DataReader] (2020-02)







using System;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Drawing;


            String ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\shn\source\repos\MAILDBSYSTEM\MailDB.accdb";
            OleDbConnection conn;
            OleDbCommand comm;

            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            dataGridView1.ColumnCount = 4;

            // カラム名を指定
            dataGridView1.Columns[0].HeaderText = "ID";
            dataGridView1.Columns[1].HeaderText = "氏名";
            dataGridView1.Columns[2].HeaderText = "住所";
            dataGridView1.Columns[3].HeaderText = "電話番号";

            try {
                conn = new OleDbConnection();
                conn.ConnectionString = ConnectionString;
                conn.Open();
                comm = new OleDbCommand();

                comm.CommandText = "SELECT * FROM USR WHERE USR LIKE '%" + textBox4.Text + "%'";
                comm.Connection = conn;
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            OleDbDataReader reader = comm.ExecuteReader();

            // 鈴木一朗の場合色付け
            int x = 0;
            while (reader.Read()) {
                dataGridView1.Rows.Add(reader["ID"], reader["USR"], reader["ADDS"], reader["TEL"]);
                
		if ((string)reader["USR"] == "鈴木一郎") {
                     dataGridView1[3, x].Style.BackColor = Color.Pink;
                }
                x++;
            }
          
            conn.Close();
            conn.Dispose();


戻る