private void btnDgv_Click(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("id", typeof(string));
dataTable.Columns.Add("value1", typeof(double));
dataTable.Columns.Add("value2", typeof(double));
dataTable.Columns.Add("value3", typeof(int));
for (int i = 0; i < 8; i++)
{
dataTable.Rows.Add(new object[] { "北京市-" + i.ToString(), 100 * i, 234.345 * i, i });//填充数据
}
//dataGridView1.ForeColor = Color.Red;
//dataGridView1.GridColor = Color.Blue;
//dataGridView1.BackgroundColor = Color.Red;
dataGridView1.DataSource = dataTable;
DataGridViewCheckBoxColumn colum1 = new DataGridViewCheckBoxColumn();
colum1.HeaderText="选择框";
colum1.Name="checkbox";
colum1.AutoSizeMode=DataGridViewAutoSizeColumnMode.DisplayedCells;
colum1.FlatStyle=FlatStyle.Standard;
colum1.ThreeState = true;
dataGridView1.Columns.Add(colum1);
//dataGridView1.Columns.Insert(3, colum1);
DataGridViewComboBoxColumn dcomb = new DataGridViewComboBoxColumn();
dcomb.Items.Add("中国");
dcomb.Items.Add("美国");
dcomb.Items.Add("德国");
dcomb.Items.Add("日本");
dcomb.Items.Add("英国");
dcomb.Items.Add("法国");
dcomb.Name = "combo";
dcomb.HeaderText = "国家";
dataGridView1.Columns.Add(dcomb);
//单元格显示图像
DataGridViewImageColumn dgvi = new DataGridViewImageColumn();
dgvi.Name = "Image";
dgvi.Width = 35;
dgvi.ValuesAreIcons = false;
dgvi.Image = new Bitmap("D:\\Backup\\我的文档\\My Pictures\\2.JPG");
dgvi.ImageLayout = DataGridViewImageCellLayout.Zoom;
dgvi.Description = "测试的图片";
dataGridView1.Columns.Add(dgvi);
dataGridView1["Image", 0].Value = new Bitmap("D:\\Backup\\我的文档\\My Pictures\\2.JPG");
DataGridViewButtonColumn col = new DataGridViewButtonColumn();
col.Name = "Button";
col.UseColumnTextForButtonValue = true;
col.Text = "按钮";
dataGridView1.Columns.Add(col);
}
//一次点击打开DataGridViewComboBoxColumn下拉框,否则要两次,第一次选择单元格,第二此才能选择
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -1)
{
SendKeys.Send("{F4}");
}
}
//DataGridViewButtonColumn按钮的触发事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1.Columns[e.ColumnIndex].Name=="Button")
{
MessageBox.Show("触发了按钮");
}
}