如何将gridview单元格设为文本框而不是标签?

时间:2016-06-29 09:22:59

标签: c# asp.net excel gridview

我的gridview应该显示来自Excel数据表的数据。我能够在网格中显示数据,但仅作为具有Excel值的标签。我试图使它成为一个可编辑的gridview,因此保持编辑模式,这就是为什么我需要单元格是文本框。我怎么能这样做?

Visual Explanation

<asp:GridView ID="GridView4" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" OnRowCommand="GridView4_RowCommand" 
            OnSelectedIndexChanged="GridView4_SelectedIndexChanged">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton OnClick="UpdateRow_Click" 
                            ID="LinkButton1" 
                            runat="server" 
                            CausesValidation="false" 
                            CommandName="Update" 
                            Text="Update">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>

1 个答案:

答案 0 :(得分:0)

在其中添加更多ItemTemplate文本框,使用列名设置textboxes值。并且不要忘记将AutoGenerateColumns设置为false

        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton OnClick="UpdateRow_Click" 
                        ID="LinkButton1" 
                        runat="server" 
                        CausesValidation="false" 
                        CommandName="Update" 
                        Text="Update">
                    </asp:LinkButton>
                </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox 
                        ID="textBox1" 
                        runat="server" 
                        Text='<%#Eval("Column1")>'>
                    </asp:TextBox>
                </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox 
                        ID="textBox2" 
                        runat="server" 
                        Text='<%#Eval("Column2")%>'>
                    </asp:TextBox>
                </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox 
                        ID="textBox3" 
                        runat="server" 
                        Text='<%#Eval("Column3")%>'>
                    </asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>