在GridView中插入行
Posted swdhywhd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在GridView中插入行相关的知识,希望对你有一定的参考价值。
GridView是个功能强大的控件,有时侯,我们用它来装载多行数据并编辑,最后再批量更新。在编辑时,很可能会需要在其中插入一些行用于添加新的数据,下面提供一个示例以演示实现的基本方法。
页面文件:
代码文件:
页面文件:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="line" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:CommandField HeaderText="插入" SelectText="插入行" ShowSelectButton="True" />
<asp:TemplateField HeaderText="yy">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=‘<%#DataBinder.Eval(Container.DataItem,"yy") %>‘></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="line" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:CommandField HeaderText="插入" SelectText="插入行" ShowSelectButton="True" />
<asp:TemplateField HeaderText="yy">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=‘<%#DataBinder.Eval(Container.DataItem,"yy") %>‘></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
代码文件:
//初始化数据
private void BindData()
{
DataTable dt = CreateTable();
DataRow dr = dt.NewRow();
dr["yy"] = "hello";
dr["line"] = 0;
dt.Rows.Add(dr);
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
//创建一个表
private DataTable CreateTable()
{
DataTable dt = new DataTable("mytable");
DataColumn dc = new DataColumn("yy", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("line", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
return dt;
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
DataTable dt = CreateTable();
foreach (GridViewRow gvr in GridView1.Rows)
{
if (e.NewSelectedIndex == int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString()))//插入行的位置
{
DataRow dr = dt.NewRow();
dr["yy"] = "";
dr["line"] = e.NewSelectedIndex;
dt.Rows.Add(dr);
dr = dt.NewRow();
TextBox tb = (TextBox)gvr.Cells[1].FindControl("TextBox1");
dr["yy"] = tb.Text;
dr["line"] = e.NewSelectedIndex + 1;
dt.Rows.Add(dr);
}
else if (e.NewSelectedIndex < int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString()))//插入行之后
{
DataRow dr = dt.NewRow();
TextBox tb = (TextBox)gvr.Cells[1].FindControl("TextBox1");
dr["yy"] = tb.Text;
dr["line"] = int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString()) + 1;
dt.Rows.Add(dr);
}
else//插入行之前
{
DataRow dr = dt.NewRow();
TextBox tb = (TextBox)gvr.Cells[1].FindControl("TextBox1");
dr["yy"] = tb.Text;
dr["line"] = GridView1.DataKeys[gvr.RowIndex][0].ToString();
dt.Rows.Add(dr);
}
}
//重新绑定
DataView dv = dt.DefaultView;
dv.Sort = "line";//指定排序字段
GridView1.DataSource = dv;
GridView1.DataBind();
}
private void BindData()
{
DataTable dt = CreateTable();
DataRow dr = dt.NewRow();
dr["yy"] = "hello";
dr["line"] = 0;
dt.Rows.Add(dr);
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
//创建一个表
private DataTable CreateTable()
{
DataTable dt = new DataTable("mytable");
DataColumn dc = new DataColumn("yy", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("line", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
return dt;
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
DataTable dt = CreateTable();
foreach (GridViewRow gvr in GridView1.Rows)
{
if (e.NewSelectedIndex == int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString()))//插入行的位置
{
DataRow dr = dt.NewRow();
dr["yy"] = "";
dr["line"] = e.NewSelectedIndex;
dt.Rows.Add(dr);
dr = dt.NewRow();
TextBox tb = (TextBox)gvr.Cells[1].FindControl("TextBox1");
dr["yy"] = tb.Text;
dr["line"] = e.NewSelectedIndex + 1;
dt.Rows.Add(dr);
}
else if (e.NewSelectedIndex < int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString()))//插入行之后
{
DataRow dr = dt.NewRow();
TextBox tb = (TextBox)gvr.Cells[1].FindControl("TextBox1");
dr["yy"] = tb.Text;
dr["line"] = int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString()) + 1;
dt.Rows.Add(dr);
}
else//插入行之前
{
DataRow dr = dt.NewRow();
TextBox tb = (TextBox)gvr.Cells[1].FindControl("TextBox1");
dr["yy"] = tb.Text;
dr["line"] = GridView1.DataKeys[gvr.RowIndex][0].ToString();
dt.Rows.Add(dr);
}
}
//重新绑定
DataView dv = dt.DefaultView;
dv.Sort = "line";//指定排序字段
GridView1.DataSource = dv;
GridView1.DataBind();
}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
以上是关于在GridView中插入行的主要内容,如果未能解决你的问题,请参考以下文章