小弟在winform里使用datagridview控件,前4列绑定了数据,第5列为动态添加的  一列 按钮,用于 控制(开或关)。
这是第5列动态添加的简单程序
            DataGridViewButtonColumn buttonColumn=new DataGridViewButtonColumn();
            buttonColumn.UseColumnTextForButtonValue = true;
            buttonColumn.HeaderText = "控制";
            dataGridView1.Columns.Add(buttonColumn);
我现在不知道
1.怎么才能根据前面绑定列的内容,来修改 按钮 的text属性——前面的数据是“开”。按钮则显示“关”;前面绑定的数据显示是“关”,按钮则为“开”。
2.按钮 点下之后的操作程序应该怎么写?(怎么才能获得点下按钮的click事件)?
请高手给指点一下,在线等啊

解决方案 »

  1.   

    对于你的第二个问题只需要dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
    就可以了,就是订购CellContentClick 的事件,dataGridView1_CellContentClick自己实现就行了
      

  2.   

     1,在DataGridView的CellFormatting事件中添加
        if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    if (你自己定义的条件)
                    {
                        this.DataGridView.Rows[e.RowIndex].Cells[buttonColumn的名字
    ].Value = “开”;
                    }
                    else
                   {this.DataGridView.Rows[e.RowIndex].Cells[buttonColumn的名字
    ].Value = “关”;
    }
                }2,在DataGridView的CellContentClick 事件中添加  if(e.RowIndex !=-1 && e.ColumnIndex !=-1)
                {
                    if (this.DataGridView.Columns[e.ColumnIndex].Name   == buttonColumn的名字
                    {
                                //是你自己要添加的方法
                    }
                }
      

  3.   

    CellFormatting事件(类似网页中gridview行绑定事件)
    buttonColumn.OnClick+=...3L正解