DataGridViewCell 类

// 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按钮效果。

你可能感兴趣的:(datagridview)