DataGridViewCell 类

注意:此类在 .NET Framework 2.0 版中是新增的。

表示 DataGridView 控件中的单个单元格。

命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
示例

下面的代码示例演示此类型的用法。有关此示例的更多信息,请参见 如何:为 Windows 窗体 DataGridView 控件中的单个单元格添加工具提示

C#
// Sets the ToolTip text for cells in the Rating column.
void dataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
{
    
if ( (e.ColumnIndex == this.dataGridView1.Columns["Rating"].Index)
        
&& e.Value != null )
    {
        DataGridViewCell cell 
= 
            
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        
if (e.Value.Equals("*"))
        {                
            cell.ToolTipText 
= "very bad";
        }
        
else if (e.Value.Equals("**"))
        {
            cell.ToolTipText 
= "bad";
        }
        
else if (e.Value.Equals("***"))
        {
            cell.ToolTipText 
= "good";
        }
        
else if (e.Value.Equals("****"))
        {
            cell.ToolTipText 
= "very good";
        }
    }
}


 

            DataGridViewCell cell = dgViewer.Rows[0].Cells[0];
            DataGridViewColumn dgvc = new DataGridViewColumn(cell);
            //dgvc.DefaultCellStyle.BackColor = Color.CadetBlue;
            dgvc.ReadOnly = true;
            dgvc.HeaderText = header;
            //dgvc.Tag = type;
            dgvc.Name = name;
            int? index = null;
            index = dgViewer.Columns[dgViewer.Columns.Count - 1].Index;
            dgViewer.Columns.Insert(index.Value + 1, dgvc);

网友的:

添加各列: 
DataGridViewTextBoxColumn TXTcolumn 
= new DataGridViewTextBoxColumn(); 
TXTcolumn.DataPropertyName 
= "字段"
TXTcolumn.HeaderText 
= "名称"
TXTcolumn.Name 
= "名称"
TXTcolumn.Width 
= 60
DataGridView1.Columns.Add(TXTcolumn); 

依次添加所有列。 
其他列可根据需要设置DefaultCellStyle。 
如果要突起的效果,还可以用DataGridViewButtonColumn按钮效果。
原文地址:https://www.cnblogs.com/xvqm00/p/1437086.html