触发从组合框的SelectedIndexChanged事件格式化DataGridView的单元格?

时间:2012-11-18 02:59:35

标签: c# winforms

我正在执行以下请求。

  1. 用户选择ComboBox中的项目。
  2. SelectedIndexChanged事件处理代码根据ComboBox的选定值格式化某些单元格。但是,它不起作用。
  3. void Combobox1_OnSelectedIndexChanged(object sender, ...)
    {
        foreach (DataGridViewRow row in GridView1.Rows)
        {
            var c = GridView1.Columns.Count;
            for (int i = 0; i < c; i++)
            {
                if (...some condition using selected value...) 
                {
                     row.Cells[i].Style.BackColor = Color.Green;
                     row.Cells[i].ToolTipText = "test";
                }
            }
        }
    

    问题:是否应将其添加到事件CellFormatting,..事件中?但是,算法取决于组合框SelectedIndexChanged的事件?

2 个答案:

答案 0 :(得分:0)

在CellFormatting事件中尝试此操作..

Foreach循环

   DataGridViewTextBoxCell txt = new DataGridViewTextBoxCell();
   txt.Value = row.Cells[i].Value
   txt.ToolTipText = "test";
   txt.BackColor = Color.Green;
   row.Cells[i] = txt;

答案 1 :(得分:0)

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            var c = dataGridView1.Columns.Count;
             foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (comboBox1.SelectedValue==1){
                                row.Cells[0].Style.BackColor = Color.Green;
                                row.Cells[0].ToolTipText = "test";
                        }

            else
            {
                        row.Cells[0].Style.BackColor = Color.Blue;
                        row.Cells[0].ToolTipText = "test";
                }
            }
    }

Thanks