2011-08-02 6 views
1

私は自分の仕事を達成するために必要なものを釘付けにすることはできません。私はVS2010のasp.netにDataGridを持っています。これは、SQLデータベースからのデータを表示します。これらのフィールドの1つに「StartDate」があります。DataGridへのカレンダー

また、私は簡単なカレンダーを実行しています。私が望むのは、選択した日付を渡してDataGridを更新し、それを「StartDate」としてDataGridの選択を呼び出すことです。その開始日のレコードのみが表示されます。

私はオンラインで見ましたが、例は非常に古いか、少し混乱しています。

これを達成するために必要なステップ(可能であればサンプルコード)をレイアウトすることができれば、私はしばらく検索していますが、感謝しています。

ありがとうございました。

私は現在、私が間違っているのは何

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 

      Calendar1.VisibleDate = DateTime.Today; 
      strConn = @"Data Source=PC88;Initial Catalog=Bookings;Integrated Security=True";  
      mycn = new SqlConnection(strConn); 
      myda = new SqlDataAdapter("Select * FROM Bookings", mycn); 
      myda.Fill(ds, "Table"); 
     } 
     else 
     { 
      // CalendarChange(); 
      Calendar1.VisibleDate = DateTime.Today; 
      strConn = @"Data Source=PC88;Initial Catalog=Bookings;Integrated Security=True";  
      mycn = new SqlConnection(strConn); 
      myda = new SqlDataAdapter("Select * FROM Bookings", mycn); 
      myda.Fill(ds, "Table"); 

     } 

    } 

    protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e) 
    { 
     // If the month is CurrentMonth 
     if (!e.Day.IsOtherMonth) 
     { 
      foreach (DataRow dr in ds.Tables[0].Rows) 
      { 
       if ((dr["Start"].ToString() != DBNull.Value.ToString())) 
       { 
        DateTime dtEvent = (DateTime)dr["Start"]; 
        if (dtEvent.Equals(e.Day.Date)) 
        { 
         e.Cell.BackColor = Color.PaleVioletRed; 
        } 
       } 
      } 
     } 
     //If the month is not CurrentMonth then hide the Dates 
     else 
     { 
     e.Cell.Text = ""; 
     } 
     } 


     private void Calendar1_SelectionChanged(object sender, System.EventArgs e) 
     { 
     myda = new SqlDataAdapter("Select * from Bookings where Start ='" + Calendar1.SelectedDate.ToString() + "'", mycn); 
     dsSelDate = new DataSet(); 
     myda.Fill(dsSelDate, "AllTables"); 
     if (dsSelDate.Tables[0].Rows.Count == 0) 
     { 
     GridView1.Visible = false; 
     } 
     else 
     { 
     GridView1.Visible = true; 
     GridView1.DataSource = dsSelDate; 
     GridView1.DataBind(); 
     } 
     } 
    } 

でいる何?。誰でも同様の問題/要件を持っている場合

EDIT__

最後にそれが動作するには、ここに私のコードです。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      if (!IsPostBack) 
      { 

       Calendar1.VisibleDate = DateTime.Today; 
       strConn = @"Data Source=BBC-PC-S054683;Initial Catalog=Bookings;Integrated Security=True"; 
       mycn = new SqlConnection(strConn); 
       myda = new SqlDataAdapter("Select * FROM Bookings", mycn); 
       myda.Fill(ds, "Table"); 

      } 
      else 
      { // CalendarChange(); 
       Calendar1.VisibleDate = DateTime.Today; 
       strConn = @"Data Source=PC-Name;Initial Catalog=Bookings;Integrated Security=True"; 
       mycn = new SqlConnection(strConn); 
       myda = new SqlDataAdapter("Select * FROM Bookings", mycn); 
       myda.Fill(ds, "Table"); 

      } 

      BindGrid(); 
     } 
    } 
    private DataTable GetRecords() 
    { 
     SqlConnection conn = new SqlConnection(strConnection); 
     conn.Open(); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = conn; 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "Select * from Bookings"; 
     SqlDataAdapter dAdapter = new SqlDataAdapter(); 
     dAdapter.SelectCommand = cmd; 
     DataSet objDs = new DataSet(); 
     dAdapter.Fill(objDs); 
     return objDs.Tables[0]; 


    } 

    public void Calendar1_SelectionChanged(object sender, System.EventArgs e) 
    { 

     String Date = Calendar1.SelectedDate.ToLongDateString(); 
     SqlConnection con = new SqlConnection(strConnection);//put connection string here 
     SqlCommand objCommand = new SqlCommand("SELECT * FROM Bookings where Date = '" + Date + "'", con);//let MyTable be a database table 
     con.Open(); 
     SqlDataReader dr = objCommand.ExecuteReader(); 

     GridView.DataSource = dr; 
     GridView.DataBind(); 



        } 
+0

isPostBackをisPostBack内に配置する必要はありません。どちらの条件も常に同じ結果になります。 – tsilb

答えて

2

DataGridViewをフィルタリングする必要があります。

try 
{ 
    myDataSet = new DataSet(); 
    myDataSet.CaseSensitive = true; 

    DataAdapter.SelectCommand.Connection = myConnection; 
    DataAdapter.TableMappings.Clear(); 
    DataAdapter.TableMappings.Add("Table", "TableName"); 
    DataAdapter.Fill(myDataSet); 

    myDataView = new DataView(myDataSet.Tables["TableName"], "TIMESTAMP >= '" + 
    Convert.ToDateTime(fromDate) + "' AND TIMESTAMP <= '" + 
    Convert.ToDateTime(toDate) + "'", "TIMESTAMP", DataViewRowState.CurrentRows); 

    dgv.DataSource = myDataView; 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

TIMESTAMPは、私のDataGridViewの列です。 注:これは単なるコードスニペットです!

関連する問題