日历到数据网格

本文关键字:网格 数据网 数据 日历 | 更新日期: 2023-09-27 18:04:21

我似乎真的无法确定完成任务需要什么。我有一个数据网格在asp.net在VS2010这显示数据从SQL数据库,在这些字段之一是一个"开始日期"。

我也有一个简单的日历运行。我想要的是能够更新我的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();

                    }

日历到数据网格

您需要过滤您的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中的一列。注意:这只是一个代码片段!