【发布时间】: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 实际上,我正在以这种形式处理我的项目和所有项目,然后我不能使用实体框架,并且“创建表”按钮只是按钮,我在后面添加了功能稍后按钮,请给我一些提示,我不知道我该怎么做