1

我必须在网格视图中绑定标签和下拉菜单,DDL 包含日期时间格式的运行日期,我必须在 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
4

3 回答 3

0

当你绑定gridview时,你还必须将主键绑定到一些模板字段控件,如果你还没有写,那么添加隐藏字段并绑定到它如下

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

RowDataBoundEvent 中,您可以找到 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;
    }

}
于 2012-10-26T12:06:43.857 回答
0

尝试这个

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

基本上只是从数据项中获取“TempId”GridView1_RowDataBound并将该“TempId”分配给下拉的 Selected 值。

像这样的东西

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>
于 2012-10-26T11:49:04.253 回答
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个表并定义关系,但是这与我提出的方法原理上非常相似,我想我更喜欢这个......

于 2012-10-26T13:10:02.430 回答