使用 C# DataGridView 寫入幾筆資料測試
namespace GridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//dataGridView1.add = "test";
// Create an unbound DataGridView by declaring a column count.
dataGridView1.ColumnCount = 4;
dataGridView1.ColumnHeadersVisible = true;
// Set the column header names.
dataGridView1.Columns[0].Name = "Recipe";
dataGridView1.Columns[1].Name = "Category";
dataGridView1.Columns[2].Name = "Main Ingredients";
dataGridView1.Columns[3].Name = "Rating";
// Populate the rows.
string[] row1 = new string[] { "Meatloaf", "Main Dish", "ground beef", "**" };
string[] row2 = new string[] { "Key Lime Pie", "Dessert", "lime juice, evaporated milk", "****" };
string[] row3 = new string[] { "Orange-Salsa Pork Chops", "Main Dish", "pork chops, salsa, orange juice", "****" };
string[] row4 = new string[] { "Black Bean and Rice Salad", "Salad", "black beans, brown rice", "****" };
string[] row5 = new string[] { "Chocolate Cheesecake", "Dessert", "cream cheese", "***" };
string[] row6 = new string[] { "Black Bean Dip", "Appetizer", "black beans, sour cream", "***" };
object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };
foreach (string[] rowArray in rows)
{
dataGridView1.Rows.Add(rowArray);
}
}
private void button1_Click(object sender, EventArgs e)
{
// Resize the height of the column headers.
dataGridView1.AutoResizeColumnHeadersHeight();
// Resize all the row heights to fit the contents of all non-header cells.
dataGridView1.AutoResizeRows(
DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
沒有留言:
張貼留言