繁体   English   中英

如何将值插入gridview行? ASP.NET,C#

[英]How can insert values into gridview rows ? ASP.NET ,C#

我的问题是如何将值插入gridviews行中。基本上我已经创建了一个gridview,其中我绑定了页脚并且我想在页脚中插入值。我创建了一个'文本框','下拉列表'和'复选框'。我想要插入值并按“插入”按钮,然后在网格视图中显示的值,然后再次插入值并按按钮,然后在网格视图中显示插入的值。这是我的网格视图图像 图片

我也想编辑和删除行。这是我的代码:aspx代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        Height="104px" ShowFooter="True" Width="463px" 
        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
        <Columns>
            <asp:TemplateField HeaderText="Column Name">
            <FooterTemplate>
             <asp:TextBox ID="name" runat="server"></asp:TextBox>
             </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Data Type">
            <FooterTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server">
                </asp:DropDownList>
             </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Allow Null">
            <FooterTemplate>
                <asp:CheckBox ID="allow_null" runat="server" />
            </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Primary Key">
            <FooterTemplate>
                <asp:CheckBox ID="primary" runat="server" />
            </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

这是我的aspx.cs代码:

protected void Button1_Click(object sender, EventArgs e)
{
    string name = TextBox1.Text;
    string type = DropDownList1.Text;
    string allow=Null.Text;
    string primary = Primary.Text;
    name = ((TextBox)GridView1.FooterRow.FindControl("TextBox2")).Text;
    type = ((DropDownList)GridView1.FooterRow.FindControl("DropDownList2")).Text;
    allow = ((CheckBox)GridView1.FooterRow.FindControl("allow_null")).Text;
    primary = ((CheckBox)GridView1.FooterRow.FindControl("primary")).Text; 
}

要以这种方式使用GridView,在页脚中有输入字段,并且在GridView外部有一个Insert按钮,则需要进行手动插入。

我不知道如何使用EF模型执行插入操作,因为我目前不使用它。 我猜您可以说“旧”方式是使用SqlConnection和SqlCommand的实例。 您可以在Button Click事件中使用与此类似的代码:

// This is VB.  C# is similar, you will have to convert where needed

Using connection As New SqlConnection(connectionString)
  Using command As New SqlCommand(queryString, connection)
    command.Connection.Open()

    command.CommandType = // Typically Text or StoredProcedure as needed
    command.Parameters.AddWithValue("@parameter_name1", some_value_1)
    command.Parameters.AddWithValue("@parameter_name2", some_value_2)
    .
    .
    .
    command.ExecuteNonQuery()
  End Using
End Using

queryString是您的sql INSERT语句或存储过程

command.Parameters是INSERT语句或Stored Proc中可替换参数的集合。


附录

Gridview是一个数据绑定控件,因此通常在使用gridview时,它会绑定到某些后备数据源。 如果不使用数据库,则使用其他构造。

例如,如果使用的是DataTable,则应使用其行和列方法将新的行和列添加到DataTable,然后将其重新绑定到Gridview。 您不会将行和列直接添加到GridView中。

有关示例,请参见JonH的其他SO答案

如果这有帮助,可以基于ASPSnippets,但可以进行修改以使其更符合您的需要。 请注意,不是使用DataTable,而是使用List,其中“ Row”是一个类。 您使用的是个人喜好问题。 “行”类为:

[Serializable]
public class Row
{
    public string FieldName { get; set; }
    public string FieldType { get; set; }
    public Boolean FieldNullible { get; set; }
    public Boolean FieldPrimaryKey { get; set; }
}

我的名字和你的名字不同。 请注意,ASPSnippets示例不使用TemplateFields,因此我不使用。 我不确定是否需要将“ allow”和/或“ primary”字段设为布尔值,因此我没有以表格形式对其进行处理。 所以我拥有的ASP.Net形式是:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    Height="104px" Width="463px"
    AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
    <Columns>
        <asp:BoundField DataField="FieldName" HeaderText="Name" ItemStyle-Width="120" />
        <asp:BoundField DataField="FieldType" HeaderText="Type" ItemStyle-Width="120" />
    </Columns>
</asp:GridView>
<br /><asp:Label ID="Label1" runat="server" Text="Name:"></asp:Label>
<br /><asp:TextBox ID="txtName" runat="server" />
<br /><asp:Label ID="Label2" runat="server" Text="Type:"></asp:Label>
<br /><asp:TextBox ID="txtType" runat="server" />
<br /><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />

后面的代码是:

protected void Page_Load(object sender, EventArgs e)
{
    if (this.IsPostBack)
        return;
    List<Row> Rows = new List<Row>();
    ViewState["Rows"] = Rows;
    BindGrid();
}

protected void BindGrid()
{
    GridView1.DataSource = (List<Row>)ViewState["Rows"];
    GridView1.DataBind();
}

protected void Insert(object sender, EventArgs e)
    {
        List<Row> Rows = (List < Row >)ViewState["Rows"];
        Row r = new Row();
        r.FieldName = txtName.Text.Trim();
        r.FieldType = txtType.Text.Trim();
        Rows.Add(r);
        ViewState["Rows"] = Rows;
        BindGrid();
        txtName.Text = string.Empty;
        txtType.Text = string.Empty;
}

我不喜欢的一件事是,只有其中包含数据,GridView才会显示。 我想你可以解决这个问题。

这不会实现编辑和删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM