DataGridview中对列的指定操作C#

时间:2016-10-26 15:41:25

标签: c# datagridview

enter image description here

此代码用于添加列' Check-Out'并将所有值设置为绿色:

DataGridViewButtonColumn column = new DataGridViewButtonColumn
{
    FlatStyle = FlatStyle.System;
    DefaultCellStyle.ForeColor = Color.ForestGreen;
    HeaderText = "Check-Out";
    Text = "Check-Out";
    UseColumnTextForButtonValue = true;
};

dataGridView1.Columns.Add(column);

我想选择例如id = 1,字段' Check-Out' ...改变绿色'退房'到Red' Check-Out'

我已尝试使用此代码(但适用于所有字段......):

foreach (DataGridViewRow dr in dataGridView1.Rows)
{
    string col1 = dr.Cells["Check-Out"].ToString();

    dr.DefaultCellStyle.ForeColor = Color.Red;
}

我有一个错误:"找不到名为Check-Out的列" !!!

由于

2 个答案:

答案 0 :(得分:1)

您必须为列添加名称:

getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);

答案 1 :(得分:0)

我找到了解决方案

这是代码:

  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int col = this.dataGridView1.CurrentCell.ColumnIndex;
        int row = this.dataGridView1.CurrentCell.RowIndex;

        dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
    }

谢谢大家