2

我有一个绑定到 DataTable 的 GridView,当您选择编辑时,我可以获得要更改的值。但是,一个字段必须是下拉列表而不是文本框。这是我到目前为止的代码。

protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {               
            DataTable dt = (DataTable)Session["table"];
            //Update the values.
            GridViewRow row = griditems.Rows[e.RowIndex];
            dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text.ToUpper();
            dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text.ToUpper();
            dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
             //dt.Rows[row.DataItemIndex]["Shipping Method"] = ((TextBox)(row.Cells[5].Controls[0])).Text;

            DropDownList cmbType = (DropDownList)griditems.Rows[e.RowIndex].FindControl("Shipping Method");

            griditems.EditIndex = -1;
            BindData();
        }
    }

当我取消注释运输方式为文本框的行时,它就像它所说的那样,是一个文本框,而不是一个下拉列表。我尝试将其更改为 DropDownList ,但没有成功。

在.aspx 文件中:

<asp:GridView ID="griditems" runat="server" 
        onrowdeleting="griditems_RowDeleting" onrowediting="griditems_RowEditing"
        onrowupdating="griditems_RowUpdating" AllowPaging="True" onpageindexchanging="griditems_PageIndexChanging" 
        onrowcancelingedit="griditems_RowCancelingEdit" ViewStateMode="Enabled" Caption="Order Details" 
        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
        onrowdatabound="griditems_RowDataBound" >            
        <EditRowStyle BackColor="#FF9900" BorderStyle="Double" /></asp:GridView>

并在生成表时:

public void CreateTable()
    {
        DataTable table = new DataTable();
        if (Session["table"] != null) 
            table = (DataTable)Session["table"]; 
        else 
        {
            table.Columns.Add("Part", typeof(string));
            table.Columns.Add("Quantity", typeof(Int32));
            table.Columns.Add("Ship-To", typeof(string));
            table.Columns.Add("Requested Date", typeof(string));
            table.Columns.Add("Shipping Method", typeof(string));
        }

        DataRow row = table.NewRow();
        row["Part"] = part;
        row["Quantity"] = qty;
        row["Ship-To"] = shipto;
        row["Requested Date"] = reqdate;
        row["Shipping Method"] = shipmthd;
        table.Rows.Add(row);

        Session["table"] = table;
        griditems.DataSource = table.DefaultView;
        griditems.DataBind();
    }
4

2 回答 2

1

尝试dis:

Default.aspx:( 最佳实践使用 GridView 中的模板字段)

  <asp:GridView ID="gvshipping" runat="server" AutoGenerateColumns="False" 
            onrowcancelingedit="gvshipping_RowCancelingEdit" 
            onrowdatabound="gvshipping_RowDataBound" 
            onrowediting="gvshipping_RowEditing" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="Part">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("part") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" Text='<%# Bind("part") %>' runat="server"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Quatity">
                <ItemTemplate>
                    <asp:Label ID="lblqty" runat="server"  Text='<%# Bind("quantity") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtqty" runat="server" Text='<%# Bind("quantity") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Ship to">
                <ItemTemplate>
                    <asp:Label ID="lblshipto" runat="server" Text='<%# Bind("ShipTo") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtshipto" runat="server" Text='<%# Bind("ShipTo") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Post date">
                <ItemTemplate>
                    <asp:Label ID="lblpostdate"  runat="server" Text='<%# Bind("RequestedDate")%>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtpostdate" Text='<%# Bind("RequestedDate")%>' runat="server"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Shipping Method">
                <ItemTemplate>
                    <asp:Label ID="lblshipmethod" runat="server" Text='<%# Bind("ShippingMethod")%>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server">
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>

                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                        CommandName="Edit" Text="Edit"></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                        CommandName="Update" Text="Update"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
   {
      gvBind();
   }
 }

 public void gvBind()
  {
     DataTable dt = createTable();
     Session["tb"] = dt;
     gvshipping.DataSource = Session["tb"];
     gvshipping.DataBind();
  }

  protected void gvshipping_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList dpshipmethod = (DropDownList)e.Row.FindControl("DropDownList1");
                //bind dropdownlist
                DataTable dt = shipingmethodTable();
                dpshipmethod.DataSource = dt;
                dpshipmethod.DataTextField = "ShippingMethod";
                dpshipmethod.DataValueField = "Id";
                dpshipmethod.DataBind();
                DataRowView dr = e.Row.DataItem as DataRowView;
                dpshipmethod.SelectedItem.Text = dr["ShippingMethod"].ToString();

            }
        }
    }

 protected void gvshipping_RowEditing(object sender, GridViewEditEventArgs e)
{
      gvshipping.EditIndex = e.NewEditIndex;
      vBind();
 }

 protected void gvshipping_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
 {
   gvshipping.EditIndex = -1;
    gvBind();
 }


    public DataTable createTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Part", typeof(string));
        dt.Columns.Add("Quantity", typeof(Int32));
        dt.Columns.Add("ShipTo", typeof(string));
        dt.Columns.Add("RequestedDate", typeof(string));
        dt.Columns.Add("ShippingMethod", typeof(string));

        string date= DateTime.Now.ToShortDateString();
        DataRow row = dt.NewRow();
        row["Part"] = "Anchor";
        row["Quantity"] = "10";
        row["ShipTo"] = "blah";
        row["RequestedDate"] = date;
        row["ShippingMethod"] = "Charge by subtotal";
        dt.Rows.Add(row);


        DataRow row1 = dt.NewRow();
        row1["Part"] = "blade";
        row1["Quantity"] = "88";
        row1["ShipTo"] = "blah";
        row1["RequestedDate"] = date;
        row1["ShippingMethod"] = "Charge by quantity";
        dt.Rows.Add(row1);

        DataRow row2 = dt.NewRow();
        row2["Part"] = "cabin";
        row2["Quantity"] = "4";
        row2["ShipTo"] = "blah";
        row2["RequestedDate"] = date;
        row2["ShippingMethod"] = "Charge by subtotal";
        dt.Rows.Add(row2);

        DataRow row3 = dt.NewRow();
        row3["Part"] = "cockpit";
        row3["Quantity"] = "11";
        row3["ShipTo"] = "blah";
        row3["RequestedDate"] = date;
        row3["ShippingMethod"] = "Charge by weight";
        dt.Rows.Add(row3);

        DataRow row4 = dt.NewRow();
        row4["Part"] = "jack";
        row4["Quantity"] = "45";
        row4["ShipTo"] = "blah";
        row4["RequestedDate"] = date;
        row4["ShippingMethod"] = "Charge by weight";
        dt.Rows.Add(row4);

        DataRow row5 = dt.NewRow();
        row5["Part"] = "cabin";
        row5["Quantity"] = "67";
        row5["ShipTo"] = "blah";
        row5["RequestedDate"] = date;
        row5["ShippingMethod"] = "Charge by weight";
        dt.Rows.Add(row5);

        DataRow row6 = dt.NewRow();
        row6["Part"] = "blade";
        row6["Quantity"] = "4";
        row6["ShipTo"] = "blah";
        row6["RequestedDate"] = date;
        row6["ShippingMethod"] = "Charge by weight";
        dt.Rows.Add(row6);
        return dt;
    }

    public DataTable shipingmethodTable()
    {
        DataTable dtshipingmethod = new DataTable();
        dtshipingmethod.Columns.Add("Id", typeof(Int32));
        dtshipingmethod.Columns.Add("ShippingMethod", typeof(string));

        DataRow row = dtshipingmethod.NewRow();
        row["Id"] = 1;
        row["ShippingMethod"] = "Charge by subtotal";
        dtshipingmethod.Rows.Add(row);

        DataRow row1 = dtshipingmethod.NewRow();
        row1["Id"] = 2;
        row1["ShippingMethod"] = "Charge by weight";
        dtshipingmethod.Rows.Add(row1);

        DataRow row2 = dtshipingmethod.NewRow();
        row2["Id"] = 3;
        row2["ShippingMethod"] = "Charge by quantity";
        dtshipingmethod.Rows.Add(row2);

        return dtshipingmethod;

    }

截屏:

在此处输入图像描述

于 2012-08-30T07:25:17.513 回答
0

你可以在这个的帮助下继续:

dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)row.FindControl("DropdownList1")).SelectedItem;

编辑:

dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)
(row.Cells[5].Controls[0])).SelectedItem.ToString();
于 2012-08-28T13:04:42.080 回答