首页 > 代码库 > 委托 C#

委托 C#

1、Form2要委托Form1来做事情就是委托;

2、在Form2中定义委托,让Form1来执行;

3、实例如下所示:

public partial class Form2 : DevComponents.DotNetBar.Office2007Form
    {
        DataSet ds;
        public delegate void ChangeDatagridviewEventHandler(bool topmost);
        public event ChangeDatagridviewEventHandler ChangeDatagridview;
        public w_cancel_appoint_reason()
        {
            InitializeComponent();
        }
private void buttonX1_Click(object sender, EventArgs e)
        {
            ChangeDatagridview(true);//执行委托实例  
        }
}
View Code
public partial class Form1 : DevComponents.DotNetBar.Office2007Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
             Form2 w_car = new Form2 ();
                         //w_car.Show();
                         w_car.ChangeDatagridview += new ChangeDatagridviewEventHandler(ChangeDatagridview);
                         w_car.Show();
        }
}
void ChangeDatagridview(bool topmost)
        {
            ds = PatientAppService.getAppPatInfo(ld_counsel_date, ls_queuename, ls_duration, ls_doctorno);
            dataGridViewX1.DataSource = ds.Tables[0];
            ChangedColor();

        }  
 void ChangedColor()
        {
            foreach (DataGridViewRow dgv in dataGridViewX1.Rows)
            {
                //取特定列的值,列索引是INDEX
                // string ld_dd = dgv.Cells[5].Value as string;
                string ls_status = dgv.Cells[7].Value as string;
                //Console.WriteLine(ls_status);
                switch (ls_status)
                {
                    case "0":
                        dgv.DefaultCellStyle.ForeColor = Color.Red;
                        //dataGridViewX1.RowsDefaultCellStyle.ForeColor = Color.Red;
                        break;
                    case "1":
                        break;
                    case "2":
                        dgv.DefaultCellStyle.ForeColor = Color.Blue;
                        //dataGridViewX1.DefaultCellStyle.ForeColor = Color.Blue;
                        break;
                    case "3":
                        break;
                    default:
                        break;
                }
            }
        }
View Code