|
■No44925 (こあら さん) に返信
教えていただいた所を参考に
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DtatGridVew
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
SetupDataGridView();
PopulateDataGridView();
}
private void SetupDataGridView()
{
dataGridView1.ColumnCount = 5;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);
dataGridView1.Name = "songsDataGridView";
dataGridView1.Location = new Point(8, 8);
dataGridView1.Size = new Size(800, 300);
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
dataGridView1.GridColor = Color.Black;
dataGridView1.RowHeadersVisible = false;
dataGridView1.Columns[0].Name = "Release Date";
dataGridView1.Columns[1].Name = "Track";
dataGridView1.Columns[2].Name = "Title";
dataGridView1.Columns[3].Name = "Artist";
dataGridView1.Columns[4].Name = "Album";
dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Italic);
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
{
if (e != null)
{
if (e.Value != null)
{
try
{
e.Value = DateTime.Parse(e.Value.ToString()).ToLongDateString();
e.FormattingApplied = true;
}
catch (FormatException)
{
MessageBox.Show(e.Value.ToString() + "is not a valid date.");
}
}
}
}
}
private void PopulateDataGridView()
{
string[] row0 = { "11/22/1968", "29", "Revolution 9", "Beatles", "The Beatles [White Album]" };
string[] row1 = { "10/10/1960", "6", "Fools Rush In", "Frank Sinatra", "Nice 'N' Easy" };
string[] row2 = { "11/11/1971", "1", "One of These Days", "Pink Floyd", "Meddle" };
string[] row3 = { "2/3/1988", "7", "Where Is My Mind?", "Pixies", "Surfer Rosa" };
string[] row4 = { "4/5/1981", "9", "Can't Find My Mind", "Cramps", "Psychedelic Jungle" };
string[] row5 = { "6/10/2003", "13", "Scatterbrain. (As Dead As Leaves.)", "Radiohead", "Hail to the Thief" };
string[] row6 = { "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };
dataGridView1.Columns[0].DisplayIndex = 3;
dataGridView1.Columns[1].DisplayIndex = 4;
dataGridView1.Columns[2].DisplayIndex = 0;
dataGridView1.Columns[3].DisplayIndex = 1;
dataGridView1.Columns[4].DisplayIndex = 2;
dataGridView1.Rows.Add(row0);
dataGridView1.Rows.Add(row1);
dataGridView1.Rows.Add(row2);
dataGridView1.Rows.Add(row3);
dataGridView1.Rows.Add(row4);
dataGridView1.Rows.Add(row5);
dataGridView1.Rows.Add(row6);
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
というようにいたしまして目的の動作をさせることができました。本当にありがとうございました。
|