【问题标题】:How can insert values into gridview rows ? ASP.NET ,C#如何将值插入到 gridview 行中? ASP.NET ,C#
【发布时间】:2016-04-12 16:35:58
【问题描述】:

我的问题是如何将值插入到 gridviews 行中。基本上我已经创建了一个 gridview,我在其中绑定了页脚,我想在页脚中插入值。我创建了一个“文本框”、“下拉列表”和“复选框'。我想要当我插入值并按下“插入”按钮然后在 gridview 中显示值,然后我再次插入值并按下按钮然后在 gridview 中显示插入的值。这是我的 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; 
}

【问题讨论】:

  • 在您的点击事件中,提供一个数据源(例如可以是一个 DataSet),然后设置您的 GridView 的 DataSource 属性并调用 DataBind 方法。
  • @JCM thnks 响应,但我不知道我该怎么做,我试试我做过的?你能重新排列我的“代码”吗?请
  • 也许有人会回答您提出的问题,但可能更好的解决方案是创建 JCM 所说的数据源,然后使用它来创建 GridView。换句话说,使用实体框架。我看到一个“创建表”按钮,这是做什么用的?是的,学习实体框架和数据源需要时间,但这些知识将在未来节省时间。如果数据不是用于数据库的,那么它将帮助我们了解数据是如何存储的,最佳答案取决于此。
  • @user34660 实际上,我正在以这种形式处理我的项目和所有项目,然后我不能使用实体框架,并且“创建表”按钮只是按钮,我在后面添加了功能稍后按钮,请给我一些提示,我不知道我该怎么做

标签: c# asp.net gridview


【解决方案1】:

要以这种方式使用 GridView,在页脚中输入字段和在 GridView 之外的插入按钮,您需要进行手动插入。

我不知道您如何使用 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 语句或存储过程中可替换参数的集合。


附录

Gridview 是一个数据绑定控件,因此通常当您使用gridview 时,它会绑定到某个支持数据源。如果您不使用数据库,那么您正在使用其他构造。

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

See this other SO answer by JonH for an example

【讨论】:

  • 说得好,但我不想将数据插入SQL数据库,我想将数据插入gridview,那为什么我需要QUERY STRING和SQL INSERT命令????
  • 我的误会,你不需要那个。我会附上我的答案。
  • 你能给我代码吗?????我不知道我该怎么做?这是我的第一次尝试
  • 添加了与您的类似的另一个 SO 答案的链接
  • 你能检查这个链接吗? aspsnippets.com/Articles/… 它有助于解决我的问题吗?
【解决方案2】:

如果这有帮助,这是基于 ASPSnippets 但修改为更像您可能需要的。请注意,我使用的是“行”是类的列表,而不是使用 DataTable。您使用哪一个在某种程度上取决于个人喜好。 “行”类是:

[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,所以我没有。我不确定您是否需要“允许” 和/或“主要”字段布尔值,所以我没有在表单中处理它们。所以我拥有的 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 直到其中有数据才显示。我想你可以解决这个问题。

这不实现编辑和删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-16
    • 2021-01-14
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多