datagridView的数据列绑定问题

…衆ロ難τιáo~ 提交于 2020-03-19 12:37:23

单元格格式化数据
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
   
if (e.ColumnIndex == 1 /*status列的Index*/)
    {
       
if (object.Equals(e.Value, 0))
        {
            e.Value
= "未完成";
            e.CellStyle.ForeColor
= Color.Red;
        }
       
else
        {
            e.Value
= "已完成";
            e.CellStyle.ForeColor
= Color.Green;
        }
    }
}

 

行数据格式化数据,一行的单元格数据可以根据另一个单元值而定

 

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {

                int rowsindex=e.RowIndex ;
                int row_count=dataGridView1 .Rows.Count ;
                if (rowsindex < row_count - 1)
                {

                    DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex];
                    try
                    {
                        dgr.Cells["name_values"].Value = dgr.Cells["Column0"].Value.ToString();
                        //dgr.DefaultCellStyle.ForeColor = 设置的颜色;                       
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }


        }

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!