如何将下拉列表添加为gridview项

时间:2014-01-01 16:24:58

标签: asp.net sql-server-2008-r2

我的表单在gridview中有三列。一个是数量(下拉列表){如何添加此数量下拉列表?},其他是价格和金额。我想计算gridview内的数量。如果我选择数量“2”,那么它计算数量*价格。如何在gridview或任何其他选项中获取selectedindexchanged属性下拉列表?

如何在gridview项目中添加下拉列表? 如何在gridview内的下拉列表中添加值? 如何为asp.net中gridview内的下拉列表编写selectedindexchanged事件的代码?

2 个答案:

答案 0 :(得分:11)

您的问题分为三个部分:

  • 如何在GridView中添加DropDownList?
  • 如何从GridView中的DropDownList中获取所选项目?
  • 如何计算值并在GridView中显示?

以下是我们如何做到这一点。首先在页面中添加此标记。我为产品名称添加了一个列,使其看起来更好:

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:DropDownList ID="ddlQuantity" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlQuantity_SelectedIndexChanged"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <asp:Label ID="lblPrice" Text='<%#Eval("Price") %>' runat="server" ></asp:Label>
            </ItemTemplate>

        </asp:TemplateField>
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>                        
                <asp:Label ID="lblAmount" Text="0.00" runat="server" ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

如何在GridView中添加DropDownList

在Page_Load()中填充GridView(我使用了产品列表):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Test data to populate GridView
        GridView1.DataSource = new List<Product>()
        {
            new Product{ID=1, Name="Paper", Price=7.99M},
            new Product{ID=2, Name="Pen", Price=14.99M},
            new Product{ID=3, Name="Pencil", Price=1.99M}
        };

        GridView1.DataBind();
    }
}

这将触发GridView的{​​{1}}事件。在event方法中,我们将在每行中绑定DropDownList,如下所示:

RowDataBound

如何从GridView中的DropDownList获取所选项目和

如何计算值并在GridView中显示

当您更改DropDownList中的任何选择时,它会触发SelectedIndexChange事件。在那个事件方法中,我们可以找到哪个DropDownList被更改,而且它的“NamingContainer” - GridView的行保存它:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ddl = e.Row.FindControl("ddlQuantity") as DropDownList;
        if (ddl != null)
        {
            ddl.DataSource = new List<string>() { "0", "1", "2", "3", "4" };
            ddl.DataBind();
        }
    }
}

这是结果:

enter image description here

您可以下载测试项目here

答案 1 :(得分:0)

试试这个示例代码。

<asp:GridView ID="grEventi" runat="server" BackColor="White" 
              ShowHeaderWhenEmpty="True" AutoGenerateColumns="False"
              BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
              CellPadding="3" Width="100%" OnRowDataBound="grEventi_RowDataBound"
              onselectedindexchanged="grEventi_SelectedIndexChanged" >
    <SelectedRowStyle CssClass="selectedRow" />
    <Columns>
        <asp:BoundField DataField="Elenco Eventi" HeaderText="Evento" />
        <asp:TemplateField  ItemStyle-Wrap="false" ItemStyle-Width="150" HeaderText="Data Inizio">
            <ItemTemplate>
                <asp:Label ID="lbl_data" runat="server" Text="" Visible="false" > 
                </asp:Label>
                <asp:DropDownList ID="ddl_data" runat="server" Visible="false" OnSelectedIndexChanged="ddl_dat_SelectedIndexChanged"  ClientIDMode = "Static" class ="calendar">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
</Columns>       
</asp:GridView>

代码隐藏:

protected void ddl_dat_SelectedIndexChanged(object sender, EventArgs e)
{
    //your logic goes here
    string test = "";    
}

protected void grEventi_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

e.Row.Attributes.Add("onclick", this.ClientScript.GetPostBackEventReference((Control)sender, "Select$" + e.Row.RowIndex));

        DropDownList ddl_dat = (DropDownList)e.Row.FindControl("ddl_data");
        ddl_dat.SelectedValue = DataBinder.Eval(e.Row.DataItem, "data inizio").ToString();
        ddl_dat.Visible = true;
        ddl_dat.DataTextFormatString = "{0: ddd d/MM/yyyy HH:mm}";
        ddl_dat.DataTextField = "data inizio";
        ddl_dat.DataValueField = "data inizio";

        ddl_dat.DataSource = mydata;
        // ddl_dat.AutoPostBack = true;
        ddl_dat.DataBind();                
    }
    else 
    {
        Label lbl_data = (Label)e.Row.FindControl("lbl_data");
        lbl_data.Visible = true;
        DateTime date=(DateTime)dr.Row["data inizio"];

        lbl_data.Text = date.ToString("ddd d/MM/yyyy HH:mm");
    }
}

protected void grEventi_SelectedIndexChanged(object sender, EventArgs e)
{
    //my logic code
}

希望这对你有所帮助。