如何向Gridview添加新行?

时间:2012-12-03 15:07:07

标签: c# asp.net gridview

我有一个带有3个TemplateFields的Gridview:1个带有DropDownList,另外2个带有Texbox。

我设法获得了GridView中的第一个空行。现在,我需要在DropDownList的SelectedIndexChanged事件中添加另一行。

我怎样才能做到这一点?

由于

1 个答案:

答案 0 :(得分:0)

<强> Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    Width="317px">
                    <Columns>
                        <asp:TemplateField HeaderText="Text 1">
                            <ItemTemplate>
                                <asp:TextBox runat="server" Visible='<%# Eval("isLast") %>' ID="Text1"></asp:TextBox>
                                <asp:Label runat="server" Visible='<%# !(bool)Eval("isLast") %>' Text='<%# Eval("text1") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Text 2">
                            <ItemTemplate>
                                <asp:TextBox runat="server" Visible='<%# Eval("isLast") %>' ID="Text2"></asp:TextBox>
                                <asp:Label runat="server" Visible='<%# !(bool)Eval("isLast") %>' Text='<%# Eval("text2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:DropDownList ID='DropDownList' Visible='<%# Eval("isLast") %>' runat="server" 
                                    onselectedindexchanged="DropDownList_SelectedIndexChanged" AutoPostBack="true">
                                    <asp:ListItem Text="Option 1" Value="1" />
                                    <asp:ListItem Text="Option 2" Value="2" />
                                    <asp:ListItem Text="Option 3" Value="3" />
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

<强> Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable("table1");
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            dt.Columns.Add("text1");
            dt.Columns.Add("text2");
            dt.Columns.Add("isLast", Type.GetType("System.Boolean"));
            for (int i = 0; i < 3; i++)
            {
                dt.Rows.Add(new object[] { 
                    i, 
                    i < 2 ? "text 1 row " + i : string.Empty, 
                    i < 2 ? "text 2 row " + i : string.Empty, 
                    i == 2 });
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Session["dataTable"] = dt; // change to sql data source
        }
    }
    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        DataTable dt = (DataTable)Session["dataTable"];
        var maxId = dt.AsEnumerable().Max(row => Convert.ToInt32(row["ID"]));
        var dataRow = dt.AsEnumerable().Where(row => (int)row["ID"] == maxId)
                        .Select(row => row)
                        .FirstOrDefault();
        dataRow["text1"] = ((TextBox)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Text1")).Text;
        dataRow["text2"] = ((TextBox)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Text2")).Text;
        dataRow["isLast"] = false;
        dt.Rows.Add(new object[] { maxId + 1, string.Empty, string.Empty, true });
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Session["dataTable"] = dt;
    }
}