将下拉列表添加到GridView单元格

时间:2016-11-21 09:33:18

标签: c# asp.net gridview drop-down-menu tablecell

我想在GridView中创建一个下拉列表列,以便用户可以从此列表中选择一个选项。

GridView代码:

<asp:GridView  style="float:left"  
      ID="gvBookings" 
      ShowHeaderWhenEmpty="true"
      CssClass="tblResults" 
      runat="server" 
      OnRowDataBound="gvBooking_RowDataBound"                             
      DataKeyField="ID" 
      AutoGenerateColumns="false"
      allowpaging="false" />
        <Columns>       
             <asp:BoundField DataField="FinishTime" HeaderText="Finish Time"></asp:BoundField>
             <asp:BoundField DataField="TimeSpentName" HeaderText="Time Spent By"></asp:BoundField>
         </Columns>
     </asp:GridView>

代码背后:

protected void gvBooking_RowDataBound(object sender, GridViewRowEventArgs e)
 {
      BHTaskClass.BookingTask booking = (BHTaskClass.BookingTask)e.Row.DataItem;
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
            int count = 1;
            foreach (TableCell c in e.Row.Cells)
            {
               if (count == 1)
               {
                    string FinishTime = booking.FinishTime.HasValue ? booking.FinishTime.Value.ToString("hh':'mm") : "";
                    c.Text = "<input type=\"text\" id=\"txtFinishTime" + booking.ID + "\" style=\"width:70px\" type=\"text\" onblur=\"UpdateFinishTime(" + booking.ID + ",this.value)\"   value=\"" + FinishTime + "\" >";
               }
                count++;
            }
      }
 }

在后面的代码中,我将FinishTime的单元格更改为文本框。当用户在此处输入值时,它会调用更新数据库的函数。 如何将单元格更改为下拉菜单?我是否可以像我创建文本框一样在后面的代码中执行此操作,或者更改GridView中的Boundfield是否更好

1 个答案:

答案 0 :(得分:0)

我认为你必须开始使用TemplateField而不是BoundField。然后,您可以将TextBox和/或DropDownList直接放在GridView中。然后,您可以使用OnRowCommand事件保存更改。

<asp:GridView ID="gvBookings" runat="server" OnRowDataBound="gvBookings_RowDataBound" OnRowCommand="gvBookings_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("FinishTime") %>'></asp:TextBox>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="saveTextBox" CommandArgument='<%# Container.DataItemIndex %>'>Save</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

背后的代码

protected void gvBookings_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //find the dropdownlist in the current row with findcontrol and cast back to one
        DropDownList dropDownList = e.Row.FindControl("DropDownList1") as DropDownList;

        //add some listitems
        dropDownList.Items.Insert(0, new ListItem("Text A", "A"));
        dropDownList.Items.Insert(1, new ListItem("Text B", "B"));
        dropDownList.Items.Insert(2, new ListItem("Text C", "C"));

        //select a value in the dropdown
        dropDownList.SelectedValue = "B";
    }
}

protected void gvBookings_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //check the commandname
    if (e.CommandName == "saveTextBox")
    {
        //convert the commandargument to a row number
        int rowNumber = Convert.ToInt32(e.CommandArgument);

        //find the textbox in the current row with findcontrol and cast back to one
        TextBox textBox = gvBookings.Rows[rowNumber].FindControl("TextBox1") as TextBox;

        //do stuff with the textbox value
        Label1.Text = textBox.Text;
    }
}