目录
45. DataGridView列中显示选择框CheckBox
46. DataGridView中显示下拉框ComboBox
47. DataGridView单击打开下拉框
48. DataGridView中显示按钮
49. DataGridView中显示链接
50. DataGridView中显示图像
45. DataGridView列中显示选择框CheckBox
[VB.NET]
'添加CheckBox列
Dim column As New DataGridViewCheckBoxColumn
DataGridView1.Columns.Add(column)
[C#]
//添加CheckBox列
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
DataGridView1.Columns.Add(column);
中间状态在内的三种状态表示
[VB.NET]
'能够显示3种检查状态
Dim column As DataGridViewCheckBoxColumn = CType(DataGridView1.Columns(0), DataGridViewCheckBoxColumn)
column.ThreeState = True
[C#]
DataGridViewCheckBoxColumn column =(DataGridViewCheckBoxColumn)DataGridView1.Columns[0];
column.ThreeState = true;
46. DataGridView中显示下拉框ComboBox
[VB.NET]
Dim column As New DataGridViewComboBoxColumn()
'在ComboBox的列表中指定要显示的项目
column.Items.Add("日曜日")
column.Items.Add("月曜日")
column.Items.Add("火曜日")
column.Items.Add("水曜日")
column.Items.Add("木曜日")
column.Items.Add("金曜日")
column.Items.Add("土曜日")
'"Week"显示绑定在列中的数据
column.DataPropertyName = "Week"
DataGridView1.Columns.Insert(DataGridView1.Columns("Week").Index, column)
DataGridView1.Columns.Remove("Week")
column.Name = "Week"
[C#]
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
//显示绑定在列中的数据
column.Items.Add("日曜日");
column.Items.Add("月曜日");
column.Items.Add("火曜日");
column.Items.Add("水曜日");
column.Items.Add("木曜日");
column.Items.Add("金曜日");
column.Items.Add("土曜日");
//"Week"显示绑定在列中的数据
column.DataPropertyName = "Week";
DataGridView1.Columns.Insert(DataGridView1.Columns["Week"].Index, column);
DataGridView1.Columns.Remove("Week");
column.Name = "Week";
通过列Data绑定设置ComboBox
[VB.NET]
Dim weekTable As New DataTable("WeekTable")
weekTable.Columns.Add("Display", GetType(String))
weekTable.Columns.Add("Value", GetType(Integer))
weekTable.Rows.Add("日曜日", 0)
weekTable.Rows.Add("月曜日", 1)
weekTable.Rows.Add("火曜日", 2)
weekTable.Rows.Add("水曜日", 3)
weekTable.Rows.Add("木曜日", 4)
weekTable.Rows.Add("金曜日", 5)
weekTable.Rows.Add("土曜日", 6)
Dim column As New DataGridViewComboBoxColumn()
column.DataPropertyName = "Week"
'DataGridViewComboBoxColumnのDataSourceを設定
column.DataSource = weekTable
column.ValueMember = "Value"
column.DisplayMember = "Display"
DataGridView1.Columns.Add(column)
[C#]
DataTable weekTable = new DataTable("WeekTable");
weekTable.Columns.Add("Display", typeof(string));
weekTable.Columns.Add("Value", typeof(int));
weekTable.Rows.Add("日曜日", 0);
weekTable.Rows.Add("月曜日", 1);
weekTable.Rows.Add("火曜日", 2);
weekTable.Rows.Add("水曜日", 3);
weekTable.Rows.Add("木曜日", 4);
weekTable.Rows.Add("金曜日", 5);
weekTable.Rows.Add("土曜日", 6);
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.DataPropertyName = "Week";
column.DataSource = weekTable;
column.ValueMember = "Value";
column.DisplayMember = "Display";
DataGridView1.Columns.Add(column);
默认状态下,所有下拉框都显示;DisplayStyleForCurrentCellOnly=True的状态下,当前的单元格显示下拉框,其余不显示;还有一种就是光标移动时强调显示。如下图左中右三列。
47. DataGridView单击打开下拉框
通常情况下要打开下拉框需要点击目标单元格三次,第一次选中单元格,第二次进入编辑状态,第三次才能打开下拉框
[VB.NET]
Private Sub DataGridView1_CellEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellEnter
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.Columns(e.ColumnIndex).Name = "ComboBox" AndAlso _
TypeOf dgv.Columns(e.ColumnIndex) Is DataGridViewComboBoxColumn Then
SendKeys.Send("{F4}")
End If
End Sub
[C#]
private void DataGridView1_CellEnter(object sender,
DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "ComboBox" &&
dgv.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
{
SendKeys.Send("{F4}");
}
}
48. DataGridView中显示按钮
[VB.NET]
Dim column As New DataGridViewButtonColumn()
column.Name = "Button"
column.UseColumnTextForButtonValue = True
column.Text = "详细阅读"
DataGridView1.Columns.Add(column)
[C#]
DataGridViewButtonColumn column = new DataGridViewButtonColumn();
column.Name = "Button";
column.UseColumnTextForButtonValue = true;
column.Text = "详细阅读";
DataGridView1.Columns.Add(column);
按钮按下事件取得
[VB.NET]
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellContentClick
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.Columns(e.ColumnIndex).Name = "Button" Then
MessageBox.Show((e.RowIndex.ToString() + _
"行按钮被点击了。"))
End If
End Sub
[C#]
private void DataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "Button")
{
MessageBox.Show(e.RowIndex.ToString() +
"行按钮被点击了。");
}
}
49. DataGridView中显示链接
[VB.NET]
Dim column As New DataGridViewLinkColumn()
column.Name = "Link"
column.UseColumnTextForLinkValue = True
column.Text = "详细阅读"
column.LinkBehavior = LinkBehavior.HoverUnderline
column.TrackVisitedState = True
DataGridView1.Columns.Add(column)
[C#]
DataGridViewLinkColumn column = new DataGridViewLinkColumn();
column.Name = "Link";
column.UseColumnTextForLinkValue = true;
column.Text = "详细阅读";
column.LinkBehavior = LinkBehavior.HoverUnderline;
column.TrackVisitedState = true;
DataGridView1.Columns.Add(column);
链接按下事件取得
[VB.NET]
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellContentClick
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.Columns(e.ColumnIndex).Name = "Link" Then
MessageBox.Show((e.RowIndex.ToString() + _
"行按钮被点击了。"))
Dim cell As DataGridViewLinkCell = _
CType(dgv(e.ColumnIndex, e.RowIndex), DataGridViewLinkCell)
cell.LinkVisited = True
End If
End Sub
[C#]
private void DataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "Link")
{
MessageBox.Show(e.RowIndex.ToString() +
"行按钮被点击了。");
DataGridViewLinkCell cell =
(DataGridViewLinkCell)dgv[e.ColumnIndex, e.RowIndex];
cell.LinkVisited = true;
}
}
50. DataGridView中显示图像
[VB.NET]
Dim column As New DataGridViewImageColumn()
column.Name = "Image"
column.ValuesAreIcons = False
column.Image = New Bitmap("C:/null.gif")
column.ImageLayout = DataGridViewImageCellLayout.Zoom
column.Description = "图像"
DataGridView1.Columns.Add(column)
DataGridView1("Image", 0).Value = New Bitmap("C:/top.gif") '
[C#]
DataGridViewImageColumn column = new DataGridViewImageColumn();
column.Name = "Image";
column.ValuesAreIcons = false;
column.Image = new Bitmap("C://null.gif");
column.ImageLayout = DataGridViewImageCellLayout.Zoom;
column.Description = "图像";
DataGridView1.Columns.Add(column);
DataGridView1["Image", 0].Value = new Bitmap("C://top.gif");
图片属性单元格未设值时红差不显示的设定
[VB.NET]
Dim imageColumn As DataGridViewImageColumn = _
CType(DataGridView1.Columns("Image"), DataGridViewImageColumn)
imageColumn.DefaultCellStyle.NullValue = Nothing
[C#]
DataGridViewImageColumn imageColumn =
(DataGridViewImageColumn)DataGridView1.Columns["Image"];
imageColumn.DefaultCellStyle.NullValue = null;
欢迎微信技术交流: