将日期绑定到gridview内的下拉列表

时间:2012-10-26 11:34:18

标签: c# sql sql-server stored-procedures

我必须在gridview中绑定标签和下拉列表,DDL包含日期时间格式的rundates我必须显示与名称相关联的DDL中的所有不同日期。

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataTable dt = new DataTable();
        dt = Common.rundate();
        DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;

        ddl.DataTextField = "RunDate";
        ddl.DataValueField = "TempID";
        ddl.DataSource = dt;
        ddl.DataBind();
    }

}

Store Proc::  

    alter PROC display_rundates
@rundate datetime,@tempid int
AS
  SELECT RunDate,TempID
    FROM History_Table

ORDER BY Rundate DESC
GO

但我必须显示与每个姓名相关的具体情况。可以理解任何帮助。

Name   rundate
Test   datetime(DDL)
Test1   datetime

3 个答案:

答案 0 :(得分:0)

试试这个

ddl.SelectedValue = ((System.Data.DataRowView)(e.Row.DataItem)).Row["TempId"].ToString();

基本上只需在GridView1_RowDataBound期间从数据项中获取“TempId”,并将“TempId”指定为下拉选定值。

像这样的东西

void bindGrid()
{
   grid.DataSource = getRunDates();
   grid.DataBind();
}
DataTable getRunDates()
{
   DataTable dt = new DataTable();
   dt.Columns.Add("TempID");
   dt.Columns.Add("RunDate");
   dt.Rows.Add(new object[] { 1, "20-May-2012" });
   dt.Rows.Add(new object[] { 2, "21-May-2012" });
   dt.Rows.Add(new object[] { 3, "10-May-2012" });
   dt.Rows.Add(new object[] { 4, "20-May-2012" });
   return dt;
}
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      DataTable dt = new DataTable();
      dt = getRunDates();
      DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;

      ddl.DataTextField = "RunDate";
      ddl.DataValueField = "TempID";
      ddl.DataSource = dt;
      ddl.DataBind();

      ddl.SelectedValue = ((System.Data.DataRowView)(e.Row.DataItem)).Row["TempId"].ToString();
    }
}

设计

<asp:GridView runat="server" ID="grid" OnRowDataBound="grid_RowDataBound" AutoGenerateColumns="false">
   <Columns>
      <asp:TemplateField HeaderText="RunDate">
         <ItemTemplate>
             <asp:DropDownList runat="server" ID="DropDownList1" />
         </ItemTemplate>
       </asp:TemplateField>
      <asp:BoundField HeaderText="TempID" DataField="TempID" />
   </Columns>
</asp:GridView>

答案 1 :(得分:0)

绑定gridview时,还必须将主键绑定到某个模板字段控件,如果尚未编写,则添加隐藏字段并绑定到它,如下所示

 <ItemTemplate>
            <asp:HiddenField runat="server" ID="hiddenfield1" Value='<%# Eval("TempID") %>' />
            <asp:DropDownList runat="server" ID="ddl" />
 </ItemTemplate>

RowDataBound事件中,您可以找到PrimaryKey,然后使用它来设置Dropdown Selected Value,如下所示...

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataTable dt = new DataTable();
        dt = Common.rundate();
        DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;

        ddl.DataTextField = "RunDate";
        ddl.DataValueField = "TempID";
        ddl.DataSource = dt;
        ddl.DataBind();

        HiddenField hdn=(HiddenField)row.Cells[cellindex].FindControl("hiddenField1");
        ddl.SelectedValue=hdn.Value;
    }

}

答案 2 :(得分:0)

我仍然不相信我的解决方案,但我认为这是我的方式......

在第一次加载数据时,我会填写一个字典,其中包含每个名称的运行日期:

    private Dictionary<string, List<DateTime>> runDates = new Dictionary<string, List<DateTime>>();

    private void LoadData()
    {
        DataTable table = new DataTable();
        table.Columns.Add("TempName", typeof(string));

        using (var connection = new SqlConnection("YourConnectionString"))
        using (var command = new SqlCommand("SELECT DISTINCT TempName, RunDate FROM History_Table;", connection))
        {
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    string tempName = reader.GetString(0);
                    if (!runDates.ContainsKey(tempName))
                    {
                        DataRow row = table.NewRow();
                        row[0] = tempName;
                        table.Rows.Add(row);
                        runDates.Add(tempName, new List<DateTime>());
                    }
                    runDates[tempName].Add(reader.GetDateTime(1));
                }
            }

            GridView1.DataSource = table;
            GridView1.DataBind();
        }
    }

当读取每一行时,它会检查名称是否已存在,如果不存在,则将其添加到将用于绑定GridView和字典的表中。然后在绑定事件中使用名称标签从字典中查找日期列表:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Find the label with the temp name and assign it
            string tempName = (e.Row.FindControl("Label1") as Label).Value;

            DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;

            ddl.DataTextField = "RunDate";
            ddl.DataValueField = "TempID";
            ddl.DataSource = runDates[tempName];
            ddl.DataBind();
        }

    }

我玩弄了各种其他想法,其中一个将日期列表作为带有唯一名称列表的xml字段返回(与之前的问题非常相似),将其存储在隐藏字段中并对行数据绑定事件进行反序列化,另一个解决方案是在数据集中生成2个表并定义关系,但这在原理上与我提出的方法非常相似,我想我更喜欢这个......