如何在编辑gridview时添加手动控件? ASP.NET C#

时间:2016-04-14 13:41:24

标签: c# asp.net gridview

我在google上搜索但是我无法找到解决方案。但是我的视觉工作室2010中没有工作或者e.row工作。我怎么能得到这个属性。当我点击&#34时我想要它;编辑"按钮然后gridview有不同的columsn和一个列名是"数据类型"然后这个专栏填写"下拉列表"我从下拉列表中选择值。我的下拉控件ID为" dropdownlist1"我怎么能这样做?。这是我的代码" gridview编辑"事件代码:

protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
  DataTable t = (DataTable)Session["MyDataTable"];
  GridView2.EditIndex = e.NewEditIndex;
  DropDownList ddlName =
                (DropDownList)e.Row.FindControl("dropdownlist1"); // HERE IS ERROR AT "Row" 
  ddlName.DataTextField = "Data Type";
  ddlName.DataValueField = "Data Type";
  GridView2.DataSource = t;
  GridView2.DataBind();
}

这是我的aspx代码:

  <asp:GridView ID="GridView2" runat="server" AutoGenerateDeleteButton="True" 
        AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" 
        ShowHeaderWhenEmpty="True" onrowcancelingedit="GridView2_RowCancelingEdit" 
        onrowdeleting="GridView2_RowDeleting" onrowediting="GridView2_RowEditing" 
        onrowupdating="GridView2_RowUpdating" AutoGenerateColumns="False" 
        Width="92px">
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FFF1D4" />
        <SortedAscendingHeaderStyle BackColor="#B95C30" />
        <SortedDescendingCellStyle BackColor="#F1E5CE" />
        <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>

</div>
<asp:TextBox ID="TextBox1" runat="server" Width="137px"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="20px" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="140px">
    <asp:ListItem>int</asp:ListItem>
    <asp:ListItem>Varchar</asp:ListItem>
</asp:DropDownList>
<asp:CheckBox ID="Null" runat="server" />
<asp:CheckBox ID="Primary" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" 
    style="margin-left: 59px" Width="56px" />
<asp:Button ID="Button2" runat="server" Height="33px" style="margin-left: 76px" 
    Text="Create Table" Width="94px" />

我是在正确的方式吗?或者我该怎么做?还告诉我如何更新gridview行。我在gridview中手动插入值。没有数据库连接等等。谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用索引检索行:

GridViewRow row = GridView2.Rows[e.NewEditIndex];


更新

您可以将DropDownList放在GridView的TemplateField中:

<asp:GridView ...>
    ...
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Literal runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DataTypeValue")) %>' />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddlTypeName" runat="server" ... />
        </EditItemTemplate>
    </asp:TemplateField>
    ...
</asp:GridView>

(我展示了一个可选的ItemTemplate,以防你想要显示所选的数据类型)

然后您可以在事件处理程序中检索DropDownList:

GridViewRow row = GridView2.Rows[e.NewEditIndex];
DropDownList ddlTypeName = (DropDownList)row.FindControl("ddlTypeName");
...