将一种形式选择的日期显示为另一种形式

时间:2014-05-19 11:15:27

标签: c#

我想使用monthCalender控件显示以其他形式选择的日期。 这是我的代码..

private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {            
        ActivityScheduler frm1 = new ActivityScheduler();
        frm1.Show();
    } 

private void ActivityScheduler_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Open) { con.Close(); }
        con.Open();
        RemainderPopUp frm = new RemainderPopUp();
        string s = "select * from [Activity_Scheduler]";
        SqlCommand sCmd = new SqlCommand(s, con);
        SqlDataAdapter da = new SqlDataAdapter(sCmd);
        DataSet ds = new DataSet();

        da.Fill(ds, "[Activity_Scheduler]");
        datagridActivityScheduler.DataSource = ds.Tables[0];

        datagridActivityScheduler.AllowUserToAddRows = true;
        DataTable dt = new DataTable();
        dt = ds.Tables["Activity_Scheduler"];
        if (dt == null)
        {

            datagridActivityScheduler.Rows[0].Cells[3].Value = frm.monthCalendar1.SelectionRange.Start.ToShortDateString();
            MessageBox.Show(datagridActivityScheduler.Rows[0].Cells[3].Value.ToString());

        }
        con.Close();
    }               

它在messagebox中显示正确的值..但该值未显示在datagridview中... Plz任何人帮助我..

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

 form_load()
 {
 private string myDate="1999/12/12";

    // Declare a Date property of type string:
    public string Name
    {
        get 
        {
           return myDate; 
        }
        set 
        {
           myName = value; 
        }
 }

private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{            
    ActivityScheduler frm1 = new ActivityScheduler();
    ActivityScheduler.myDate = frm.monthCalendar1.SelectionRange.Start.ToShortDateString()
    frm1.Show();
} 

现在您可以在其他表单上获取日期:

string myDate = frm1.myDate();

如果有任何疑问,请告诉我。