close
原本只想在DataGridView加入ComboBox的功能, 結果網路一查之下, 發現這也是一門學問,
.NET已經將一些常用的如
DataGridView + ComboBox
DataGridView + CheckBox
DataGridView + Button
DataGridView + Image等諸如此類的元件都已經整合在一起, 真是棒!
但是有些還是得自己手動加入
DataGridView + NumericUpDown
DataGridView + RadioButton
網路上的MSDN範例又臭又長,實在難以下嚥,
幸運地, 花了一點時間總算找到一個簡單一些又可行的範例, 有時間再來慢慢消化囉!
先把目前的資料整合成一個範例程式, 有需要的人可以下載最後面的sample code^_^
Add DataGridView component to windows form
1: private void button1_Click(object sender, EventArgs e)
2: {
3:
4: dataGridView1.ColumnCount = 3; // 定義所需要的行數
5: dataGridView1.Columns[0].Name = "Product ID";
6: dataGridView1.Columns[1].Name = "Product Name";
7: dataGridView1.Columns[2].Name = "Product Price";
8: string[] row = new string[] { "1", "Product 1", "1000" }; // 定義一列的字串陣列
9: dataGridView1.Rows.Add(row); // 加入列
10: row = new string[] { "2", "Product 2", "2000" };
11: dataGridView1.Rows.Add(row);
12: row = new string[] { "3", "Product 3", "3000" };
13: dataGridView1.Rows.Add(row);
14: row = new string[] { "4", "Product 4", "4000" };
15: dataGridView1.Rows.Add(row);
16: }
執行結果
1: private void button2_Click(object sender, EventArgs e)
2: {
3: DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
4: cmb.HeaderText = "ComboBoxColumn";
5: cmb.Name = "cmb";
6: cmb.MaxDropDownItems = 4;
7: cmb.Items.Add("True");
8: cmb.Items.Add("False");
9: cmb.Items.Add("100");
10: dataGridView1.Columns.Add(cmb);
11: }
1: private void button3_Click(object sender, EventArgs e)
2: {
3: for (int i = 0; i < dataGridView1.RowCount - 1; i++)
4: {
5: dataGridView1.Rows[i].Cells[3].Value = "True";
6: }
7: }
1: private void button4_Click(object sender, EventArgs e)
2: {
3: for (int i = 0; i < dataGridView1.RowCount - 1; i++)
4: {
5: dataGridView1.Rows[i].Cells[3].Value = "False";
6: }
7: }
1: private void button5_Click(object sender, EventArgs e)
2: {
3: DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
4:
5: chk.HeaderText = "啟動";
6: chk.Name = "chk";
7: dataGridView1.Columns.Add(chk);
8: }
1: private void button6_Click(object sender, EventArgs e)
2: {
3: for (int i = 0; i < dataGridView1.RowCount - 1; i++)
4: {
5: dataGridView1.Rows[i].Cells[4].Value = true;
6: }
7: }
1: private void button7_Click(object sender, EventArgs e)
2: {
3: DataGridViewImageColumn img = new DataGridViewImageColumn();
4: Image image = Image.FromFile("c:\\bg.jpg");
5: img.Image = image;
6: dataGridView1.Columns.Add(img);
7: img.HeaderText = "背景";
8: img.Name = "img";
9: }
1: private void button8_Click(object sender, EventArgs e)
2: {
3: DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
4: dataGridView1.Columns.Add(btn);
5: btn.HeaderText = "Click Data";
6: btn.Text = "Click Here";
7: btn.Name = "btn";
8: btn.UseColumnTextForButtonValue = true;
9: }
1: private void button9_Click(object sender, EventArgs e)
2: {
3: NumericUpDownColumn num = new NumericUpDownColumn();
4: dataGridView1.Columns.Add(num);
5: num.HeaderText = "NumericUpDown";
6: num.Name = "num";
7: }
1: private void button11_Click(object sender, EventArgs e)
2: {
3: for (int i = 0; i < dataGridView1.RowCount - 1; i++)
4: {
5: MessageBox.Show(dataGridView1.Rows[i].Cells[7].Value.ToString());
6: }
7: }
Sample code: DataGridViewWithComboBox
參考來源:
Add ComboBox to C# DataGridView
Add CheckBox to C# DataGridView
Add Image to C# DataGridView
Add Button to C# DataGridView
DataGridViewComboBoxColumn set the selectedindex
WinForm 如何取得DataGridview中的Button的Click事件
Build a Custom NumericUpDown Cell and Column for the DataGridView Control
- Problem with custom NumericUpDown control in DataGridView
全站熱搜
留言列表