[C#Winform]控件ⅠdataGridView使用代码添加行列、修改单元格内容、修改字体、调整列宽行高
最笨的方法
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { SetupDataGridView(); comboBox1.Items.Add("SairenjiHaruna"); comboBox1.Items.Add("LalaSatalinDeviluke"); comboBox1.Items.Add("YuukiMikan"); comboBox1.Items.Add("MomoBeliaDeviluke"); comboBox1.Items.Add("NanaAstaDeviluke"); comboBox1.Items.Add("KotegawaYui"); comboBox1.Items.Add("Eve"); comboBox1.SelectedIndex = 0; } private void SetupDataGridView() { DataGridViewCheckBoxColumn theCheckBoxColumn = new DataGridViewCheckBoxColumn(); DataGridViewTextBoxColumn theTextBoxColumn0 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn theTextBoxColumn1 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn theTextBoxColumn2 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn theTextBoxColumn3 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn theTextBoxColumn4 = new DataGridViewTextBoxColumn(); dataGridView1.Columns.AddRange(new DataGridViewColumn[] { theCheckBoxColumn, theTextBoxColumn0, theTextBoxColumn1, theTextBoxColumn2, theTextBoxColumn3, theTextBoxColumn4 }); DataGridViewRow theRow0 = new DataGridViewRow(); theRow0.CreateCells(dataGridView1); theRow0.Cells[0].Value = 0; theRow0.Cells[1].Value = "标题1"; theRow0.Cells[2].Value = "标题2"; theRow0.Cells[3].Value = "标题3"; theRow0.Cells[4].Value = "标题4"; theRow0.Cells[5].Value = "标题5"; DataGridViewRow theRow1 = new DataGridViewRow(); theRow1.CreateCells(dataGridView1); theRow1.Cells[0].Value = 0; theRow1.Cells[1].Value = "SairenjiHaruna"; theRow1.Cells[2].Value = "LalaSatalinDeviluke"; theRow1.Cells[3].Value = "YuukiMikan"; theRow1.Cells[4].Value = "MomoBeliaDeviluke"; theRow1.Cells[5].Value = "NanaAstaDeviluke"; DataGridViewRow theRow2 = new DataGridViewRow(); theRow2.CreateCells(dataGridView1); theRow2.Cells[0].Value = 0; theRow2.Cells[1].Value = "SairenjiHaruna"; theRow2.Cells[2].Value = "LalaSatalinDeviluke"; theRow2.Cells[3].Value = "YuukiMikan"; theRow2.Cells[4].Value = "MomoBeliaDeviluke"; theRow2.Cells[5].Value = "NanaAstaDeviluke"; DataGridViewRow theRow3 = new DataGridViewRow(); theRow3.CreateCells(dataGridView1); theRow3.Cells[0].Value = 0; theRow3.Cells[1].Value = "SairenjiHaruna"; theRow3.Cells[2].Value = "LalaSatalinDeviluke"; theRow3.Cells[3].Value = "YuukiMikan"; theRow3.Cells[4].Value = "MomoBeliaDeviluke"; theRow3.Cells[5].Value = "NanaAstaDeviluke"; DataGridViewRow theRow4 = new DataGridViewRow(); theRow4.CreateCells(dataGridView1); theRow4.Cells[0].Value = 0; theRow4.Cells[1].Value = "SairenjiHaruna"; theRow4.Cells[2].Value = "LalaSatalinDeviluke"; theRow4.Cells[3].Value = "YuukiMikan"; theRow4.Cells[4].Value = "MomoBeliaDeviluke"; theRow4.Cells[5].Value = "NanaAstaDeviluke"; DataGridViewRow theRow5 = new DataGridViewRow(); theRow5.CreateCells(dataGridView1); theRow5.Cells[0].Value = 0; theRow5.Cells[1].Value = "SairenjiHaruna"; theRow5.Cells[2].Value = "LalaSatalinDeviluke"; theRow5.Cells[3].Value = "YuukiMikan"; theRow5.Cells[4].Value = "MomoBeliaDeviluke"; theRow5.Cells[5].Value = "NanaAstaDeviluke"; DataGridViewRow theRow6 = new DataGridViewRow(); theRow6.CreateCells(dataGridView1); theRow6.Cells[0].Value = 0; theRow6.Cells[1].Value = "SairenjiHaruna"; theRow6.Cells[2].Value = "LalaSatalinDeviluke"; theRow6.Cells[3].Value = "YuukiMikan"; theRow6.Cells[4].Value = "MomoBeliaDeviluke"; theRow6.Cells[5].Value = "NanaAstaDeviluke"; DataGridViewRow theRow7 = new DataGridViewRow(); theRow7.CreateCells(dataGridView1); theRow7.Cells[0].Value = 0; theRow7.Cells[1].Value = "SairenjiHaruna"; theRow7.Cells[2].Value = "LalaSatalinDeviluke"; theRow7.Cells[3].Value = "YuukiMikan"; theRow7.Cells[4].Value = "MomoBeliaDeviluke"; theRow7.Cells[5].Value = "NanaAstaDeviluke"; dataGridView1.ColumnHeadersVisible = false; dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; dataGridView1.Columns[0].ReadOnly = false; dataGridView1.Columns[1].ReadOnly = true; dataGridView1.Columns[2].ReadOnly = true; dataGridView1.Columns[3].ReadOnly = true; dataGridView1.Columns[4].ReadOnly = true; dataGridView1.Columns[5].ReadOnly = true; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.Rows.Add(theRow0); dataGridView1.Rows.Add(theRow1); dataGridView1.Rows.Add(theRow2); dataGridView1.Rows.Add(theRow3); dataGridView1.Rows.Add(theRow4); dataGridView1.Rows.Add(theRow5); dataGridView1.Rows.Add(theRow6); dataGridView1.Rows.Add(theRow7); dataGridView1.Rows[0].DefaultCellStyle.Font = new Font("Microsoft YaHei", 12, FontStyle.Bold); dataGridView1.Rows[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.RowHeadersVisible = false; dataGridView1.DefaultCellStyle.Font = new Font("Microsoft YaHei", 12, FontStyle.Regular); dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders; dataGridView1.Rows[0].Cells[0].Value = 1; dataGridView1.Rows[1].Cells[0].Value = 1; dataGridView1.Rows[2].Cells[0].Value = 1; dataGridView1.Rows[3].Cells[0].Value = 1; dataGridView1.Rows[4].Cells[0].Value = 1; dataGridView1.Rows[5].Cells[0].Value = 1; dataGridView1.Rows[6].Cells[0].Value = 1; dataGridView1.Rows[7].Cells[0].Value = 1; dataGridView1.ClearSelection(); } private void textBox1_TextChanged(object sender, EventArgs e) { dataGridView1[4, 1].Value = textBox1.Text; dataGridView1[4, 2].Value = textBox1.Text; dataGridView1[4, 3].Value = textBox1.Text; dataGridView1[4, 4].Value = textBox1.Text; dataGridView1[4, 5].Value = textBox1.Text; dataGridView1[4, 6].Value = textBox1.Text; dataGridView1[4, 7].Value = textBox1.Text; } private void comboBox1_DropDownClosed(object sender, EventArgs e) { dataGridView1[5, 1].Value = comboBox1.Text; dataGridView1[5, 2].Value = comboBox1.Text; dataGridView1[5, 3].Value = comboBox1.Text; dataGridView1[5, 4].Value = comboBox1.Text; dataGridView1[5, 5].Value = comboBox1.Text; dataGridView1[5, 6].Value = comboBox1.Text; dataGridView1[5, 7].Value = comboBox1.Text; } } }