如何在gridview行中添加不同的控件

时间:2015-04-21 10:31:10

标签: c# asp.net gridview

我有一个gridview我从列表数据填充。 gridview中的每一行都有一个文本框。 gridview中有一行我需要dropdown控制而不是textbox。我无法弄清楚如何从网格中的一行将文本框更改为下拉控件。

我的网格视图如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  style="width:100%;" ShowHeader="false"
                     CellPadding="3" BackColor="White" ForeColor="Black" Font-Bold="false" GridLines="None"
                                    RowStyle-CssClass="GridRow">
    <Columns>
          <asp:TemplateField Visible="false">
               <ItemTemplate>
                   <asp:Label ID="lbl_ItemID" runat="server" Text='<%# Eval("GroupItemTypeID") %>' ></asp:Label>
               </ItemTemplate>
            </asp:TemplateField>
          <asp:TemplateField Visible="false">
               <ItemTemplate>
                   <asp:Label ID="lbl_ItemCode" runat="server" Text='<%# Eval("GroupItemTypeCode") %>' ></asp:Label>
               </ItemTemplate>
          </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="310px" >
            <ItemTemplate>
                <asp:Label ID="lbl_ItemValuesName" runat="server" Text='<%# Eval("ControlName") %>'  ></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="245px">
            <ItemTemplate>
                <asp:TextBox ID="txtPrice" runat="server"  CssClass="form-control"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-CssClass="RowWid">
            <ItemTemplate>
                <asp:Label ID="lbl_IsPercentbased" runat="server" Text='<%# Eval("PercentBasedText") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField ItemStyle-Width="70px">
            <ItemTemplate>
                <asp:CheckBox ID="ChkIsPercent" runat="server"  />
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

背后的代码

private void GetItemValues()
{
    List<Entities.ItemValues> IValues = new List<Entities.ItemValues>();

    IValues = BLL.PriceGroupItemValues.GetAllPriceGroupItemValues();

    GridView1.DataSource = IValues;

    GridView1.DataBind();

}

3 个答案:

答案 0 :(得分:1)

你可以在你的情况下采用这种方法:

TextBoxDropDown放在当前TextBox所在的TemplateField,并在下拉列表中设置Visible=false

现在,在您的代码隐藏中,您可以使用GridView.RowDataBound事件随时随地显示TextBoxDropdown。像这样:

public void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // set Dropdown visible = true/false as per your requirement.
    }
    else
    {
      // set Textboxvisible = true/false as per your requirement.
    }
  }

此事件将针对GridView中的耳朵行启动,并根据指定的条件,您可以操纵要在该特定行中显示的控件。你可以找到这样的下拉控件:

foreach (GridViewRow row in GridView1.Rows)
{
    categoryName = ((DropDownList)row.FindControl("ddlCategoryName"));
}

希望这有帮助。

答案 1 :(得分:0)

使用占位符控件而不是文本框,并根据您的条件以编程方式将文本框或DDL添加到rowdatabound事件中的占位符控件

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                  DataSourceID="SqlDataSource1"
                  DataKeyNames="user_id">
      <Columns>
        <asp:BoundField DataField="user_id" HeaderText="user_id" ReadOnly="True" InsertVisible="False"
                    SortExpression="user_id"></asp:BoundField>
        <asp:BoundField DataField="user_logon" HeaderText="user_logon" SortExpression="user_logon">
                </asp:BoundField>

        <asp:TemplateField>
          <ItemTemplate>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>

示例数据源

'Primary Datasource to bind to the gridview
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:SomeConnectionString %>'
    SelectCommand="select top 10 user_id, user_logon from your_user_table"></asp:SqlDataSource>

'Secondary datasource to bind to the ddl
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString='<%$ ConnectionStrings:SomeConnectionString %>'
    SelectCommand="select top 5 user_id, user_logon from your_user_table"></asp:SqlDataSource>

代码落后,基于user_id是奇数还是我放置一个或另一个控件:

Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ph As PlaceHolder = e.Row.FindControl("PlaceHolder1")

        If GridView1.DataKeys(e.Row.RowIndex).Value Mod 2 = 0 Then
            Dim tb As New TextBox()
            tb.Enabled = False
            tb.Text = CType(e.Row.DataItem, DataRowView)("user_logon")
            ph.Controls.Add(tb)
        Else
            Dim ddl As New DropDownList()
            ddl.DataSourceID = SqlDataSource2.ID
            ddl.DataTextField = "user_logon"
            ddl.DataValueField = "user_id"
            ph.Controls.Add(ddl)
            ddl.DataBind()
        End If

    End If
End Sub

根据您的评论编辑:

Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ph As PlaceHolder = e.Row.FindControl("PlaceHolder1")

        If CType(e.Row.DataItem, DataRowView)("GroupItemTypeID") ="SUBFREQ" Then
            Dim ddl As New DropDownList()
            ddl.DataSourceID = <substitute your requirements>
            ddl.DataTextField = ...
            ddl.DataValueField = ...
            ph.Controls.Add(ddl)
            ddl.DataBind()
        Else
            Dim tb As New TextBox()
            tb.Enabled = ...whatever...
            tb.Text = CType(e.Row.DataItem, DataRowView)("GroupItemTypeID")
            ph.Controls.Add(tb)
        End If

    End If
End Sub

答案 2 :(得分:0)

是否有人解决了Harvey提供的解决方案以及我现有的代码。在这里发表我的答案。

在gridview中,创建了一个控件的可控性和可见性(下拉列表)为false

 <asp:TemplateField ItemStyle-Width="245px">
     <ItemTemplate>
         <asp:TextBox ID="txtPrice" runat="server"  CssClass="form-control"></asp:TextBox>
          <asp:DropDownList ID="ddFrequencyBilling" runat="server" CssClass="form-control" Visible="false"></asp:DropDownList>
    </ItemTemplate>
 </asp:TemplateField>

在gridview的RowDataBound事件中

 if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //foreach( GridViewRow row in GridView1.Rows)
                //{
                    String ItemCode = (e.Row.FindControl("lbl_ItemCode") as Label).Text;

                    if ( ItemCode == "SUBFREQ")
                    {
                        List<Entities.ItemValues> PGItemValues = new List<Entities.ItemValues>();

                        TextBox TextBoxtemp = ((TextBox)e.Row.FindControl("txtPrice"));

                        TextBoxtemp.Visible = false;

                        Label lbel = ((Label)e.Row.FindControl("lbl_IsPercentbased"));

                        lbel.Visible = false;

                        CheckBox chk = ((CheckBox)e.Row.FindControl("ChkIsPercent"));

                        chk.Visible = false;

                        DropDownList dd1 = ((DropDownList)e.Row.FindControl("ddFrequencyBilling"));

                        dd1.Visible = true;

                        PGItemValues = BLL.PriceGroupItemValues.GetItemValueOnCode(ItemCode, 1);

                        dd1.DataSource = PGItemValues;

                        dd1.DataTextField = "IValue";

                        dd1.DataValueField = "ItemCode";

                        dd1.DataBind();
                    }
                //}
            }

是的,它有效。非常感谢你们。